int add(int a, int b)=> a + b;
int subtract(a, b)=> a - b;performOperation(int a, int b, int Function(int, int) function)=>function(a, b);// //简化写法// add(a, b) => a + b;// subtract(a, b) => a - b;// performOperation(a, b, function) => function(a, b);
使用
voidmain(){
var res11 =performOperation(1,2, add);var res12 =performOperation(1,2,(a, b)=> a + b);var res21 =performOperation(1,2, subtract);var res22 =performOperation(1,2,(a, b)=> a - b);print("res11: $res11");print("res12: $res12");print("res21: $res21");print("res22: $res22");}
(2)kotlin
定义
funadd(a: Int, b: Int)= a + b
funsubtract(a: Int, b: Int)= a - b
funperformOperation(a: Int, b: Int, action:(Int, Int)-> Int)=action(a, b)
使用
funmain(){
val res11 =performOperation(1,2,::add)val res12 =performOperation(1,2, action ={
a: Int, b: Int -> a + b })val res21 =