C#List <baseClass> - 用于存储继承类的任何方法吗?

Dea*_*ust 6 c# inheritance list

我正在尝试创建一个简单的"库存"项目,就像在任何RPG中一样.我有非常基本的类,它们都有属性.

无论如何,我有一个基类,item并从中继承weapon.item具有属性(名称,值,重量,"重要项目")也在其中使用weapon,但weapon具有额外的属性(攻击,防御,速度,手性).

我有以下代码(抱歉,如果可读性太可怕了):

static void Main(string[] args)
{   
     List<item> inventory = new List<item>();
     inventory.Add(new weapon("Souleater", 4000, 25.50f, false, 75, 30, 1.25f, 2));
                           //item--------------------------->  weapon--------->

     Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack: {4}\nDefense: {5}\nSpeed: {6}\nHandedness: {7}",
            inventory[0].Name, inventory[0].BValue, inventory[0].Weight, inventory[0].Discard, 
            inventory[0].Atk, inventory[0].Def, inventory[0].Speed, inventory[0].Hands);

     Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

基本上,我要做的是weapon在库存中添加一个新的,但库存是一种List<item>类型.我一时兴起,希望由于它的继承,它会被接受.它是,但weapon无法访问特定属性:

('shopSystem.item'不包含'Atk'的定义,也没有扩展方法'Atk'接受'shopSystem.item'类型的第一个参数可以找到)

那么,有什么方法可以达到我的目的吗?有一个"库存",它可以存储item对象,以及weapon,armour,accessory等对象,它继承item?还值得一提的是,如果我声明以下内容,我可以访问所有需要的属性:

weapon Foo = new weapon("Sword", 200, 20.00f, false, 30, 20, 1.10f, 1);
Run Code Online (Sandbox Code Playgroud)

非常感谢阅读.

如果有人感兴趣的话,这是itemweapon课程:

class item
{
    #region Region: Item Attributes
    protected string name = "";
    protected int baseValue = 0;
    protected float weight = 0.00f;
    protected bool noDiscard = false;
    #endregion

    public item(string n, int v, float w, bool nd){
        name = n; baseValue = v; weight = w; noDiscard = nd;}

    public string Name{
        get{return name;}
        set{if(value != ""){
            name = value;}
        }//end set
    }

    public int BValue{
        get{return baseValue;}
    }

    public float Weight{
        get{return weight;}
    }

    public bool Discard{
        get{return noDiscard;}
    }
}

class weapon : item
{
    #region Region: Weapon Attributes
    private int atk = 0;
    private int def = 0;
    private float speed = 0.00f;
    private byte hands = 0;
    #endregion

    public weapon(string n, int v, float w, bool nd, int a, int d, float s, byte h) : base(n, v, w, nd){
        atk = a; def =d; speed = s; hands = h;}

    public int Atk{
        get{return atk;}
    }

    public int Def{
        get{return def;}
    }

    public float Speed{
        get{return speed;}
    }

    public byte Hands{
        get{return hands;}
    }

}
Run Code Online (Sandbox Code Playgroud)

com*_*ech 8

要访问特定于继承类的属性,需要转换为适当的具体类类型.因此,inventory[0]您需要访问,而不是访问(weapon)inventory[0].

此外,如果您执行的常见任务将基于底层类具有不同的实现,例如将值记录到控制台(我意识到这是为了测试,但无论如何都是一个很好的示例),您应该实现一个可覆盖的方法在基类中.

例如,在基类中:

public virtual void LogProperties()
{
Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack",
            this.Name, this.BValue, this.Weight, this.Discard);
}
Run Code Online (Sandbox Code Playgroud)

在武器类:

public override void LogProperties()
{
Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack: {4}\nDefense: {5}\nSpeed: {6}\nHandedness: {7}",
            this.Name, this.BValue, this.Weight, this.Discard, 
            this.Atk, this.Def, this.Speed, this.Hands);
}
Run Code Online (Sandbox Code Playgroud)

然后,访问这个:

inventory[0].LogProperties();
Run Code Online (Sandbox Code Playgroud)

  • 尝试使用 ((weapon)inventory[0]).Atk 代替 (2认同)