我有一个名为的基类StockItem
和一个名为EcoStockItem
的Plate
子类。EcoStockItem
有一个Juice
对象是 的子类EcoStockItem
。在另一堂课中,我有方法AddItem (StockItem p)
。我应该从Form1
课堂上将StockItem
项目作为参数发送给方法AddItem
吗?我无法从子类访问其他属性。我该怎么做?创建一个对象StockItem
以继承子类的所有属性并将其添加到StockItem
数组中?
public class StockItem
{
private Juice newJuice;
public int Id { get; set; }
public string Name { get; set; }
public int StockCount { get; set; }
public StockItem(int Id, string Name, int StockCount)
{
this.Id = Id;
this.Name = Name;
this.StockCount = StockCount;
}
public StockItem()
{
// TODO: Complete member initialization
}
public StockItem(Juice newJuice)
{
// TODO: Complete member initialization
this.newJuice = newJuice;
}
public override string ToString()
{
return String.Format("Id:{0} Namn:{1} Count:{2}", Id,Name,StockCount);
}
}
public class EcoStockItem : StockItem
{
public string Markning { get; set; }
public EcoStockItem()
{
}
public EcoStockItem(string Markning)
{
this.Markning = Markning;
}
public EcoStockItem(int i, string n, int sc, string Markning):base (i,n,sc)
{
base.Id = i;
base.Name = n;
base.StockCount = sc;
this.Markning = Markning;
}
public override string ToString()
{
return String.Format("Mark: {0}", Markning);
}
}
public class Plate : StockItem
{
public string Typ { get; set; }
public Plate(string Typ)
{
this.Typ = Typ;
}
public Plate(int i, string n, int sc, string Typ):base (i,n,sc)
{
base.Id = i;
base.Name = n;
base.StockCount = sc;
this.Typ = Typ;
}
public Plate()
{
// TODO: Complete member initialization
}
public override string ToString()
{
return String.Format("Plate: {0}", Typ);
}
}
public class Juice : EcoStockItem
{
public string Typ { get; set; }
public Juice (string Typ)
{
this.Typ = Typ;
}
public Juice(int i, string n, int sc, string m,string Typ):base (i,n,sc,m)
{
base.Id = i;
base.Name = n;
base.StockCount = sc;
base.Markning = m;
this.Typ = Typ;
}
public Juice()
{
// TODO: Complete member initialization
}
public override string ToString()
{
return String.Format("Juice: {0}", Typ);
}
}
class Stock
{
public StockItem[] StockItems = new StockItem[10];
public void AddItem(StockItem item)
{
int index = GetCount();
StockItems[index] = item;
Form1.myForm.textBoxTest.Text = StockItems[index].ToString();
}
public int GetCount()
{
int count = 0;
for (int n = 0; n < StockItems.Length; n++)
{
if (StockItems[n] != null)
{
count++;
}
}
return count;
}
}
}
class Form1
private void buttonAdd_Click(object sender, EventArgs e)
{
newStockItem = new StockItem();
newStockItem.Id = id;
newStockItem.Name = name;
newStockItem.StockCount = stockcount;
newEcoStockItem = new EcoStockItem ();
newEcoStockItem.Markning = markning;
newPlate = new Plate();
newPlate.Typ = typ;
newJuice = new Juice();
newJuice.Typ = jtyp;
//StockItem p = new StockItem(...........); ?????????????
Stock stock = new Stock();
//stock.AddItem(p);