过去几个月我一直在使用 Java 8,并试图了解 lambdas。我对演唱会有点了解。但是在将自定义功能接口执行为 lambda 调用时苦苦挣扎。
如果我创建 java Bifuctional 接口实现
BiFunction<t1,t2,R> trade = (t1, t2) -> {
// calling another method for merger
return t1,t2;
};
我可以像下面那样将其称为 lambda 吗?
(a, b)-> trade:
还是我必须创建执行方法?
private int execute(BiFunction<t1,t2,R> trade, int a, int b){
return trade.apply(a, b)
}
下面是调用 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);
}
我很好奇为什么编译器不能理解这个
public static void main(String[] args) { int i= (1,2) -> trade; }