什么是方法引入

方法引入:需要结合lambda表达式能够让代码变得更加精简。
1.匿名内部类使用
2.Lambda调用匿名内部类
3.方法引入
方法引入
1.静态方法引入: 类名::(静态)方法名称
2.对象方法引入 类名:: 实例方法名称
3.实例方法引入 new对象 对象实例::方法引入
4.构造函数引入 类名::new

需要遵循一个规范:
方法引入 方法参数列表、返回类型与函数接口参数列表与返回类型必须
要保持一致。

Lambda: 匿名内部类使用代码简洁问题。

类型 语法 对应lambda表达式
构造器引用 Class::new (args) -> new 类名(args)
静态方法引用 Class::static_method (args) -> 类名.static_method(args)
对象方法引用 Class::method (inst,args) -> 类名.method(args)
实例方法引用 instance::method (args) -> instance.method(args)

方法引用提供了非常有用的语法,可以直接引用已有的java类或对象的方法或构造器。方法引用其实也离不开Lambda表达式,
与lambda联合使用 ,方法引用可以使语言的构造更加紧凑简洁,减少冗余代码。

方法引用提供非常有用的语法,可以直接引用已有的java类或者对象中方法或者构造函数,
方法引用需要配合Lambda表达式语法一起使用减少代码的冗余性问题。

静态方法引入

    public static void main(String[] args) {
//      MyFunctionalInterface myFunctionalInterface = () -> System.out.println("Hello World");
//        myFunctionalInterface.add();
        MyFunctionalInterface myFunctionalInterface = Test04::getStaticMethod;
        myFunctionalInterface.add();
    }
    /**
     * 静态方法引入
     */
    public static void getStaticMethod() {
        System.out.println("我是 getMethod");
    }

方法引入

    public static void main(String[] args) {

       MyFunctionalInterface myFunctionalInterface= Test014::add;
    }

    private static void add(String a) {
        System.out.println("Hello World");
    }

构造函数引入

    public static void main(String[] args) {
        MyFunctionalInterface aNew = UserEntity::new;

    }
    @FunctionalInterface
public interface MyFunctionalInterface {
    UserEntity userEntity();

}

对象方法引入

public class Test014 {
    public static void main(String[] args) {
//        MyFunctionalInterface myFunctionalInterface = new MyFunctionalInterface() {
//
//            @Override
//            public String userEntity(Test014 test014) {
//                return test014.getStaticMethod();
//            }
//        };
        MyFunctionalInterface myFunctionalInterface = Test014::getStaticMethod;

    }
    /**
     * 静态方法引入
     */
    public  String getStaticMethod() {
        System.out.println("我是 getMethod");
        return "我是 getMethod";
    }


}
@FunctionalInterface
public interface MyFunctionalInterface {
    String userEntity(Test014 test014);

}