flutter、kotlin、java中将方法作为入参的区别

1、带参数带返回值的方法作为入参

(1)flutter

  • 定义
dart 复制代码
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);
  • 使用
dart 复制代码
void main() {
  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

  • 定义
kotlin 复制代码
fun add(a: Int, b: Int) = a + b
fun subtract(a: Int, b: Int) = a - b

fun performOperation(a: Int, b: Int, action: (Int, Int) -> Int) = action(a, b)
  • 使用
kotlin 复制代码
fun main() {
    val res11 = performOperation(1, 2, ::add)
    val res12 = performOperation(1, 2, action = { a: Int, b: Int -> a + b })
    val res21 = performOperation(1, 2, ::subtract)
    val res22 = performOperation(1, 2, action = { a: Int, b: Int -> a - b })

    println("res11: $res11")
    println("res12: $res12")
    println("res21: $res21")
    println("res22: $res22")
}

(3)java

  • 定义
java 复制代码
public class Test {

    int add(int a, int b) {
        return a + b;
    }

    int subtract(int a, int b) {
        return a - b;
    }


    int performOperation(int a, int b, BiFunction<Integer, Integer, Integer> action) {
        return action.apply(a, b);
    }
 }
  • 使用
java 复制代码
public class Test {
    int add(int a, int b) {
        return a + b;
    }

    int subtract(int a, int b) {
        return a - b;
    }


    int performOperation(int a, int b, BiFunction<Integer, Integer, Integer> action) {
        return action.apply(a, b);
    }

    void main(Test test) {
        int res11 = performOperation(1, 2, test::add);
        int res12 = performOperation(1, 2, (a, b) -> a + b);
        int res21 = performOperation(1, 2, test::subtract);
        int res22 = performOperation(1, 2, (a, b) -> a - b);
    }
}

2、带参数不带返回值的方法作为入参

(1)flutter

  • 定义
dart 复制代码
equals(int a, int b, Function(bool) callback) {
  if (a == b) {
    callback(true);
  } else {
    callback(false);
  }
}

// //简化写法
// equals(a, b, callback) {
//   if (a == b) {
//     callback(true);
//   } else {
//     callback(false);
//   }
// }
  • 使用
dart 复制代码
void main() {

  equals(2, 5, (value) => {
        if (value){
            //相等
            print("res3: 相等")
        }else{
            //不相等
            print("res3: 不相等")
        }
      }
  );
}

(2)kotlin

  • 定义
kotlin 复制代码
fun equals(a: Int, b: Int, callback: (Boolean) -> Unit) {
    if (a == b) {
        callback(true)
    } else {
        callback(false)
    }
}
  • 使用
kotlin 复制代码
fun main() {
    equals(1, 2, callback = { value ->
        if (value) {
            println("相等")
        } else {
            println("不相等")
        }
    })
}

(3)java

  • 定义
java 复制代码
public class Test {

    void equals(int a, int b, Function<Boolean, Void> callback) {
        if (a == b) {
            callback.apply(true);
        } else {
            callback.apply(false);
        }
    }
}
  • 使用
java 复制代码
public class Test {

    void equals(int a, int b, Function<Boolean, Void> callback) {
        if (a == b) {
            callback.apply(true);
        } else {
            callback.apply(false);
        }
    }


    void main() {

        equals(1, 2, (value) -> {
            if (value) {
                System.out.println("相等");
            } else {
                System.out.println("不相等");
            }
            return null;
        });
    }
}

3、不带参数不带返回值的方法作为入参

(1)flutter

dart 复制代码
checkOddNumber(int num, Function() oddCallback, Function() evenCallback) {
  if (num % 2 == 1) {
    //奇数
    oddCallback();
  } else {
    //偶数
    evenCallback();
  }
}

// //简化写法
// checkOddNumber(num, oddCallback, evenCallback) {
//   if (num % 2 == 1) {
//     oddCallback();
//   } else {
//     evenCallback();
//   }
// }
  • 使用
dart 复制代码
void main() {

  checkOddNumber(3, () => {print("res4: 奇数")}, () => {print("res4: 偶数")});
}

(2)kotlin

  • 定义
kotlin 复制代码
fun checkOddNumber(num: Int, oddCallback: () -> Unit, evenCallback: () -> Unit) {
    if (num % 2 == 1) {
        oddCallback()
    } else {
        evenCallback()
    }
}
  • 使用
kotlin 复制代码
fun main() {

    checkOddNumber(1,
        oddCallback = {
            println("奇数")
        },
        evenCallback = {
            println("偶数")
        })
}

(3)java

  • 定义
java 复制代码
public class Test {

    void checkOddNumber(int num, Function<Void, Void> oddCallback, Function<Void, Void> evenCallback) {
        if (num % 2 == 1) {
            oddCallback.apply(null);
        } else {
            evenCallback.apply(null);
        }
    }
}
  • 使用
java 复制代码
public class Test {

    void checkOddNumber(int num, Function<Void, Void> oddCallback, Function<Void, Void> evenCallback) {
        if (num % 2 == 1) {
            oddCallback.apply(null);
        } else {
            evenCallback.apply(null);
        }
    }

    void main() {

        checkOddNumber(1, (t1) -> {
            System.out.println("奇数");
            return null;
        }, (t2) -> {
            System.out.println("偶数");
            return null;
        });
    }
}
相关推荐
哎呦没31 分钟前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥1 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程2 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇2 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码2 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
郭二哈3 小时前
C++——模板进阶、继承
java·服务器·c++
A尘埃3 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-23073 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端
沉登c3 小时前
幂等性接口实现
java·rpc
代码之光_19803 小时前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端