1 回答

TA贡献1815条经验 获得超6个赞
非静态字段、方法或需要对象引用 属性“Player.AddItems(int, string)”
您需要先创建一个对象才能调用类的非静态方法。
Player p = new Player("John", "25");
p.AddItems(1, "Data");
非静态字段、方法或需要对象引用 属性“Player.Items”。
您无法从静态方法访问非静态成员
PS:您可以直接为属性分配默认值。
class Player
{
public Player(string Name, int Age) : this()
{
this.Name = Name;
this.Age = Age;
}
public string Name { get; set; }
public int Age { get; set; }
public Dictionary<int, string> Items { get; set; } = new Dictionary<int, string>();
public float Health { get; set; } = 20;
public float Damage { get; set; }
public void AddItems(int key, string item)
{
this.Items.Add(key, item);
}
}
- 1 回答
- 0 关注
- 155 浏览
添加回答
举报