我在我的PC類中得到了一個編譯錯誤,我將它設置爲繼承的STR屬性。編譯器出錯是一個「Entity.Stat不包含一個帶2個參數的構造函數,現在我知道情況並非如此,因爲基本實體類在其初始化序列中做出了相同的聲明使用類繼承編譯錯誤
如果有人可以看看,看看有什麼我做錯了,那將是巨大的。該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; }
}
}
}
謝謝丹尼爾。我剛纔意識到我自己,保護與私人。 –
至於readonly的評論,Visual Studio並沒有向我拋出一個錯誤,即該文檔不會隨只讀存在而編譯。這是編輯器在編譯之前無法提取的東西嗎? –
沒關係,我運行了代碼分析,它對只讀屬性大吼大叫。感謝您的支持! –