kotlin 有没有办法在不声明类级别泛型类型的情况下创建泛型属性?看起来像这样的东西:
interface Generic {
val t: T //I need this type only once, thats why I dont wanna pass in the class level
fun <E> gMethod(e: E) { //This works, so I'm wondering if there's something similiar to properties
}
}
Run Code Online (Sandbox Code Playgroud)
我是 Kotlin 的新手,但泛型属性并不是什么问题,不是吗?
作为一个展示怎么样?我确实明白这个解决方案并不能完全解决您的问题。
interface BaseProperty<T> {
var value: T
}
class IntProperty(var stringVal: String) : BaseProperty<Int?> {
override var value: Int?
get() = Integer.valueOf(stringVal)
set(v: Int?) {
stringVal = v.toString()
}
}
Run Code Online (Sandbox Code Playgroud)
由于属性的 getter 和 setter 也是一个函数,如果我能够将 T 指定为泛型类型,那就太好了。我尝试使用 getValue 和 setValue 作为通用函数,这似乎有效,但没有使用 Kotlin 属性习惯用法。
归档时间: |
|
查看次数: |
4811 次 |
最近记录: |