方法引用——引用构造器

定义接口

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表达式被构造器代替的时候,它的形式参数全部传递给构造器作为参数

posted @ 2020-05-19 17:53  硬盘红了  阅读(128)  评论(0编辑  收藏  举报