来自儿童班的父亲班

时间:2015-04-28 19:43:29

标签: c# inheritance

我有一个班级Father和一个继承的班级Child。在Child的构造函数中,我想传递一个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;

    }
}

2 个答案:

答案 0 :(得分: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对象,它只是对当前类派生的基类实例的引用。

答案 1 :(得分:2)

绝对无法做到这一点。 Child Father,您无法将对象的一部分交换到另一个引用。 base关键字仅用于显式调用基类方法。

鉴于Child a&#34;类型为&#34; Father,无论如何,继承可能是错误的答案。你最好做一些事情:

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

(上面的伪代码)。基本上,这里更喜欢构图而不是继承。