1

我在设置继承 STR stat 的行中的 PC 类中出现编译错误。得到的编译器错误是“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; }
        }

    }
}
4

2 回答 2

2

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

你也不能有readonly属性,所以你也会得到一个编译器错误public readonly int Modifier

于 2013-07-01T00:23:10.250 回答
1

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

protected struct Stat

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

于 2013-07-01T00:22:48.863 回答