方法引用(Method reference)

1. 对象::实例方法

被赋值的接口方法的参数列表要和方法引用相同,返回值也要相同

interface Imt<T>{
    boolean equ(T t2);
}
public class TestApplication {
    public static void main(String[] args) {
        // 等效的
        Imt<String> mt = "aa"::equals;
        System.out.println(mt.equ("ab"));

        Imt<String> imt = a -> a.equals("aa");
        System.out.println(imt.equ("aa"));
    }
}

2. 类::实例方法

被赋值的接口方法的参数列表要等于 方法引用的实例调用者+参数列表,返回值要相同
【类名::实例方法__(参数列表) == 接口.方法__(类实例,参数列表)】

interface OTK{
    // 参数列表 == 方法引用的实际调用者+方法引用的参数列表
    // 返回值和方法引用 相同
    int otk(AClass aClass,Object... objects);
}
class AClass{
    // 用作方法引用
    public int method(Object... objects){
        return objects.length;
    }

    // psvm主函数
    public static void main(String[] args) {
        OTK otk = AClass::method;
        int otk1 = otk.otk(new AClass(), new Object(), new Object());
        System.out.println(otk1);
    }
}

3. 类::静态方法

被赋值的接口方法的参数列表要和方法引用相同,返回值也要相同

interface Imt<T>{
    boolean equ(T t2, T t1);
}
public class TestApplication {
    // 用作方法引用
    public static <T> boolean index(T t1, T t2){
        return t1.equals(t2);
    }

    // psvm主函数
    public static void main(String[] args) {
        Imt<Integer> imt = TestApplication::index;
        System.out.println(imt.equ(12, 12));
    }
}

4. Class::new

【Class::new__(参数列表) == 类 method__(参数列表)】

// 实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
class Apple{
    private Double price;
    private Integer count;

    public Apple(Double aDouble) {
        price = aDouble;
    }
}

// 自定义接口,处理接收三个参数类型  ,最后一个参数类型用作方法的返回值
interface CustomFunction<T,E,R>{
    R poll(T t,E e);
}

public class TestApplication {
    public static void main(String[] args) {
        Function<Double,Apple> aNew = Apple::new;
        Apple apple = aNew.apply(12.0D);    // 等效于 Apple apple = new Apple(12.0D);

        Supplier<Apple> bNew = Apple::new;
        Apple apple1 = bNew.get();  // 等效于 Apple apple1 = new Apple();

        CustomFunction<Double,Integer,Apple> cNew = Apple::new;
        Apple apple2 = cNew.poll(12.0D, 15);    // 等效于 Apple apple2 = new Apple(12.0D,15);
    }
}

5. Class[]::new

// 实体类
@Data
class Apple{
    private Double price;
    private Integer count;
}

// 自定义接口,用于接收创建数组的方法引用,参数一定要是一个int型(创建数组的大小)
interface Custom<T>{
    T get(int x);
}

public class TestApplication {
    public static void main(String[] args) {
        Custom<Apple[]> custom = Apple[]::new;
        Apple[] apples = custom.get(5);    // 等效于 Apple[] apples = new Apple[5];
    }
}
————————

1. Object: instance method

The parameter list of the assigned interface method shall be the same as the method reference, and the return value shall also be the same

interface Imt<T>{
    boolean equ(T t2);
}
public class TestApplication {
    public static void main(String[] args) {
        // 等效的
        Imt<String> mt = "aa"::equals;
        System.out.println(mt.equ("ab"));

        Imt<String> imt = a -> a.equals("aa");
        System.out.println(imt.equ("aa"));
    }
}

2. Class: instance method

The parameter list of the assigned interface method shall be equal to the instance caller + parameter list referenced by the method, and the return value shall be the same
[class name: instance method _ (parameter list) = = interface. Method _ (class instance, parameter list)]

interface OTK{
    // 参数列表 == 方法引用的实际调用者+方法引用的参数列表
    // 返回值和方法引用 相同
    int otk(AClass aClass,Object... objects);
}
class AClass{
    // 用作方法引用
    public int method(Object... objects){
        return objects.length;
    }

    // psvm主函数
    public static void main(String[] args) {
        OTK otk = AClass::method;
        int otk1 = otk.otk(new AClass(), new Object(), new Object());
        System.out.println(otk1);
    }
}

3. Class: static method

The parameter list of the assigned interface method shall be the same as the method reference, and the return value shall also be the same

interface Imt<T>{
    boolean equ(T t2, T t1);
}
public class TestApplication {
    // 用作方法引用
    public static <T> boolean index(T t1, T t2){
        return t1.equals(t2);
    }

    // psvm主函数
    public static void main(String[] args) {
        Imt<Integer> imt = TestApplication::index;
        System.out.println(imt.equ(12, 12));
    }
}

4. Class::new

[class:: new_ (parameter list) = = class method_ (parameter list)]

// 实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
class Apple{
    private Double price;
    private Integer count;

    public Apple(Double aDouble) {
        price = aDouble;
    }
}

// 自定义接口,处理接收三个参数类型  ,最后一个参数类型用作方法的返回值
interface CustomFunction<T,E,R>{
    R poll(T t,E e);
}

public class TestApplication {
    public static void main(String[] args) {
        Function<Double,Apple> aNew = Apple::new;
        Apple apple = aNew.apply(12.0D);    // 等效于 Apple apple = new Apple(12.0D);

        Supplier<Apple> bNew = Apple::new;
        Apple apple1 = bNew.get();  // 等效于 Apple apple1 = new Apple();

        CustomFunction<Double,Integer,Apple> cNew = Apple::new;
        Apple apple2 = cNew.poll(12.0D, 15);    // 等效于 Apple apple2 = new Apple(12.0D,15);
    }
}

5. Class[]::new

// 实体类
@Data
class Apple{
    private Double price;
    private Integer count;
}

// 自定义接口,用于接收创建数组的方法引用,参数一定要是一个int型(创建数组的大小)
interface Custom<T>{
    T get(int x);
}

public class TestApplication {
    public static void main(String[] args) {
        Custom<Apple[]> custom = Apple[]::new;
        Apple[] apples = custom.get(5);    // 等效于 Apple[] apples = new Apple[5];
    }
}