2015-04-28 109 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我发布了真正的代码,其中大部分都是西班牙文,我希望不会造成任何问题 –