0
点赞
收藏
分享

微信扫一扫

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

四月天2021 2022-03-11 阅读 235
java

方法引用

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);
}

对象引用方法

语法格式为:

例如:

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;
    }
}

构造方法引用

语法格式

无参构造器

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表达式可以引用数组类型的构造函数,语法要求为

例如:

public class Test {
    public static void main(String[] args) {
       /* 匿名内部类实现
       Action a1 = new Action() {
            @Override
            public Student 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));

    }
}

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

举报

相关推荐

0 条评论