我有一個類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;
}
}
深層複製在C#中非常罕見。確保你明白你在用這個解決方案正在做什麼。 – BradleyDotNET
這是一個有趣的答案,無論如何,無法避免枚舉和匹配屬性? –
正如你在答案中所說的那樣,如果父親更名爲「人」,這將會更有意義。它不清楚它是否僅僅是這個問題中的一個不好的例子或者發生了什麼。 –