方法引入

package com.mayikt.service;

/**
 * @ClassName MessageInterface
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
@FunctionalInterface
public interface MessageInterface {
    String get(Integer a, Integer b);
}

package com.mayikt.method;

import com.mayikt.service.MessageInterface;

/**
 * @ClassName Test012
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test012 {
    public static void main(String[] args) {
        // 1.最原生匿名内部类调用方式
//        MessageInterface messageInterface = new MessageInterface() {
//            @Override
//            public void get(Integer a) {
//                System.out.println("geta:" + a);
//            }
//        };
//        messageInterface.get(1);

        // 在Lambda表达式中方法体直接引入方法
//        MessageInterface messageInterface2 = (a) -> Test012.staticGet(a);
//        // 方法引入 --实际上就是我们 Lambda表达式中方法体直接引入方法---
//        messageInterface2.get(2);
//        // 方法引入写法
//        MessageInterface messageInterface3 = Test012::staticGet;
//        messageInterface3.get(10,20);
        /**
         *Test012::staticGet为何能够等于
         * (a) -> Test012.staticGet(a);
         */
    }

    public static String  staticGet(Integer a, Integer b) {
        System.out.println("staticGet,a:" + a);
        return "123456";
    }
}

package com.mayikt.method;

import com.mayikt.service.MessageInterface;

/**
 * @ClassName Test013
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test013 {
    public static void main(String[] args) {
        Test013 test013 = new Test013();
//        MessageInterface messageInterface = new MessageInterface() {
//
//            @Override
//            public String get(Integer a, Integer b) {
//                return test013.objGet(a, b);
//            }
//        };
//
//        String s = messageInterface.get(1, 2);
//        System.out.println(s);

//        MessageInterface messageInterface2 = (a, b) -> test013.objGet(a, b);
        MessageInterface MessageInterface3 = test013::objGet;
        String s = MessageInterface3.get(1, 2);
        System.out.println(s);
    }

    public String objGet(Integer a, Integer b) {
        System.out.println("objGet,a:" + a + ",b:" + b);
        return a + b + "";
    }
}
原文地址:https://www.cnblogs.com/angdh/p/15596553.html