class SuperMarket
{
WareHouse CangKu = new WareHouse();
//CangKu.GoodsIn("DreamCar", 100);這麼寫會報錯
public SuperMarket()
{
CangKu.GoodsIn("DreamCar", 100);
CangKu.GoodsIn("Apple", 800);
CangKu.GoodsIn("Book", 997);
CangKu.GoodsIn("Pencil", 68);
}
}
——————————————分割線—————————————————
class WareHouse
{
List<List<Commodity>> storeroom = new List<List<Commodity>>();
public WareHouse()
{
storeroom.Add(new List<Commodity>());
storeroom.Add(new List<Commodity>());
storeroom.Add(new List<Commodity>());
storeroom.Add(new List<Commodity>());
}
public void GoodsIn(string type, int count)
{
for (int i = 0; i < count; i++)
{
switch (type)
{
case "DreamCar": storeroom[0].Add(new DreamCar("DreamCar", 180000, Guid.NewGuid().ToString())); break;
case "Apple": storeroom[1].Add(new Apple("嘎?果", 3, Guid.NewGuid().ToString())); break;
case "Book": storeroom[2].Add(new Book("心靈鴨湯讀本", 14, Guid.NewGuid().ToString())); break;
case "Pencil": storeroom[3].Add(new Pencil("小嘴牌鉛筆", 0.9, Guid.NewGuid().ToString())); break;
}
}
}
} ![圖片說明](http://img.ask.csdn.net/upload/201508/30/1440866253_103476.png)
想簡單模擬超市收銀,寫好WareHouse類,然後在SuperMarket中創建CangKu的時候,想添加商品進去,出現了圖示情況。我初學,實在太菜,請大神指導,3Q。
你必須把代碼寫在方法裡,你寫在類的定義中了。
不一定寫在構造函數,但是一定要寫在函數裡。