posts - 397,comments - 0,views - 25332

this指代当前对象,如果需要引用的方法就是本类当中的成员方法,那么可以使用***this::成员方法***格式来优化Lambda表达式

实现:

@FunctionlInterface
public intterface Study {
    // 定义一个学习抽象方法
    void study();
}
复制代码
public class Husband {

    /**
     * 通过this引用本类方法
     */
    public void buyHouse(){
        System.out.println("北京二环内卖一套四合院");
    }

    public void marry(Richable r){
        r.buy();
    }

    public void soHappy(){
        marry(()->{
            this.buyHouse();
        });
    }

    public static void main(String[] args) {

        new Husband().soHappy();

    }

}
复制代码

 

 

 

 

 

 

 

 

方法引用_类的构造器(构造方法)引用

代码:

复制代码
public class Person {

    private String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
复制代码
@FunctionalInterface
public interface  PersonBuilder {

    Person builderPerson(String name);
复制代码
 public static void printName(String name,PersonBuilder pb){
        Person person = pb.builderPerson(name);
        System.out.println(person.getName());
    }

    public static void main(String[] args) {

        printName("迪丽热巴",(String name)->{
            return new Person(name);
        });
        /*
            使用方法引用优化Lambda表达式
            构造方法


         */
        printName("古力娜扎",Person::new);
    }
复制代码

 

posted on   淤泥不染  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示