Java Lambda 构造函数引用(二) ClassName::new
@FunctionalInterface
public interface TriFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
public class Color {
private Integer a;
private Integer b;
private Integer c;
public Color() {
}
public Color(Integer a, Integer b, Integer c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
public Integer getC() {
return c;
}
public void setC(Integer c) {
this.c = c;
}
public static Color getColor(Integer a, Integer b, Integer c, TriFunction<Integer, Integer, Integer, Color> f) {
return f.apply(a, b, c);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TriFunction<Integer, Integer, Integer, Color> triFun = Color::new;
Color color = triFun.apply(1, 2, 3);
System.out.println(color.getA() + " " + color.getB() + " " + color.getC());
Color cc = getColor(4, 5, 6, Color::new);
System.out.println(cc.getA() + " " + cc.getB() + " " + cc.getC());
TriFunction<Integer, Integer, Integer, Color> tf = (a, b, c) -> new Color(a, b, c);
Color ccc = getColor(7, 8, 9, tf);
System.out.println(ccc.getA() + " " + ccc.getB() + " " + ccc.getC());
}
}
1 2 3
4 5 6
7 8 9
|