供应商的Java功能接口< t>无法编译非lambda
我已经说过:
import java.util.function.*;
public class FluentApi {
public Integer myfunc(){
return Integer.valueOf(1);
}
public void fSupplier(Supplier<Integer> si){
System.out.println(si.get());
}
public void callFunc(){
fSupplier(myfunc); // compilation failure
}
}
它说: myFunc无法解决到变量Java(33554515)
如果我将其更改为lambda函数,则它会编译:
public void callFunc(){
fSupplier(()->Integer.valueOf(1));
}
那么这里的核心差异是什么?如何在此处使用非LAMBDA实现?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要将
myFunc
作为。尝试以下操作:
You will need to pass
myfunc
as a method reference.Try this:
您的方法具有类型
供应商&lt; integer&gt;
的一个参数,因此您有三个选择将作为参数传递给此方法的选择(这个想法是通过一个实现功能接口供应商的类的实例):方法参考
匿名内部类
Your method have one parameter of type
Supplier<Integer>
so you have three choices to pass as argument to this method (the idea is to pass an instance of a class who implements the functional interface Supplier):A lambda expression
A method reference
An anonymous inner class