简体   繁体   English

C#-努力访问列表中对象的变量

[英]C# - Struggling to access variables for object within a list

I'm have tried looking around for a solution but to no avail. 我曾尝试寻找解决方案,但无济于事。

Why can't i access any of the variables within the created items in the list 为什么我不能访问列表中已创建项目中的任何变量

(inv.inventory[i].Name as seen near the bottom) (inv.inventory [i]。名​​称如底部所示)

If needed i can post all my code although it is 569 lines. 如果需要,我可以发布所有代码,尽管它是569行。

 public class Item { //All Item Attributes } public class PotionHP : Item { // Specific Attributes public string Name = "HP Potion"; public string Desc = "Restores HP by 10."; public int Cost = 8; public int Modifier = 10; } public class PotionAT : Item { // Specific Attributes public string Name = "AT Potion"; public string Desc = "Restores Attack by 1."; public int Cost = 8; public int Modifier = 10; } public class Revive : Item { // Specific Attributes public string Name = "Revive"; public string Desc = "Revives upon death with 5 HP."; public int Cost = 8; public int Modifier = 10; } public class Inventory { public List<Item> inventory = new List<Item>(); public Inventory() { inventory.Add(new PotionHP()); inventory.Add(new PotionAT()); inventory.Add(new PotionHP()); inventory.Add(new Revive()); } } for (int i = 0; i < inv.inventory.Count; i++) { //Why can't i access inv.inventory[i].Name? Console.WriteLine($"{inv.inventory[i].Name}"); } 

Because you don't have Name field in base class. 因为您在基类中没有Name字段。 Move it to base class. 将其移至基类。 Also I recommend to use properties instead of public fields: 另外,我建议使用属性而不是公共字段:

public class Item
{
    public string Name { get; set; }
}

Also seems like you don't need inheritance here. 似乎您在这里不需要继承。 You just providing values to base class fields. 您只需向基类字段提供值。 You can use Creation Methods for that. 您可以为此使用创建方法。

Of course, you can use other options to create items with predefined field values. 当然,您可以使用其他选项来创建具有预定义字段值的项目。 But anyway - you don't need new classes if your 'classes' differ only by values of fields . 但是无论如何- 如果您的“类”仅因field的值而不同,则不需要新的类 You need different instances of one class in that case. 在这种情况下,您需要一个类的不同实例。

public class Item
{
    public string Name { get; set; }
    public string Desc { get; set; }
    public int Cost { get; set; }
    public int Modifier { get; set; }

    public static Item CreatePotionAT()
    {
        return new Item
        {
           Name = "AT Potion",
           Desc = "Restores HP by 10.",
           Cost = 8,
           Modifier = 10
        };
    }

    // etc
}

Or even: 甚至:

public class Item
{
    public string Name { get; }
    public string Desc { get; }
    public int Cost { get; }
    public int Modifier { get; }

    public Item(string name, string desc, int cost, int modifier)
    {
        Name = name;
        Desc = desc;
        Cost = cost;
        Modifier = modifier;
    }

    public static Item CreatePotionHP()
    {
        return new Item("HP Potion","Restores HP by 10.", 8, 10);
    }

    // etc
}

And inventory: 和库存:

public Inventory()
{
    inventory = new List<Item>
    {
       Item.CreatePotionHP(), // creation method
       new Item("AT Potion","Restores Attack by 1.", 8, 10), // constructor
       Item.CreatePotionHP(),
       new Item("Revive","Revives upon death with 5 HP.", 8, 10)
    }
 }   

You are dangling around the inheritance that you are using. 您正在闲逛正在使用的继承。 Up to now the inheritance from Item does not have any advantage. 到目前为止,从Item继承没有任何优势。 Since all your children classes have the same parameters you can simply write the properties into the base class and leave only the initialization in the constructors of the derived classes: 由于所有子类都具有相同的参数,因此您可以简单地将属性写入基类,而仅将初始化保留在派生类的构造函数中:

public class Item
{

    public string Name = "HP Potion";
    public string Desc = "Restores HP by 10.";
    public int Cost = 8;
    public int Modifier = 10;

    //All Item Attributes
}
public class PotionHP : Item
{
    public PotionHP()
    {
        // Specific Attributes
        Name = "HP Potion";
        Desc = "Restores HP by 10.";
        Cost = 8;
        Modifier = 10;
    }
}
public class PotionAT : Item
{
    public PotionAT()
    {
        Name = "AT Potion";
        Desc = "Restores Attack by 1.";
        Cost = 8;
        Modifier = 10;
    }
}
public class Revive : Item
{
    public Revive()
    {
        // Specific Attributes
        Name = "Revive";
        Desc = "Revives upon death with 5 HP.";
        Cost = 8;
        Modifier = 10;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM