使用 :: 完成对方法的引用
使用@FunctionInterface注解的函数表示接口式函数,接口式函数只能拥有一个抽象方法,可以拥有多个默认方法。
person.class
class Person {
String name;
Integer age;
public Person(){
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static Person create(String name, Integer age) {
return new Person(name, age);
}
}
personfactory.calss
@FunctionalInterface
interface PersonFactory {
Person create(String name, Integer age);
}
test.class
public class Test {
public static void main(String[] args) {
//方法的引用
PersonFactory personFactory = Person::create;
Person person = personFactory.create("xxxx", 12);
System.out.println(person);
//lambda表达式
PersonFactory per2 = (name, age) -> new Person(name,age);
Person person2 = per2.create("hell", 12);
System.out.println(person2);
}
}
标签:静态方法,name,age,接口,Person,引用,create,public,String From: https://www.cnblogs.com/huoziqi/p/17517849.html