[英]Kotlin optional parameter with Java backwards compatibility
我目前正在编写一个 Kotlin 库,其中 object 具有需要多个可选参数的多种方法。 由于在 JAVA 中,您需要将 null 传递给这些参数,我认为这有点不舒服。 重载也不是解决方案,因为我不一定知道可能的组合,或者有很多可能性的方法。
class MyObj() {
fun method1(param1: Int, param2: String? = null, param3: Int? = null): String { ... }
fun method2(param1: Int, param2: Float? = null, param3: String? = null): Any { ... }
fun method5(param1: Int, ...): User { ... }
}
在 Kotlin 我可以简单地写:
myObj = MyObj()
myObj.method1(param1 = 4711, param3 = 8850)
在 Java 我需要写:
MyObj myObj = new MyObj()
myObj.method1(4711, null, 8850)
...
myObj.method5("abc", null, null, null, null, 8850, null, null, "def")
由于我有很多带有很多选项的方法,我想只为每种方法传递一个 class :
/** For JAVA backwards compatibility */
class Method1(
val param1: Int
) {
param2: String? = null
param3: Int? = null
fun withParam2(value: String) = apply { param2 = value }
fun withParam3(value: Int) = apply { param3 = value }
}
class MyObj() {
fun method1(param1: Int, param2: String? = null, param3: Int? = null): String { ... }
/** For JAVA backwards compatibility */
fun method1(values: Method1) = method1(values.param1, values.param2, values.param3)
}
所以在 Kotlin 我可以使用命名参数,在 Java 我可以简单地写:
MyObj myObj = new MyObj()
myObj.method1(new Method1(4711).withParam3(8850))
从我的角度来看,它看起来相当难看,因为我总是不得不说method1(new Method1())
或method2(new Method2())
。 你认为有一个更优雅的版本吗?
背景:这是为了调用带有大量可选参数的 REST API。
对于 Java,处理大量可选 arguments 的最佳方法通常是通过某种形式的构建器模式。 我会建议以下两种变体之一:
1:返回一个可链接的 object 并在最后使用某种“运行”方法来获取提供的 arguments 并运行实际方法。
myObj.retrieveWhatever().from("abc").where(LESS_THAN, 3).setAsync(true).run();
2:对于 Java 8+,执行相同操作,但在 lambda 中:
myObj.retrieveWhatever(args ->
args.from("abc").where(LESS_THAN, 3).setAsync(true)
);
...或者,或者,
myObj.retrieveWhatever(args -> {
args.from("abc");
args.where(LESS_THAN, 3);
args.setAsync(true);
});
通过在 lambda 中执行此操作,您消除了最后忘记.run()
调用的风险。
您可以为函数添加@JvmOverloads
注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.