1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
你不能重載一個(gè)類,所以:
public class Other : Class {}
public class Other : Class, IC<Other> {}
永遠(yuǎn)不會(huì)工作。
現(xiàn)在,我要拉喬恩斯基特并告訴你如何能做到這一點(diǎn),但后來從做它阻止你。你可以這樣做:
public class CloneableOther : Class, ICloneable<Other> { }
public class Other : CloneableOther
{
}
public class CloneableFoo : Class, ICloneable<Foo> { }
public class Foo : CloneableFoo
{
}
這段代碼所做的是有效地從繼承中刪除泛型參數(shù)。除了,F(xiàn)oo仍然可以這樣做:Foo : CloneableFoo, ICloneable<Other>,現(xiàn)在您必須為每個(gè)ICloneable實(shí)例創(chuàng)建兩個(gè)類。
這就是為什么你首先需要這個(gè)?這是一種做法Foo : IInterface<Foo>,但沒有辦法強(qiáng)制執(zhí)行。最好的辦法是復(fù)制和粘貼,并確保課程匹配。
也許另一種方法是在 的構(gòu)造函數(shù)中Class檢查 的類型是否是類ICloneable的類型,如果不是則拋出異常,這可能會(huì)讓人感覺像是編譯時(shí)錯(cuò)誤,如果它在運(yùn)行時(shí)完成得足夠早。
- 1 回答
- 0 關(guān)注
- 201 瀏覽
添加回答
舉報(bào)