假設(shè)我有課程:class A : SuperType() {}class B : SuperType() {}class C : B() {}假設(shè)我不想C再擴(kuò)展B():我希望它擴(kuò)展A(),但現(xiàn)在我想A擴(kuò)展B()。如何在編譯時(shí)A擴(kuò)展B()(或任何子級SuperType())而不是僅擴(kuò)展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貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以執(zhí)行以下操作:
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())
添加回答
舉報(bào)
0/150
提交
取消