构造器引用和数组引用的使用
* 一、构造器引用
* 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。
* 抽象方法的返回值类型即为构造器所属的类的类型
*
* 二、数组引用
* 可以把数组看做一个特殊的类,则写法与构造器引用一致。
*
*
* Created by 阿昌
public class ConstructorRefTest {
@Test
public void test1(){
Supplier
<Employee> sup0
= new Supplier<Employee>() {
@Override
public Employee
get() {
return new Employee();
}
};
System
.out
.println(sup0
.get());
System
.out
.println("==========================================");
Supplier
<Employee> sup1
= () -> new Employee();
System
.out
.println(sup1
.get());
System
.out
.println("==========================================");
Supplier
<Employee> sup2
= Employee
::new;
System
.out
.println(sup2
.get());
}
@Test
public void test2(){
Function
<Integer,Employee> fun1
= id
-> new Employee(id
);
Employee employee
= fun1
.apply(1001);
System
.out
.println(employee
);
System
.out
.println("==========================================");
Function
<Integer,Employee> fun2
= Employee
::new;
Employee employee1
= fun2
.apply(1002);
System
.out
.println(employee1
);
}
@Test
public void test3(){
BiFunction
<Integer,String,Employee> bif1
= (id
,str
) -> new Employee(id
,str
);
Employee employee
= bif1
.apply(1001, "张薇");
System
.out
.println(employee
);
System
.out
.println("==========================================");
BiFunction
<Integer,String,Employee> bif2
= Employee
::new;
Employee employee1
= bif2
.apply(1002, "刘芳");
System
.out
.println(employee1
);
}
@Test
public void test4(){
Function
<Integer
,String
[]> func1
= length
-> new String[length
];
String
[] arr1
= func1
.apply(8);
System
.out
.println(Arrays
.toString(arr1
));
System
.out
.println("==========================================");
Function
<Integer
,String
[]> func2
= String
[] :: new;
String
[] arr2
= func2
.apply(5);
System
.out
.println(Arrays
.toString(arr2
));
}
}
转载请注明原文地址: https://lol.8miu.com/read-9215.html