2013-07-01 52 views
1

我在我的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; } 
     } 

    } 
} 

回答

2

您的Stat結構是私有的,這意味着它只對Entity類可見,而不是子類。使其受到保護,並且它對Entity類和任何子類都可見。

你也不能有readonly屬性,所以你也會在行public readonly int Modifier上得到編譯器錯誤。

+0

謝謝丹尼爾。我剛纔意識到我自己,保護與私人。 –

+0

至於readonly的評論,Visual Studio並沒有向我拋出一個錯誤,即該文檔不會隨只讀存在而編譯。這是編輯器在編譯之前無法提取的東西嗎? –

+0

沒關係,我運行了代碼分析,它對只讀屬性大吼大叫。感謝您的支持! –

1

我認爲你需要切換Stat要保護:

protected struct Stat 

我不瘦k PC類可以看到Stat類,因爲它不可見 - 只對實體。

+0

嗨大衛。你確實是對的。在我再次掃描代碼後,我想到了這一點。 –