我在过去几个月里一直在使用Java 8,并试图让我的头脑围绕着lambdas.我对音乐会有很多了解.但是作为lambda调用自定义功能接口执行的努力.
如果我创建java Bifuctional接口实现
BiFunction<t1,t2,R> trade = (t1, t2) -> {
// calling another method for merger
return t1,t2;
};
Run Code Online (Sandbox Code Playgroud)
我可以像下面一样把它称为lambda吗?
(a, b)-> trade:
Run Code Online (Sandbox Code Playgroud)
或者我是否必须创建执行方法?
private int execute(BiFunction<t1,t2,R> trade, int a, int b){
return trade.apply(a, b)
}
Run Code Online (Sandbox Code Playgroud)
以下是调用lambda的代码示例:
BiFunction<t1,t2,R> trade = (t1, t2) -> {
// calling another method for merger return t1+t2;
};
public static void main(String[] args) {
execute(trade , 1, 2);
}
private int execute(BiFunction<t1,t2,R> trade, int a, int b) {
return trade.apply(a, b);
}
Run Code Online (Sandbox Code Playgroud)
我很好奇为什么编译器无法理解这一点
public …Run Code Online (Sandbox Code Playgroud)