首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Lambda表达式(四)方法引入

Lambda表达式(四)方法引入

作者头像
高大北
发布2022-06-14 20:24:47
发布2022-06-14 20:24:47
20300
代码可运行
举报
运行总次数:0
代码可运行

方法引入:

什么是方法引入?

方法引入:需要结合lambda表达式能够让代码变得更加精简。

  1. 匿名内部类使用
  2. Lambda调用匿名内部类
  3. 方法引入

方法引入的四大分类

  1. 静态方法引入 类名::(静态)方法名称
  2. 对象方法引入 类名:: 实例方法名称
  3. 实例方法引入 new对象 对象实例::方法引入
  4. 构造函数引入 类名::new

1. 静态方法引入 类名::(静态)方法名称

代码语言:javascript
代码运行次数:0
运行
复制
  public static void main(String[] args) {
        //静态方法引入
        TestInterface testInterface=Test017::getName;
        System.out.println(testInterface.getName("静态方法引入"));
    }

    public static String getName(String name){
        return name;
    }
代码语言:javascript
代码运行次数:0
运行
复制
@FunctionalInterface
public interface TestInterface {

    String getName(String name);
}
代码语言:javascript
代码运行次数:0
运行
复制
注意点:静态方法的参数和返回值需要与函数接口的方法参数保持一致

2. 对象方法引入 类名:: 实例方法名称

代码语言:javascript
代码运行次数:0
运行
复制
public class Test017 {

    public static void main(String[] args) {
        //对象方法引入引入
        TestInterface testInterface=Test017::getName;
        System.out.println(testInterface.getName(new Test017()));
    }

    public String getName(){
        return "对象方法引入";
    }
}

函数接口

代码语言:javascript
代码运行次数:0
运行
复制
@FunctionalInterface
public interface TestInterface {

    String getName(Test017 test017);

}

​ 注意点:函数接口的参数是对象,函数接口和调用目标方法返回值一致,参数不一致

3. 实例方法引入 new对象 对象实例::方法引

代码语言:javascript
代码运行次数:0
运行
复制
public class Test017 {

    public static void main(String[] args) {
        //实例方法引入
        Test017 test017 = new Test017();
        TestInterface testInterface=test017::getName;
        System.out.println(testInterface.getName("实例方法引入"));
    }

    public String getName(String name){
        return name;
    }
}

函数接口:

代码语言:javascript
代码运行次数:0
运行
复制
@FunctionalInterface
public interface TestInterface {

    String getName(String name);
}

注意点:函数接口返回值和参数和方法保持一致

4. 构造函数引入 类名::new

代码语言:javascript
代码运行次数:0
运行
复制
public class Test017 {

    public static void main(String[] args) {
        //构造函数方法引入
        TestInterface testInterface=UserEntity::new;
        System.out.println(testInterface.getName("构造函数方法引入"));
    }

}

函数接口:

代码语言:javascript
代码运行次数:0
运行
复制
@FunctionalInterface
public interface TestInterface {

    UserEntity getName(String name);

}

实体:

代码语言:javascript
代码运行次数:0
运行
复制
public class UserEntity {
    private String userName;
    private int age;

    public UserEntity() {

    }

    public UserEntity(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public UserEntity(String userName) {
        this.userName = userName;
    }

    public UserEntity(int age) {
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
    
}

注意点:构造方法的参数要和函数接口的参数保持一致,函数接口的多返回值和构造函数一致

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方法引入:
    • 什么是方法引入?
    • 方法引入的四大分类
    • 1. 静态方法引入 类名::(静态)方法名称
    • 2. 对象方法引入 类名:: 实例方法名称
    • 3. 实例方法引入 new对象 对象实例::方法引
    • 4. 构造函数引入 类名::new
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档