- Lambda表达式
@FunctionalInterface
interface MyOperation{
abstract float operation(float a, float b);
}
@Test
public void testLambda(){
float a = 123;
float b = 234;
// 有参数、有返回值、多条语句使用{ }
float ret1 = fun(a, b, (float x, float y) -> {
System.out.println("求和计算");
return x + y;
});
System.out.println(ret1);
// 参数类型可省略,仅一条返回值语句,return可省略
float ret2 = fun(a, b, ( x, y) -> x + y);
System.out.println(ret2);
}
private float fun(float a, float b, MyOperation m){
return m.operation(a, b);
}