方法引用(::,Lambda表达式的孪生兄弟)

Java方法引用详解与示例
本文介绍了Java中的方法引用,作为Lambda表达式的孪生兄弟,详细解释了方法引用符`::`的用法,包括引用类方法、对象实例方法、类的实例方法和构造器。通过示例代码展示了不同引用方式的应用,强调了方法引用的推导与省略特性,同时指出方法引用并非能处理所有Lambda表达式能处理的情况。

什么是方法引用呢,我们先看一个例子(了解这个之前,得先了解Lambda表达式,不清楚Lambda表达式的可以看这个

Interface1:接口1,里面有一个抽象方法mathod

Demo:测试类,体验方法引用

Interface1:接口1的代码

package com.testMethodReference;

/**
 * @author 林高禄
 * @create 2020-06-03-9:33
 */
public interface Interface {
    int method(int a,int b);
}

Demo:测试类代码

package com.testMethodReference;

/**
 * @author 林高禄
 * @create 2020-06-03-9:34
 */
public class Demo {
    public static void main(String[] args) {
        // Lambda表达式
        // 提示,Lambda can be replaced with method reference more... (Ctrl+F1),Lambda表达式可用方法引用代替
        useInterface((a,b)-> Math.max(a,b));
        // 方法引用
        useInterface(Math::max);
    }
    private static void useInterface(Interface i){
        int method = i.method(3, 5);
        System.out.println(method);
    }
}

运行测试Demo,输出:

5
5

方法引用符::

  • ::该符号为引用运算符,而它所在的表达式被称为方法引用
  • 方法的引用基于Lambda表达式

推导与省略

  • 如果使用Lambda,那么根据“可推导就是可省略”的原则,无需指定参数类型,也无需指定重载形式,它们都将被自动推导
  • 如果使用方法引用,可是可以根据上下文进行推导的
  • 方法引用就是Lambda表达式的孪生兄弟

方法引用常见的引用方式:

  • 引用类方法
  • 引用对象的实例方法
  • 引用类的实例方法
  • 引用构造器:格式:类名::new

代码,为了方便,所有的类都写在了一个文件

  • Demo2:测试类
  • Converter:测试引用类方法的接口
  • Interface1:测试引用对象的实例方法的接口
  • MyClass:测试引用对象的实例方法的类
  • Interface2:测试引用类的实例方法的接口
  • Interface3:测试引用类的实例方法的接口
  • StudentInterface:测试引用构造器的接口
  • Student:测试引用构造器的类
package com.testMethodReference;

/**
 * @author 林高禄
 * @create 2020-06-03-10:00
 */
public class Demo2 {
    public static void main(String[] args) {
        System.out.println("-----引用类方法-----");
        // Lambda表达式
        useConverter(s->Integer.parseInt(s));
        // 方法引用--引用类方法
        // Lambda表达式被类的方法替代的时候,它的形式参数全部传递给静态方法作为参数
        useConverter(Integer::parseInt);
        System.out.println("-----引用对象的实例方法-----");
        // Lambda表达式
        useInterface1(s-> System.out.println(s.toUpperCase()));
        // 方法引用--引用对象的实例方法
        // Lambda表达式被对象的实例方法替代的时候,它的形式参数全部传递给该方法作为参数
        MyClass my = new MyClass();
        useInterface1(my::printUpper);
        System.out.println("-----引用类的实例方法-----");
        // Lambda表达式
        useInterface2((x,s,y)->s.substring(x,y));
        // 方法引用--引用类的实例方法
        // 报错,Cannot resolve method 'substring',无法解析方法“substring”
        //useInterface2(String::substring);
        // Lambda表达式
        useInterface3((s,x,y)->s.substring(x,y));
        // 方法引用--引用类的实例方法
        // Lambda表达式被类的实例方法替代的时候,第一个参数作为调用者,后面的参数全部传递给该方法作为参数
        useInterface3(String::substring);
        System.out.println("-----引用构造器-----");
        // Lambda表达式
        useStudentInterface((name,age)->new Student(name,age));
        // 方法引用--引用构造器
        // Lambda表达式被构造器替代的时候,它的形式参数全部传递给构造器作为参数
        useStudentInterface(Student::new);
    }

    private static void useConverter(Converter c){
        int convert = c.convert("110");
        System.out.println(convert);
    }
    private static void useInterface1(Interface1 i){
            i.printUpper("LinGaoLu");
    }
    private static void useInterface2(Interface2 i){
        String s = i.mySubString(3,"LinGaoLu",6);
        System.out.println(s);
    }
    private static void useInterface3(Interface3 i){
        String s = i.mySubString("LinGaoLu",3,6);
        System.out.println(s);
    }
    private static void useStudentInterface(StudentInterface s){
        Student student = s.studentBuild("林高禄", 27);
        System.out.println(student.toString());
    }
}

interface Converter{
    int convert(String s);
}
class MyClass{
    public void printUpper(String s){
        System.out.println(s.toUpperCase());
    }
}
interface Interface1{
    void printUpper(String s);
}
interface Interface2{
    String mySubString(int a,String s,int b);
}
interface Interface3{
    String mySubString(String s,int a,int b);
}
interface StudentInterface{
    Student studentBuild(String name,int age);
}
class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = 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 +
                '}';
    }
}

运行,输出

-----引用类方法-----
110
110
-----引用对象的实例方法-----
LINGAOLU
LINGAOLU
-----引用类的实例方法-----
Gao
Gao
Gao
-----引用构造器-----
Student{name='林高禄', age=27}
Student{name='林高禄', age=27}

到此,方法引用的讲解完毕,虽然方法引用很简洁,并且是Lambda表达式的孪生兄弟,不过Lambda表达式能处理的额,方法的引用不一定能处理,比如例子中的useInterface2就是很好的证明,这个和useInterface3不同的就是把参数调换一下位置就不行了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林高禄

你打不打赏,我都会一直写博客

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

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

打赏作者

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

抵扣说明:

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

余额充值