引用构造器

复制代码
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;
    }
}
复制代码

 

posted @   刘品水  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示