package com.example.demo.java8;
import org.junit.Test;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 构造器引用
*
* 抽象方法的形参列表和 构造器的形参列表一致
* 抽象方法的返回值为构造器的类型
*
*/
public class ConstructorReference {
@Test
public void test1(){
Supplier<Employee> s1 = () -> new Employee(12,"威威",78d);
System.out.println(s1.get());
//空参构造器
Supplier<Employee> s2 = Employee::new;
System.out.println(s2.get());
}
@Test
public void test2(){
Function<Integer,Employee> fun1 = id -> new Employee(id);
System.out.println(fun1.apply(789));
Function<Integer,Employee> fun2 = Employee::new;
System.out.println(fun2.apply(789));
}
@Test
public void test3(){
BiFunction<Integer,String,Employee> bi1 = (id,name) -> new Employee(id,name);
System.out.println(bi1.apply(12, "单点的"));
BiFunction<Integer,String,Employee> bi2 = Employee::new;
System.out.println(bi2.apply(12, "单点的"));
}
}
java8之构造器引用简单例子
最新推荐文章于 2024-03-05 19:41:25 发布