2015-04-28 102 views
1

我有一個類Father和一個繼承類Child。在Child的構造函數中,我想通過一個Father類來施放所有Father的屬性。子級的子域父類

這是我的代碼

class Father 
{ 
    int prop1; 
    int prop2; 
    // many more properties; 
} 

class Child : Father 
{ 
    string _name; 
    int _age; 
    //etc... 

    public Child(string Name, int Age, Father father) 
    { 
     this._name = Name; 
     this._age = Age; 
     base = father; //<== this is what I mean to do 
    } 

我知道我不能直接做到這一點。什麼是正確的方式?

這是完整的代碼,一些代碼是在孩子的西班牙

class AuditRegistry : Audit 
{ 
    protected string _cud; 
    protected int _employee, _consecutive; 
    protected DateTime _date; 
    public string CUD { get { return _cud; } } 
    private int Consecutive { get { return _consecutive; } } 
    public DateTime Date { get { return _date; } } 
    public int Client { get; set; } 
    public int Employee { get { return _employee; } } 
    public float NetAmount 
    { 
     get 
     { 
      float acum = 0; 
      //Sum (qty * price) of products in a struct collection 

     } 

    } 
    public float GrossAmount 
    { 
     get 
     { 
      float acum = 0; 
      //Sum in acum (qty * price + tax) of each products in a struct collection 
      return acum; 
     } 
    } 
    public float Paid 
    { 
     get 
     { 
      float acum = 0; 
      //Sum every paid in a struct collection 

      return acum; 
     } 
    } 
    public float Change 
    { get; set;  } 
    public bool FullPaid 
    { 
     get { return (Paid != null && Paid >= NetAmount); } 
    } 
    public ArticlesCollection Products { get; set; } //struct previusly declared 
    public PaidsCollection Paids { get; set; } // struct previously declared 

    public AuditRegistry(string CUD, int Employee, int Consecutive, DateTime Date, int Client, int C, int Company, int Terminal) 
    { 
     this._cud = CUD; 
     this._employee = Employee; 
     this._consecutive = Consecutive; 
     this._date = Date; 
     this.Client = Client; 
     base._c = C; 
     base._company = Company; 
     base._terminal = Terminal; 
    } 
} 

class Order : AuditRegistry 
{ 
    int _consec; 
    public DateTime DeliveryDate { get; set; } 
    public int Consecutive { get { return _consec; } } 
    public char Modification { get; set; } 
    public string Code { get { return (_consec.ToString() + Modificacion); } } 
    public bool Entregado { get; set; } 

    /// <summary> 
    /// Constructor for load a Order from database to memory 
    /// </summary> 
    public Order(DateTime DeliveryDate, int Consecutive, char Modification, AuditRegistry Registry) // Here is the child constructor 
    { 
     this.DeliveryDate = DeliveryDate; 
     this._consec = Consecutive; 
     this.Modification = Modification; 
     base = AuditRegistry; //Wrong idea 
    } 

    /// <summary> 
    /// Constructor for new Order 
    /// </summary> 
    public Pedido(DateTime DeliveryDate, int Employee) 
    { 
     this.DeliveryDate = DeliveryDate; 
     this._consec = 1; 
     this.Modification = 'A'; 
     base._employee = Employee; 
     base._date = DateTime.Now; 

    } 
} 

回答

3

語義做父親一邊......

的一個好方法是使用一個拷貝構造函數:

class Father 
{ 
    int prop1; 
    int prop2; 
    // much more properties; 
    protected Father(Father copy) 
    { 
     prop1 = copy.prop1; 
     prop2 = copy.prop2; 
    } 
} 
class Child : Father 
{ 
string _name; 
int _age; 
//etc... 
public Child(string Name, int Age, Father father) 
    : base(father) 
{ 
    this._name = Name; 
    this._age = Age; 
} 
} 

它是一個受保護的構造函數,所以只有父類的孩子可以調用它。您使用構造函數鏈base(father)來指示基類的構造器並傳遞要複製的對象。

您不能直接在代碼中指定base對象,它只是對當前類派生自的基類實例的引用。

+0

深層複製在C#中非常罕見。確保你明白你在用這個解決方案正在做什麼。 – BradleyDotNET

+0

這是一個有趣的答案,無論如何,無法避免枚舉和匹配屬性? –

+0

正如你在答案中所說的那樣,如果父親更名爲「人」,這將會更有意義。它不清楚它是否僅僅是這個問題中的一個不好的例子或者發生了什麼。 –

2

還有絕對沒辦法做到這一點。 A Child a Father並且您不能將部分對象交換到另一個引用。 base關鍵字僅用於明確調用基類方法。

鑑於一個Child「類型的」 Father,繼承可能是錯誤的答案在這裏反正。你最好做類似的事情:

class Person 
class Father : Person 
class Child : Person 
{ 
    Father father; 
} 

(上面的僞代碼)。基本上,比起繼承,在這裏更喜歡構圖。

+0

@CesarRomeroo你需要顯示一些更多的細節,如什麼屬性需要共享。 – BradleyDotNET

+0

@CesarRomeroo Bradley認爲,由於當前在你的文章中的代碼示例,你不需要目前的繼承。如果你實際使用的課程名稱更好,在你的文章中做一個小例子會非常有幫助。如果你需要繼承,這將有助於清除。 – gunr2171

+0

@BradleyDotNET我發佈了真正的代碼,其中大部分都是西班牙文,我希望不會造成任何問題 –