构造器的引用
构造器的引用 ClassName :: new
// 11.使用匿名内部类的方式通过Supplier函数式接口创建Person类型的对象并返回
Supplier<Person> supplier3 = new Supplier<Person>() {
@Override
public Person get() {
return new Person();
}
};
System.out.println(supplier3.get()); // null 0
Supplier<Person> supplier4 = () -> new Person();
System.out.println(supplier4.get()); // null 0
Supplier<Person> supplier5 = Person::new;
System.out.println(supplier5.get()); // null 0
-
这里我们先来看一下无参构造器的使用,因为是无参有返回值类型,所以我们使用Supplier函数式接口
-
Supplier supplier5 = Person::new;
// 12.使用匿名内部类的方式通过BiFunction函数式接口采用有参方式创建Person类型的对象并返回 BiFunction<String, Integer, Person> biFunction = new BiFunction<String, Integer, Person>() { @Override public Person apply(String s, Integer integer) { return new Person(s, integer); } }; System.out.println(biFunction.apply("zhangfei", 30)); // zhangfei 30 BiFunction<String, Integer, Person> biFunction1 = (s, integer) -> new Person(s, integer); System.out.println(biFunction1.apply("zhangfei", 30)); // zhangfei 30 BiFunction<String, Integer, Person> biFunction2 = Person::new; System.out.println(biFunction2.apply("zhangfei", 30)); // zhangfei 30
- 有参有返回值类型,我们使用Function函数式接口,传入两个参数类型,String和Integer,返回类型为Person