2
我想遍歷類層次結構和打印所有超類:如何迭代類層次結構?
open class A {}
open class B : A() {}
open class C : B() {}
fun main(args: Array<String>) {
var currentClass = C::class
while(currentClass != Any::class) {
println(currentClass)
currentClass = currentClass.superclass
}
}
但科特林KClass
沒有superclass
方法。
我曾嘗試使用superclass
從Java:
currentClass = currentClass.java.superclass.kotlin
但它造成的編譯錯誤:
Error:(9, 52) Type inference failed: 'T' cannot capture 'in ('C'..'C?')'.
Type parameter has an upper bound 'kotlin.Any' that cannot be satisfied
capturing 'in' projection
因此,任何人可以解釋我怎麼辦呢?