0

呼び出された基本クラスと呼び出されたクラス、StockItemおよびEcoStockItemサブクラスPlateがあります。のサブクラスでEcoStockItemあるオブジェクトがあります。別のクラスには、メソッドがあります。クラスからアイテムをパラメーターとしてメソッドに送信する必要がありますか? サブクラスから他のプロパティにアクセスできませんでした。どうすればいいですか?サブクラスからすべてのプロパティを継承して配列に追加するオブジェクトを作成しますか?JuiceEcoStockItemAddItem (StockItem p)Form1StockItemAddItemStockItemStockItem

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