定义接口
public interface StudentBuilder { Student build(String name, int age); }
测试类
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()); } }
运行结果:
Lambda表达式被构造器代替的时候,它的形式参数全部传递给构造器作为参数