尝试使用泛型类,但得到了下面的问题,那就是:科特林泛型超/子类
类型不匹配:推断类型是
ChildClass
但SuperClass<SuperType>
预计
open class SuperClass<T> where T: SuperType {
fun modifySomething(input: T): T {
input.someValue.inc()
return input
}
}
open class SuperType {
val someValue: Int = 0
}
class ChildClass : SuperClass<ChildType>()
class ChildType: SuperType() {
fun getModifiedValue(): Int {
return someValue
}
}
class TestEnvironment {
fun testType(superClass: SuperClass<SuperType>) {
// do something with superClass
}
fun itDoesntWork() {
testType(ChildClass()) // compilation error
}
}
这里是the gist和the kotlin playground
所需结果LT是功能testType(superClass: SuperClass<SuperType>)
应该接受类ChildClass()
不使用通配符*
请在问题本身的代码。 – nhaarman