簡體   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