方法引用——引用构造器

定义接口

public interface StudentBuilder {
    Student build(String name, int age);
}
View Code

测试类

public class StudentDemo {
    public static void main(String[] args) {
        //使用Lambda表达式
        useStudentBuilder((name,age)->new Student(name,age));

        //引用构造器
        useStudentBuilder(Student::new);
    }
    private static void useStudentBuilder(StudentBuilder student){
        Student s1 = student.build("林青霞", 30);
        System.out.println(s1.getName()+","+s1.getAge());
    }
}
View Code

运行结果:

Lambda表达式被构造器代替的时候,它的形式参数全部传递给构造器作为参数

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/12918435.html