假設我有課程:class A : SuperType() {}class B : SuperType() {}class C : B() {}假設我不想C再擴展B():我希望它擴展A(),但現在我想A擴展B()。如何在編譯時A擴展B()(或任何子級SuperType())而不是僅擴展SuperType()?換句話說,我怎樣才能使類A聲明通用以接受任何孩子SuperType()?希望很清楚。我想做類似的事情:class B(whatever_it_receives_as_long_as_child_of_SuperType) : whatever_it_receives_as_long_as_child_of_SuperType()class C : A(B()) // B is subtype of SuperType so it's ok
2 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
您可以執行以下操作:
open class SuperType {}
open class A(val obj: SuperType) : B() {}
open class B : SuperType() {}
class C : A(B())
或使用泛型:
open class SuperType {}
open class A<T: SuperType>(val obj: T) : B() {}
open class B : SuperType() {}
class C : A<B>(B())
添加回答
舉報
0/150
提交
取消