看板 C_Sharp 關於我們 聯絡資訊
※ 引述《SDNiceBoat (NiceBoat.)》之銘言: : 例如: : class A: : { : public virtual void FunA() : { : //---------- : } : } : class B:List<A>,A ← 會出錯,因為不能多重繼承 : { : public override void FunA() : { : foreach(A a in this) : { : a.FunA(); : } : } : } : ==================================================== : 主要是希望能把B class 放進另一個B class當中 : 讓他可以變成巢狀結構 : 請問該怎麼做? : 雖然可以用介面去解 : 但萬一裡面的東西一多的話,會有一堆重複的程式碼..... : 實作起來跟之後要修改都會很麻煩.... 你需要的是default property... using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class A { public virtual void FuncA() { } } public class B : A { protected List<A> _item; // 這裡看你想這樣還是implement一堆forwarder // 把List<A>中需要的method expose出來 public List<A> CollA { get { return this._item; } } public A this[int index] { get { return this._item[index]; } set { this._item[index] = value; } } } } -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.92.4.195 ※ 編輯: leicheong 來自: 61.92.4.195 (08/21 23:33)
SDNiceBoat:可是,我是想直接把List的功能繼承下來,而不是重新寫 08/22 00:53
SDNiceBoat:重新寫List的工作會比繼承介面的方法來的浩大...... 08/22 00:54
SDNiceBoat:總而言之,還是謝謝回答 08/22 01:00
leicheong:那沒解了, 你看XmlNode也是用一堆Interface來做的. 08/22 09:25
leicheong:這也算是簡化語言結構的代價吧... (就像functional 08/22 09:27
leicheong:programming不能直接轉寫某些常用pattern一樣) 08/22 09:28
SDNiceBoat:瞭解了 感謝..... 08/22 11:32