繁体   English   中英

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

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

我曾尝试寻找解决方案,但无济于事。

为什么我不能访问列表中已创建项目中的任何变量

(inv.inventory [i]。名​​称如底部所示)

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

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

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

似乎您在这里不需要继承。 您只需向基类字段提供值。 您可以为此使用创建方法。

当然,您可以使用其他选项来创建具有预定义字段值的项目。 但是无论如何- 如果您的“类”仅因field的值而不同,则不需要新的类 在这种情况下,您需要一个类的不同实例。

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

您正在闲逛正在使用的继承。 到目前为止,从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;
    }
}

暂无
暂无

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

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