Chr*_*xps 3 generics interface kotlin
我目前正在开发一个接口,它有一个简单的函数,扩展该接口的所有类都应该实现该函数。但是这些类应该可以使用不同的函数参数进行调用,如下所示
interface IDoSomething<T> {
fun execute(vararg any: Any?): T // Make function arguments interchangeable
}
Run Code Online (Sandbox Code Playgroud)
class DoSomethingA : IDoSomething<String> {
// This is what i want
override fun execute(int: Int, boolean: Boolean): String {
println("Do something with parameters: $int, $boolean")
...
}
// This is what i need to do
override fun execute(vararg any: Any?): String {
val int = any[0] as Int
val boolean = any[1] as Boolean
println("Do something with parameters: $int, $boolean")
...
}
}
Run Code Online (Sandbox Code Playgroud)
实现此接口的其他类应该能够具有其他参数
class DoSomethingB : IDoSomething<String> {
// Should also be possible with same interface
override fun execute(string: String, double: Double, boolean: Boolean): String {
println("Do something with parameters: $string, $double, $boolean")
...
}
}
Run Code Online (Sandbox Code Playgroud)
Kotlin 中有什么可以帮助我做这样的想法吗?或者存在一种有助于解决此类问题的模式。
该语言中没有内置任何内容来实现您想要的功能(例如 C++ 可变参数模板)。
但是您仍然可以使用通用输入来实现您想要的目的,并将多个参数替换为包装它们的类:
interface IDoSomething<I, T> {
fun execute(input: I): T
}
class DoSomethingB : IDoSomething<Pair<String, Double>, String> {
// Should also be possible with same interface
override fun execute(input: Pair<String, Double>): String {
val (string, double) = input
println("Do something with parameters: $string, $double")
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是解决您的问题的最简单的解决方案。
您还有另一个更复杂的解决方案。
您可以有一个注释(例如@Input
),它接受每个实现所需支持的类型,然后您可以使用注释处理器生成接口的扩展,以实现编译时安全。
例如
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class Input(
val types: Array<KClass<*>> = []
)
@Input(types = [String::class, Double::class])
class DoSomethingB : IDoSomething<String> {
override fun execute(vararg any: Any?): String = execute(any) { string, double ->
println("Do something with parameters: $string, $double")
...
}
}
// With an annotation processor you can generate an extension like this.
fun DoSomethingB.execute(vararg input: Any?, block: (string: String, double: Double) -> String): String {
val string = input[0] as String
val double = input[1] as Double
return block(string, double)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1097 次 |
最近记录: |