1、构造器引用:
与方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致,抽象方法的返回值烈性即为构造器所属的类的类型。
2、数组引用
可以将数组视为特殊的类,则写法与构造器引用一致。
3、代码:
// Supplier中的T get()
// Employee的空参构造器
@Test
public void test1() {
Supplier<Employee> sup = new Supplier<Employee>() {
@Override
public Employee get() {
return new Employee();
}
};
Supplier<Employee> sup1 = () -> new Employee();
System.out.println(sup1.get());
// 构造器引用
Supplier<Employee> sup2 = Employee::new;
System.out.println(sup2.get());
}
// 数组引用
// Function中的R apply(T t)
@Test
public void test2() {
Function<Integer,String[]> function = length -> new String[length];
String[] arrays = function.apply(5);
System.out.println(Arrays.toString(arrays));
//数组引用
Function<Integer,String[]> func1 = String[] :: new;
String[] str = func1.apply(5);
System.out.println(Arrays.toString(str));
}