java进阶-JDK1.8之后新特性:方法引用

方法引用

Lambda表达式中,提供了特殊的语法,能让我们直接引用已经存在的方法,作为当前要表示的函数,例如

public class Test {
    public static void main(String[] args) {
        //通过lambda表达式表示一个函数,这个函数作为run方法的具体实现
        Action a1 = () -> System.out.println("hello!");

        //方法引用:被引用的Student类中的sayHello方法作为run方法的具体实现
        Action a2 = Student::sayHello;

        //调用接口中的run()方法
        a1.run();
        a2.run();

    }
}

interface Action{
    void run();
}

class Student{
    public static void sayHello(){
        System.out.println("hello!");
    }
}

方法引用和lambda表达式一样,需要保证接口时函数式接口才可以使用。

静态方法引用

使用Lambda表达式可以引用类中的静态方法,语法要求为:

类名::静态方法名
注意:方法名字后面一定没有小括号,因为这不是在调用方法,而是在引用这个方法的定义

例如:

public class Test {
    public static void main(String[] args) {
        //Lambda表达式中只要参数是String类型,返回值是int类型就可以作为Action接口的具体实现
        Action a1 = str -> str.length();

        //使用静态方法引用(类名::静态方法名)来引用当前类中的静态方法len
        Action a2 = Test::len;
        
        System.out.println(a1.run("hello")); //输出5
        System.out.println(a2.run("hello")); //输出5
    }

    public static int len(String str){
        return str.length();
    }
}

interface Action{
    int run(String str);
}

实例方法引用

使用Lambda表达式可以引用类中的非静态方法,语法如下:

类名::非静态方法名

例如:

public class Test {
    public static void main(String[] args) {

        //引用的非静态方法中没有参数
        Action a1 = String::length;
       /* 等价于:
         Action a1 = new Action() {
            @Override
            public int run(String str) {
                return str.length();
            }
        };
        */
        //引用的非静态方法中有一个参数
        Index i1 = String::indexOf;
       /* 相当于:
        Index i1 = new Index(){
            @Override
            public int run(String str, String s) {
                return str.indexOf(s);
            }
        };
        */
        System.out.println(a1.run("hello")); //输出5
        System.out.println(i1.run("hello","e")); //输出1

    }
}

interface Action{
    //该方法中只有一个参数
    int run(String str);
}
interface Index{
    //该方法中有两个参数
    int run(String str, String s);
}

补充:
(1)对于int run(String str)方法来讲,可以直接引用String类中的length()方法来表示对run()方法的实现,需要注意的是run方法中有一个参数,而length方法是没有参数的,run方法中的参数是到时要调用方法的对象,相当于
public int run(String str){ return str.xxx(); }
(2)对于int run(String str,String s)来讲,可以直接引用String类中的inidexOf(int ch)方法来表示对run方法的实现,需要注意该run方法中有两个参数,且indexOf方法中也有一个参数,可以理解为run方法中的第一个参数是到时要调用方法的对象,后一个参数是传入到indexOf方法中的参数。相当于
public int run(String str, String i) { return str.xxx(i); }
-----当被调用的方法中参数变多,那么同样的,接口中方法的参数也要变多,且第一个参数为想要调用该方法的那个对象。

对象引用方法

语法格式为:

对象::非静态方法

例如:

public class Test {
    public static void main(String[] args) {

        /* 匿名内部类实现
        Action a1 = new Action() {
            @Override
            public String run(String str) {
                return "ByeBye! " + str;
            }
        };
        */
        //Lambda表达式简化
        Action a1 = str -> "ByeBye! " + str;

        //对象引用方法
        MyHandler myHandler = new MyHandler();
        Action a2 = myHandler::test;
         
        System.out.println(a1.run("Tom"));;
        System.out.println(a2.run("John"));
    }
}

interface Action{
    String run(String str);
}
class MyHandler{
    public String test(String name){
        return "hello! " + name;
    }
}

其实这时候,任何一个对象的中的方法,只要是参数列表和返回类型和run方法保持一致,都可以使用这个对象中的这个方法,来对Action接口进行实现。

构造方法引用

语法格式

类名::new

无参构造器

public class Test {
    public static void main(String[] args) {
       /* 匿名内部类实现
       Action a1 = new Action() {
            @Override
            public Student run() {
                return new Student();
            }
        };
        */
        
        //Lambda表达式简化
        //Action a1 = ()->new Student();
        
        //使用Student类的无参构造函数对Action接口进行实现
        Action a1 = Student::new;
        System.out.println(a1.run());

    }
}

interface Action{
    //返回值是Student类型
    Student run();
}
class Student{

}

含参构造器

public class Test {
    public static void main(String[] args) {
       /* 匿名内部类实现
       Action a1 = new Action() {
            @Override
            public Student run(String name) {
                return new Student(name);
            }
        };
        */

        //Lambda表达式简化
        //Action a1 = name->new Student(name);

        //使用Student类的含参构造器对Action接口进行实现
        Action a1 = Student::new;
        System.out.println(a1.run("tom"));
		//输出的是student对象中的name属性值tom所在的位置
    }
}

interface Action{
    //返回值是Student类型
    Student run(String name);
}
class Student{
    private String name;
    //含参构造器
    public Student(String name){
        this.name = name;
    }
}

数组构造

使用lambda表达式可以引用数组类型的构造函数,语法要求为

数组类型::new

例如:

public class Test {
    public static void main(String[] args) {
       /* 匿名内部类实现
       Action a1 = new Action() {
            @Override
            public int[] run(int len) {
                return new int[len];
            }
        };
        */

        //Lambda表达式简化
        //Action a1 = len->new int(len);

        //使用int数组的构造函数,来对Action接口进行实现
        //int数组的构造函数恰好是和run方法的参数列表和返回类型保持一致,所以可引用
        Action a1 = int[]::new;
        System.out.println(a1.run(5)); //输出的是数组所在的内存地址
        System.out.println(Arrays.toString(a1.run(5)); //输出的是数组值,因为没有传值,默认值都是0,即[0, 0, 0, 0,0]
    }
}

interface Action{
    //返回值是数组类型
   int[] run(int len);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值