引用构造器
package LambdaTest.LambdaTest03; /** * FileName: StudentDemo * Author: lps * Date: 2022/4/5 15:34 * Sign:刘品水 Q:1944900433 */ public class StudentDemo { public static void main(String[] args) { useStudentBuilder((name, age) -> new Student(name, age)); //Lambda表达式被构造器替代的时候 他的形式参数全部传递给构造器作为参数 useStudentBuilder(Student::new); } private static void useStudentBuilder(StudentBuilder s) { Student s1 = s.build("刘品水", 21); System.out.println(s1); } }
package LambdaTest.LambdaTest03; /** * FileName: StudentBuilder * Author: lps * Date: 2022/4/5 15:33 * Sign:刘品水 Q:1944900433 */ public interface StudentBuilder { Student build(String name,int age); }
package LambdaTest.LambdaTest03; /** * FileName: Student * Author: lps * Date: 2022/4/5 15:32 * Sign:刘品水 Q:1944900433 */ public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } }