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

时间:2017-01-13 13:06:18

标签: c#

我一直在寻找解决方案,但无济于事。

为什么我无法访问列表

中创建的项目中的任何变量

(在底部附近看到的inv.inventory [i] .Name)

如果需要,我可以发布我的所有代码,尽管它是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}");
}

2 个答案:

答案 0 :(得分:4)

因为您在基类中没有Name字段。将其移动到基类。我还建议使用属性而不是公共字段:

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

你好像也不需要继承。您只需为基类字段提供值。您可以使用创建方法。

当然,您可以使用其他选项来创建具有预定义字段值的项目。但无论如何 - 如果您的班级不是,那么您就不需要新课程。只有字段值才有区别。在这种情况下,您需要一个类的不同实例。

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
}

甚至:

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
}

和库存:

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)
    }
 }   

答案 1 :(得分:1)

你正在徘徊你正在使用的继承。到目前为止,Item的继承没有任何优势。由于您的所有子类具有相同的参数,因此您只需将属性写入基类,并仅在派生类的构造函数中保留初始化:

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;
    }
}