您现在的位置是:首页 > 编程 > 

Java方法引用详解

2025-07-27 06:52:17
Java方法引用详解 一、方法引用1、目的简化Lambda表达式;2、简单演示函数式接口:代码语言:javascript代码运行次数:0运行复制package study.method_references; @FunctionalInterface public interface myPrint { void print(String s); }测试类:代码语言:javascript代

Java方法引用详解

一、方法引用

1、目的

简化Lambda表达式;

2、简单演示

函数式接口:
代码语言:javascript代码运行次数:0运行复制
package _references;
@FunctionalInterface
public interface myPrint {
    void print(String s);
}
测试类:
代码语言:javascript代码运行次数:0运行复制
package _references;

public class Test {
    public static void main(String[] args) {
        String s = "打印我哦!";
        print(::println,s);//打印我哦!
    }
    private static void print(myPrint myPrint,String s){
        myPrint.print(s);
    }
}

、两种写法

Lambda表达式写法:
代码语言:javascript代码运行次数:0运行复制
s -> .println(s);
方法引用写法:
代码语言:javascript代码运行次数:0运行复制
::println;
效果完全一致;
二、通过对象名引用成员方法

代码演示:

函数式接口myPrint:
代码语言:javascript代码运行次数:0运行复制
package _references;
@FunctionalInterface
public interface myPrint {
    void print(String s);
}
实体类TestA:
代码语言:javascript代码运行次数:0运行复制
package _references;

public class TestA {
    public void printA(String s){
        .println(s);
    }
}
测试类Test:
代码语言:javascript代码运行次数:0运行复制
package _references;

public class Test {
    public static void main(String[] args) {
        //一般写法
        String string = "打印我哦!";
        print((s)->{
            TestA testA = new TestA();
            testA.printA(s);//打印我哦!
        },string);
        //通过对象名引用方法
        TestA testB = new TestA();
        print(testB::printA,string);//打印我哦!
    }
    private static void print(myPrint myPrint,String s){
        myPrint.print(s);
    }
}
三、通过类型引用静态方法

函数式接口myPrint:

代码语言:javascript代码运行次数:0运行复制
package _references;
@FunctionalInterface
public interface myPrint {
    void print(String s);
}

实体类TestB:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class TestB {
    public static void printB(String s){
        .println(s);
    }
}

测试类Test:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class Test {
    public static void main(String[] args) {
        //一般写法
        String string = "打印我哦!";
        print((s)->{
            TestB.printB(s);//打印我哦!
        },string);
        //通过类引用静态方法
        print(TestB::printB,string);//打印我哦!
    }
    private static void print(myPrint myPrint,String s){
        myPrint.print(s);
    }
}
四、使用super引用父类的成员方法

父类:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class Fu {
    public void say(){
        .println("父类的say");
    }
}

接口:

代码语言:javascript代码运行次数:0运行复制
package _references;

public interface myMethod {
    void doSth();
}

子类兼测试类:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class Zi extends Fu {
    @Override
    public void say(){
        .println("子类的say");
    }
    public static void main(String[] args) {
        new Zi().show();//父类的say

    }
    private static void myDO(myMethod myMethod){
        myMethod.doSth();
    }
    private void show(){
        myDO(super::say);
    }
}
五、使用this引用本类成员变量

将【四】中的super改成this即可;

六、类的构造器(构造方法)的引用

Person类:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class Person {
    private String name;
    public Person(String name){
        this.setame(name);
    }
    public Person(){
        super();
    }

    public String getame() {
        return name;
    }

    public void setame(String name) {
         = name;
    }
}

函数式接口PersonBuilder:

代码语言:javascript代码运行次数:0运行复制
package _references;
@FunctionalInterface
public interface PersonBuilder {
    Person buildPerson(String name);
}

测试类Main:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class Main {
    public static void main(String[] args) {
        printame("大哥",Person::new);//带参构造
    }
    private static void printame(String name, PersonBuilder pb){
        Person person = pb.buildPerson(name);
        .println(person.getame());
    }
}
七、数组的构造器引用

函数式接口:

代码语言:javascript代码运行次数:0运行复制
package _references;
@FunctionalInterface
public interface ArrayBuilder {
    int[] buildArray(int len);
}

测试类:

代码语言:javascript代码运行次数:0运行复制
package _references;

public class ArrayBuilderTest {
    public static void main(String[] args) {
        goOn((len)->{
            //传统写法
            return new int[len];
            },10);
        //引用写法
        goOn(int[]::new,10);
    }
    private static void goOn(ArrayBuilder arrayBuilder,int len){
        int[] arr = arrayBuilder.buildArray(len);
        .println(arr.length);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent 删除函数接口javapublic测试

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1200269.html

相关标签:无
上传时间: 2025-07-23 18:25:00
留言与评论(共有 12 条评论)
本站网友 你最红
11分钟前 发表
Person
本站网友 埃博拉病人融化图片
30分钟前 发表
new
本站网友 隐私法
18分钟前 发表
printB
本站网友 客户端开发
21分钟前 发表
10); //引用写法 goOn(int[]
本站网友 常熟租房
23分钟前 发表
String s){ myPrint.print(s); } }
本站网友 郁芳狐臭净
17分钟前 发表
原始发表:2025-01-06
本站网友 投资资本回报率
24分钟前 发表
int len){ int[] arr = arrayBuilder.buildArray(len); .println(arr.length); } }本文参与 腾讯云自媒体同步曝光计划
本站网友 伊利集团邮件系统
6分钟前 发表
String s){ myPrint.print(s); } }四
本站网友 readprocessmemory
17分钟前 发表
s);//打印我哦! } private static void print(myPrint myPrint
本站网友 西安美食排名
28分钟前 发表
10); //引用写法 goOn(int[]
本站网友 子在川上曰逝者如斯夫
5分钟前 发表