简体   繁体   English

Kotlin 可选参数与 Java 向后兼容

[英]Kotlin optional parameter with Java backwards compatibility

I'm currently writing a Kotlin library with an object having multiple methods requirering multiple optional parameters.我目前正在编写一个 Kotlin 库,其中 object 具有需要多个可选参数的多种方法。 Since in JAVA you would require to pass null to those parameters I think this is kind of unconfortable.由于在 JAVA 中,您需要将 null 传递给这些参数,我认为这有点不舒服。 Also overloading is not an solution as I don't know the possible combinations necessarily or there are ways to many possibilities.重载也不是解决方案,因为我不一定知道可能的组合,或者有很多可能性的方法。

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 { ... }
}

In Kotlin I can simply write:在 Kotlin 我可以简单地写:

myObj = MyObj()
myObj.method1(param1 = 4711, param3 = 8850)

In Java I need to write:在 Java 我需要写:

MyObj myObj = new MyObj()
myObj.method1(4711, null, 8850)
...    
myObj.method5("abc", null, null, null, null, 8850, null, null, "def")

Since I have quite a lot of methods with a lot of optionals I thought about just passing a class for each method:由于我有很多带有很多选项的方法,我想只为每种方法传递一个 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)
}

So in Kotlin I can used named parameters and in Java I could simply write:所以在 Kotlin 我可以使用命名参数,在 Java 我可以简单地写:

MyObj myObj = new MyObj()
myObj.method1(new Method1(4711).withParam3(8850))

From my point of view it looks fairly ugly because I always have to say method1(new Method1()) or method2(new Method2()) .从我的角度来看,它看起来相当难看,因为我总是不得不说method1(new Method1())method2(new Method2()) Do you think there is a more elegant version of this?你认为有一个更优雅的版本吗?

Background: This is for calling a REST API taking lots of optional parameters.背景:这是为了调用带有大量可选参数的 REST API。

For Java, the best way to handle a large amount of optional arguments is typically by some form of builder pattern.对于 Java,处理大量可选 arguments 的最佳方法通常是通过某种形式的构建器模式。 I would suggest one of two variants:我会建议以下两种变体之一:

1: Return a chainable object and have some sort of "run" method at the end to take the provided arguments and run the actual method. 1:返回一个可链接的 object 并在最后使用某种“运行”方法来获取提供的 arguments 并运行实际方法。

myObj.retrieveWhatever().from("abc").where(LESS_THAN, 3).setAsync(true).run();

2: For Java 8+, do the same, but in a lambda: 2:对于 Java 8+,执行相同操作,但在 lambda 中:

myObj.retrieveWhatever(args -> 
    args.from("abc").where(LESS_THAN, 3).setAsync(true)
);

...or, alternatively ...或者,或者,

myObj.retrieveWhatever(args -> {
    args.from("abc");
    args.where(LESS_THAN, 3);
    args.setAsync(true);
});

By doing it in a lambda, you remove the risk of forgetting the .run() call at the end.通过在 lambda 中执行此操作,您消除了最后忘记.run()调用的风险。

You can just add @JvmOverloads annotation for the functions您可以为函数添加@JvmOverloads注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java 7语言向后兼容性 - java 7 language backwards compatibility Java 8 Javascript引擎向后兼容性 - Java 8 Javascript Engine backwards compatibility 如果我们将 Optional.of 方法设为私有,并且只允许 Java 中的 Optional.ofNullable 会怎样; 除了向后兼容会不会有什么问题? - What if we make the Optional.of method private, and only allow Optional.ofNullable in Java; would there be any problem except backwards-compatibility? Java 与 Kotlin 的兼容性 - Java compatibility with Kotlin EJB规范和Java版本 - 向后兼容性 - EJB specifications and Java versions - backwards compatibility 增强Java 8库,同时保持向后兼容性 - Enhance library for Java 8 while keeping backwards compatibility 如何在保持向后兼容性的同时向现有方法添加新参数 - How to add a new parameter to existing methods while maintaining backwards compatibility 在不破坏二进制向后兼容性的情况下,Java 中的扩展方法可能吗? - Extension Methods in Java possible without breaking Binary Backwards Compatibility? JDBC 4的java.sql.Clob.free()方法和向后兼容性 - JDBC 4's java.sql.Clob.free() method and backwards compatibility 在Java Generics的情况下,向后兼容性究竟意味着什么? - What exactly is meant by backwards compatibility in case of Java Generics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM