0

我有一个名为的基类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);
4

1 回答 1

1

三件事:

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

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

    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. 不要直接从课堂上引用您的表格 - 这可能是您没有看到预期结果的原因。通常,表单要么从它创建的对象中提取数据,要么使用事件来获取推送给它的信息。

于 2013-11-02T15:14:32.613 回答