2009-11-21 94 views
5

我在簡短的問題:C#:繼承單獨的靜態成員的派生類

class A 
{ 
    /* Other stuff in my class*/ 
    protected static staticMember; 
} 

class B : A 
{ 
    /* Other stuff in my class*/ 
    // Will have A.staticMember but I want B.staticMember (same type) 
} 

class C : A 
{ 
    /* Other stuff in my class*/ 
    // Will have A.staticMember but I want C.staticMember (same type) 
} 

所以我想我所有的派生類中有一個是針對paricular類共享的共享數據,但必須在限定的公共簽名基類。

我在空閒時間爲樂趣創建了一個簡單的RTS遊戲。有幾種單位(宇宙飛船,建築物等)具有一些基本屬性。 這些單元可以由玩家來升級(屬於同一玩家相同類型的所有設備都升級例如玩家A升級坦克裝甲意味着他的所有坦克 會有更好的盔甲。)

這裏我如何努力實現這一目標:

abstract class Unit 
{ 
    /*several methods that is common for all units*/ 

    /*We don't know the unit's attributes at this point*/ 
    protected abstract double getMaxHitpoints(); 
    protected abstract double getFusionArmor(); 
    protected abstract double getNormalArmor(); 
    // and more similar abstract methods to come. 

    double maxHitpoints; 
    double fusionArmor; 
    double normalArmor; 
    //... 

    // This method is called on construction and after upgrade completion. 
    public void cacheAttributes(Player forPlayer) 
    { 
     Upgrade upgradesForThisUnit; //<<< Upgrade is class that is used like a struct to hold the upgrades for this kind of unit. 
     upgrades.TryGetValue(forPlayer,out upgradesForThisUnit); ///< get upgrades if available (if not available it will give the default value [=no bonuses]) 

     maxHitpoints=getMaxHitpoints()+upgradesForThisUnit.hitpointBonus; 
     fusionArmor=getFusionArmor()+upgradesForThisUnit.fusionArmorBonus; 
     normalArmor=getNormalArmor()+upgradesForThisUnit.normalArmorBonus; 
     //... 
    } 
    // This data structure is intended to hold the upgrades for every player for this kind of the unit 
    // but unfortunally derived classes have this instance too so if the player upgrades the tanks it will upgrade the interceptors, peasants, buildings too... 

    protected static Dictionary<Player,Upgrade> upgrades; 
} 

class Tank : Unit 
{ 
    protected override double getMaxHitpoints() {return 1000;} 
    protected override double getFusionArmor() {return 10;} 
    protected override double getNormalArmor() {return 50;} 
    //... 
} 

我想過增加一個額外的鑰匙我的dictonary(使用嵌套的字典):型結構爲重點,並修改這樣的代碼:

protected static Dictionary<Player,Dictionary<Type,Upgrade>> upgrades; 

public void cacheAttributes(Player forPlayer) 
{ 
    Dictionary<Type,Upgrade> upgradesForThePlayer; 
    upgrades.TryGetValue(forPlayer,out upgradesForThePlayer); 

    Upgrade upgradesForThisUnit; //<<< Upgrade is class that is used like a struct to hold the upgrades for this kind of unit. 
    upgradesForThePlayer.TryGetValue(GetType(),out upgradesForThisUnit); ///< get upgrades if available (if not available it will give the default value [=no bonuses]) 

    maxHitpoints=getMaxHitpoints()+upgradesForThisUnit.hitpointBonus; 
    fusionArmor=getFusionArmor()+upgradesForThisUnit.fusionArmorBonus; 
    normalArmor=getNormalArmor()+upgradesForThisUnit.normalArmorBonus; 
    //... 
} 

但我「米不知道這按預期工作。

該解決方案可能很簡單,但我不知道如何解決這個問題。

回答

0

如果通過字典字典你的意思是類似Dictionary<Player, Dictionary<Type, Upgrade>>的東西,那麼,是的,這將工作如你所願,並且是所述問題的良好解決方案。如果你有一些最大的玩家,你也可以使用一系列的字典,但你並沒有真正得到任何過分有用的散列值。 (如果你有4個玩家最大)

1
public class Singleton<T> 
    { 
     private static string _value; 
     public string Value 
     { 
      get 
      { 
       return _value; 
      } 
      set 
      { 
       _value = value; 
      } 
     } 
    } 
    public class At<T> 
    { 
     public static Singleton<T> field = new Singleton<T>(); 
    } 

    public class Bt : At<Bt> 
    { 
    } 

    public class Ct : At<Ct> 
    { 
    } 
... 
    Bt.field.Value = "bt"; 
    Ct.field.Value = "ct"; 
0

一個相當古老的問題,但是,未來的人受益,這是我身邊有類似的問題是如何得到。

class A 
{ 
    /* Other stuff in my class*/ 
    protected MyClass nonStaticMember; 
} 

class B : A 
{ 
    private static B _instance; 
    public static B instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 
    static B() 
    { 
     _instance = new B(); 
    } 
    /* Other stuff in my class*/ 
    // Will have B.instance.nonStaticMember 
} 

class C : A 
{ 
    private static C _instance; 
    public static C instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 
    static C() 
    { 
     _instance = new C(); 
    } 
    /* Other stuff in my class*/ 
    // Will have C.instance.nonStaticMember 
} 

這假定您可能想要從另一個班級訪問C.instance.doSomethingTo_nonStaticMember()