0

我使用实验室 c#。将制定计划进行盘点。addItem 方法有问题。应该传递一个 StockItem 类型的参数,但在我调用该方法之前不对其进行整形。'已经尝试了一切,创建一个带有子类的新日志项实例,但是来自 subklaserna 的所有变量都被锁定在实例中。任何人都可以帮忙吗?提前致谢。

代码:

public class StockItem
    {

        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 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 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 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;
    }

表单类

private void buttonAdd_Click(object sender, EventArgs e)
{

    newStock = new StockItem(id, name, stockcount);
    newEcoStockItem = new EcoStockItem (markning);
    newPlate = new Plate(typ);
    newJuice = new Juice(jtyp);

    Stock st = new Stock();
    //st.AddItem(allItem);

}
4

0 回答 0