Lambda_05 函数表达式引用构造方法

声明接口,接口作为对象的生成器

声明接口

@FunctionalInterface
interface PersonInterface1 {
    public Person interfaceFun();
}

@FunctionalInterface
interface PersonInterface2 {
    public Person interfaceFun(String name);
}

@FunctionalInterface
interface PersonInterface3 {
    public Person interfaceFun(String name, Integer age);
}

调用函数式接口,匿名函数调用。

public static void main(String[] args) {
        //函数式接口 实现无参数构造方法 方法1
        PersonInterface1 intf0 = () -> {
            return new Person();
        };
        intf0.interfaceFun();

        //函数式接口 实现无参数构造方法 方法2 (方法1的简写)
        PersonInterface1 intf1 = () -> new Person();
        intf1.interfaceFun();
		//函数式接口实现一个参构造方法
        PersonInterface1 intf2 = () -> new Person("小明");
        intf2.interfaceFun();
		//函数式接口实现多个参构造方法
        PersonInterface1 intf3 = () -> new Person("小明",16);
        intf3.interfaceFun();
}

创建对象的形式 类名::new

interfaceName intf = class::new;
intf.fun(); 根据接口入参分别实现不同的构造方法。

public static void main(String[] args) {
       //使用 class::new 实现
       PersonInterface1 intf4 = Person::new;
       intf4.interfaceFun();

       PersonInterface2 intf5 = Person::new;
       intf5.interfaceFun("小武");

       PersonInterface3 intf6 = Person::new;
       intf6.interfaceFun("小帅哥",18);
}

调用接口的方法可以返回该对象的实例

代码展示

public class Lambda6 {
    public static void main(String[] args) {
        //函数式接口 实现无参数构造方法 方法1
        PersonInterface1 intf0 = () -> {
            return new Person();
        };
        intf0.interfaceFun();

        //函数式接口 实现无参数构造方法 方法2 (方法1的简写)
        PersonInterface1 intf1 = () -> new Person();
        intf1.interfaceFun();

        PersonInterface1 intf2 = () -> new Person("小明");
        intf2.interfaceFun();

        PersonInterface1 intf3 = () -> new Person("小明",16);
        intf3.interfaceFun();

        //使用 class::new 实现
        PersonInterface1 intf4 = Person::new;
        intf4.interfaceFun();

        PersonInterface2 intf5 = Person::new;
        intf5.interfaceFun("小武");

        PersonInterface3 intf6 = Person::new;
        intf6.interfaceFun("小帅哥",18);


    }
}

@FunctionalInterface
interface PersonInterface1 {
    public Person interfaceFun();
}

@FunctionalInterface
interface PersonInterface2 {
    public Person interfaceFun(String name);
}

@FunctionalInterface
interface PersonInterface3 {
    public Person interfaceFun(String name, Integer age);
}

class Person {
    private String name;
    private Integer age;

    Person() {
        System.out.println("无参数构造函数");
    }

    public Person(String name) {

        this.name = name;
        System.out.println("一个参数构造函数,name:" + name);
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("有二个参数构造函数,name:" + name + ",age:" + age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineerForSoul

你的鼓励是我孜孜不倦的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值