2011-10-12 17 views
6

這是次要的,我知道,但假設我有一個類Character和一個類的能力(主要是因爲這就是我正在做的)。類角色有六種能力(所以典型的D & D ...)。基本上是:如何在C#中爲一個類創建一個簡化的賦值或默認屬性

public class Character 
{ 
    public Character() 
    { 
     this.Str = new Ability("Strength", "Str"); 
     this.Dex = new Ability("Dexterity", "Dex"); 
     this.Con = new Ability("Constitution", "Con"); 
     this.Int = new Ability("Intelligence", "Int"); 
     this.Wis = new Ability("Wisdom", "Wis"); 
     this.Cha = new Ability("Charisma", "Cha"); 
    } 

    #region Abilities 
    public Ability Str { get; set; } 
    public Ability Dex { get; set; } 
    public Ability Con { get; set; } 
    public Ability Int { get; set; } 
    public Ability Wis { get; set; } 
    public Ability Cha { get; set; } 
    #endregion 
} 

public class Ability 
{ 
    public Ability() 
    { 
     Score = 10; 
    } 
    public Ability(string Name, string Abbr) 
     : this() 
    { 
     this.Name = Name; 
     this.Abbr = Abbr; 
    } 

    public string Name { get; set; } 
    public string Abbr { get; set; } 
    public int Score { get; set; } 
    public int Mod 
    { 
     get 
     { 
      return (Score - 10)/2; 
     } 
    } 
} 

當實際使用在未來的代碼,這些能力的屬性,我希望能夠默認只是得分,就像這樣:

//Conan hits someone 
int damage = RollDice("2d6") + Conan.Str; 

//evil sorcerer attack drains strength 
Conan.Str = 0; 

而不是:

//Conan hits someone 
int damage = RollDie("2d6") + Conan.Str.Score; 

//evil sorcerer attack drains strength 
Conan.Str.Score = 0; 

現在,第一個案例可以採取隱式轉換:

public static implicit operator int(Ability a) 
{ 
    return a.Score; 
} 

任何人都可以幫我做相反的事嗎?隱式轉換是這樣的:

public static implicit operator Ability(int a) 
{ 
    return new Ability(){ Score = a }; 
} 

將替換整個屬性而不是屬性—不想要的結果的僅僅是比分...

+1

作爲該* *真的要做些什麼?沒有名字和縮寫的能力是否有效?你必須記住,你永遠不會真的將這些'int'存儲爲'Ability'能力,因爲它們缺少顯示數據。 –

+0

這是一個很好的觀點,埃德。簡短的回答,並不是真的(特別是因爲名字和Abbr在某個時候弄糟了本地化)。但是,這個例子表明,因爲我需要將「Bonus」,「CurScore」和「CurMod」屬性添加到Ability中。 –

+0

如果你的角色類是靜態的,那很容易。很明顯,你必須有各種角色,所以它不能是靜態的。 – GianT971

回答

3

首先,讓你的隱式轉換:

public static implicit operator Ability(int a) 
{ 
    return new Ability(){ Score = a }; 
} 

然後在你的角色等級中:爲str增加一個私有屬性屬性,並如下更改Str屬性的獲取者和設置者:

private Ability str; 
    public Ability Str 
    { 
     get 
     { 
      return this.str; 
     } 
     set 
     { 
      if (value.Name == "") 
      { 
       this.str.Score = value.Score; 
      } 
      else 
      { 
       this.str = value; 
      } 
     } 
    } 

你去那裏:)

您還可以使用:

   if(string.IsNullOrWhiteSpace(value.Name)) 

,而不是

   if (value.Name == "") 

如果要編譯到.NET 4.0版本

編輯 :我給你的解決方案是正是你想要,但ja72寫的也是一個很好的建議與運營商+和 - ;你可以將他的解決方案添加到我的(或者我的,不管),它會工作得很好。然後,您就可以寫:

 Character Jax = new Character(); // Str.Score = 10 
     Character Conan = new Character(); // Str.Score = 10 

     Jax.Str = 2000; // Str.Score = 2000; 
     Conan.Str += 150; // Str.Score = 160 
+0

這很棒。我無法相信自己沒有想到它。但是,那是一個很好的答案,不是嗎? :) –

+1

謝謝:-)我發現自己喜歡幫助人們解決編程問題,也許這是我將在下一份工作中做的事,誰知道^^ – GianT971

4

你可以做的是通過將這些方法Ability遞增成績最好的。

public static Ability operator + (Ability lhs, int score) 
    { 
     lhs.Score += score; 
     return lhs; 
    } 

    public static Ability operator - (Ability lhs, int score) 
    { 
     lhs.Score -= score; 
     return lhs; 
    } 

    public static implicit operator int(Ability rhs) 
    { 
     return rhs.Score; 
    } 

,並使用它們,如:

static void Main(string[] args) 
    { 
     Character evil = new Character(); //Str.Sccore=10 

     evil.Str += 10; //cast spell for Str.Sccore=20 

     evil.Str -= evil.Str; //death with Str.Sccore=0 
    } 
+0

+1,這就是我將如何去做。 –

+0

你並沒有真正回答他所問的問題,但這是一個很好的建議。我認爲他應該同時使用我們的兩種解決方案,因爲他可能想在某些情況下設置實力,而不是添加或移除能力點。 我測試了它,它對兩種解決方案都很好。 – GianT971

+0

這非常有用,我很感謝答案。 Gian's確實滿足我的需求,所以我會接受他的,但這是一個艱難的選擇。我很高興它獲得了可比的選票。它值得。 –

1

另一種選擇是與代表,以取代這類似

public class Character 
{ 
    public Character() 
    { 
     ... 
    } 

    #region Abilities 
    ... 
    #endregion 

    public Func<int> Strength 
    { 
     get { return() => Str.Score; } 
     set { Str.Score = value(); } 
    } 

} 

,並使用它像這樣

 Character evil = new Character(); //Str.Sccore=10 
     // fist spell hits 
     evil.Strength =() => 5; //set Str.Score=5 
     // second spell hits 
     evil.Strength =() => 0; //set Str.Score=5 

     if (evil.Strength() == 0) 
     { 
      // dead 
     } 
0

也許你可以製作Ability摘要,然後派生新類從Ability每個子類:Strength ...

Strength類的構造函數會是這個樣子:

public Strength() : base ("Strength", "Str") { ...} 

現在的能力屬性關閉一個Character將被強制鍵入,並且隱式轉換可以將值5轉換爲值爲5的Strength對象。例如,這也可以防止您意外地將Dexterity存儲在Strength屬性中。

[假設名稱和縮寫,其實固定該類型的所有對象。]

相關問題