问:用Java创建函数最方便的方法是什么?
In Python, when we need a light-weight lambda function myFunc = lambda a,b,c,d -> a+b+c+d
.
It has 2 advantages, 1. general syntax (one syntax works no matter of # of vars, return type) 2. cheap, no extra class is created.
在Java中,我们能做的最好的事情就是
interface MyFunc {
public int sum(int a, int b, int c, int d);
}
// then
MyFunc myFunc = (a, b, c, d) -> a + b + c + d;
myFunc.sum(0, 1, 2, 3);
但是,这还不可以吗?我们只需要一个函数,但是在这里我们必须创建一个仅使用一次的接口,那么用Java创建函数最方便的方法是什么?