Java 自定义函数接口作为方法参数

Java 自定义函数接口作为方法参数,java,functional-interface,Java,Functional Interface,下面的代码确实产生了预期的结果。有人能解释一下窗帘后面发生了什么吗?我不明白编译器/JVM如何知道需要调用Storer对象上的store(String str),或者它如何定义doSomething(Storer s,String str)实现 Storer.java public class Storer { private List<String> storer = new ArrayList<>(); public void store(Strin

下面的代码确实产生了预期的结果。有人能解释一下窗帘后面发生了什么吗?我不明白编译器/JVM如何知道需要调用Storer对象上的store(String str),或者它如何定义doSomething(Storer s,String str)实现

Storer.java

public class Storer {
    private List<String> storer = new ArrayList<>();

    public void store(String str) {
        storer.add(str);
    }

    @Override
    public String toString() {
        return "Storer [storer=" + storer + "]";
    }

}
@FunctionalInterface
public interface MyInterface {
    public abstract void doSomething(Storer s, String str);
}
public class Executor {
    public void doStore(Storer storer, String s, MyInterface p) {
        p.doSomething(storer, s);
    }
}
public class TestFunctionalInterfaces {
    public static void main(String[] args) {
        Storer storer = new Storer();

        Executor test = new Executor();
        test.doStore(storer, "I've got added", Storer::store);

        System.out.println(storer);
    }
}
Executor.java

public class Storer {
    private List<String> storer = new ArrayList<>();

    public void store(String str) {
        storer.add(str);
    }

    @Override
    public String toString() {
        return "Storer [storer=" + storer + "]";
    }

}
@FunctionalInterface
public interface MyInterface {
    public abstract void doSomething(Storer s, String str);
}
public class Executor {
    public void doStore(Storer storer, String s, MyInterface p) {
        p.doSomething(storer, s);
    }
}
public class TestFunctionalInterfaces {
    public static void main(String[] args) {
        Storer storer = new Storer();

        Executor test = new Executor();
        test.doStore(storer, "I've got added", Storer::store);

        System.out.println(storer);
    }
}
testfunctioninterfaces.java

public class Storer {
    private List<String> storer = new ArrayList<>();

    public void store(String str) {
        storer.add(str);
    }

    @Override
    public String toString() {
        return "Storer [storer=" + storer + "]";
    }

}
@FunctionalInterface
public interface MyInterface {
    public abstract void doSomething(Storer s, String str);
}
public class Executor {
    public void doStore(Storer storer, String s, MyInterface p) {
        p.doSomething(storer, s);
    }
}
public class TestFunctionalInterfaces {
    public static void main(String[] args) {
        Storer storer = new Storer();

        Executor test = new Executor();
        test.doStore(storer, "I've got added", Storer::store);

        System.out.println(storer);
    }
}
输出为:

Storer [storer=[I've got added]]

提前感谢。

Storer::store
是一种方法参考,可用于替代任何
@functional界面。这里的内容基本上是我的接口实现的简写。等效值为:

public class MyInterfaceImpl implements MyInterface {
    public void doSomething(Storer storer, String str) {
       storer.store(str);
    } 
}

此实现(通过方法引用指定)是执行storer.store()的原因。。。由于MyInterface的实现,您已经指定并传递给您的执行者。编译器足够聪明,可以将实现/方法引用与您提供的参数相匹配。

方法引用
Store::storer
相当于lambda
(s,str)->s.Store(str)
。通常,给定一个需要args a1、a2、a3等的函数接口,这种类型的方法引用相当于调用a1.namedMethod(a2、a3等)的lambda。看