list为什么不能调用??

zhaooooooooo 2015-10-03 10:26:47
namespace 仓库类
{
class CangKu
{
//第一个功能:存储货物
//list[0]存储Acer笔记本电脑
//list[1]存储三星手机
//list[2]存储酱油
//list[3]存储香蕉
//List<FatherProduct> list1 = new List<FatherProduct>();
List<List<FatherProduct>> list = new List<List<FatherProduct>>();//这里的List<FatherProduct>相当于货柜
/// <summary>
/// 在创建仓库对象的时候,向仓库中添加货架
/// </summary>
public CangKu()
{
list.Add(new List<FatherProduct>());
list.Add(new List<FatherProduct>());
list.Add(new List<FatherProduct>());
list.Add(new List<FatherProduct>());
}
//进货
public static void GetProducts(string type,int count)
{
for (int i = 0; i < count; i++)
{
switch(type)
{
case "Acer":list[0].Add(new Acer(Guid.NewGuid().ToString(), 4000, "宏基笔记本"));
break;
case "Samsung":list[1].Add(new Samsung(Guid.NewGuid().ToString(), 3000, "三星手机"));
break;
case "Banana":list[2].Add(new Banana(Guid.NewGuid().ToString(), 10, "香蕉"));
break;
case "JiangYou":list[3].Add(new JiangYou(Guid.NewGuid().ToString(), 20, "老抽"));
break;
}
}
}


}
}

什么原因呢?
...全文
252 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2015-10-04
  • 打赏
  • 举报
回复
要定义成static的
threenewbee 2015-10-03
  • 打赏
  • 举报
回复
要定义成public的
ajianchina 2015-10-03
  • 打赏
  • 举报
回复
你这是静态方法调用非静态变量,此异常只需去掉方法修饰符static,或者给list变量定义时加上static修饰符。 我看你list[]里的元素是List<FatherProduct>类型,你下面的add操作中,像new Acer(,,)这些是在干啥?实例化吗?是list类型吗?到时我看要出错了吧。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧