使用类继承编译错误

时间:2013-07-01 00:18:09

标签: c# xna xna-4.0

我在我的PC类中遇到编译错误,我将其设置为继承的STR stat。编译器错误获取是“Entity.Stat不包含带有2个参数的构造函数。现在我知道情况并非如此,因为基本Entity类在其初始化序列中生成相同的声明。

如果有人可以看看我做错了什么,那就太好了。 StatType项是在另一个文件中声明的ENUM,并且没有问题。

class PC : Entity {

    private Class job;

    public PC(string name, Race race, Class job, int STR, int DEX, int CON, int WIS, int INT, int CHA){
        this.name = name;
        this.race = race;
        this.job = job;

        // This line here is the line giving me difficulties.
        this.STR = new Entity.Stat(STR, StatType.Strength);
    }

}

public class Entity
{
    protected string name;
    protected Race race;
    protected Stat STR;
    protected Stat DEX;
    protected Stat CON;
    protected Stat WIS;
    protected Stat INT;
    protected Stat CHA;

    public Entity(){ }

    public Entity(string name, Race race, int STR, int DEX, int CON, int WIS, int INT, int CHA){
        this.name = name;
        this.race = race;
        this.STR = new Stat(STR, StatType.Strength);
        this.DEX = new Stat(DEX, StatType.Dexterity);
        this.CON = new Stat(CON, StatType.Constitution);
        this.WIS = new Stat(WIS, StatType.Wisdom);
        this.INT = new Stat(INT, StatType.Intelligence);
        this.CHA = new Stat(CHA, StatType.Charisma);
    }

    private struct Stat
    {
        private int id;
        private int value;
        private int modifier;

        public Stat(int value, StatType id)
        {
            this.id = (int)id;
            this.value = value;
            this.modifier = ((value - 10) / 2);
        }

        public int Value
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;
                this.modifier = ((value - 10) / 2);
            }
        }

        public readonly int Modifier
        {
            get { return this.modifier; }
        }

    }
}

2 个答案:

答案 0 :(得分:2)

您的Stat结构是私有的,这意味着它只对Entity类可见,而不是子类。使其受到保护,Entity类和任何子类都可以看到它。

您也不能拥有readonly属性,因此您也会在public readonly int Modifier行上收到编译错误。

答案 1 :(得分:1)

我相信您需要将Stat切换为受保护的:

protected struct Stat

我不认为PC类可以看到Stat类,因为它对它不可见 - 只对实体有效。