栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
       
        //引用的非静态方法中有一个参数
        Index i1 = String::indexOf;
       
        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) {

        
        //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) {
       
        
        //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) {
       

        //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) {
       

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号