访问子类属性

时间:2013-11-02 15:03:02

标签: c#

我有一个名为StockItem的基类,一个名为EcoStockItemPlate的子类。 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);

1 个答案:

答案 0 :(得分:1)

三件事:

  1. 继承提供的关系,所以这应该可以正常工作:

    newJuice = new Juice();
    newJuice.Typ = jtyp;
    
    Stock stock = new Stock();
    //stock.AddItem(newJuice);   // this works since Juice IS a StoceItem
    
  2. 要在集合中存储可变数量的项目,List的效率会更高:

    class Stock
    {
        public List<StockItem> StockItems = new List<StockItem>();
    
        public void AddItem(StockItem item)
        {
            StockItems.Add(item);
            Form1.myForm.textBoxTest.Text = item.ToString();
        }
    
        public int GetCount()
        {
            return StockItems.Count;
        }
    }
    
  3. 不要直接从课程中引用您的表格 - 这可能就是您的原因;没有看到您期望的结果。通常,表单从其创建的对象中提取数据,或使用事件来获取推送给它的信息。