2009-11-21 10 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は戦車鎧が が良い鎧を持って彼のすべての戦車ことを意味アップグレードされます。)

ここにありますどのように私はこれをachiveしようとした:

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; 
    //... 
} 

しかしI:タイプの構造をキーとして、このようなコードを変更します'これが期待どおりに機能しているかわかりません。

解決策はおそらく単純ですが、私は今これをどのように解決するかという手がかりがありません。

答えて

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

(あなたは4人の選手の最大を持っている場合)、しかし、未来の人々の利益のために、ここで私は同様の問題を中心に得た方法です。

これは、別のクラスからC.instance.doSomethingTo_nonStaticMember()にアクセスしたいと仮定していることを前提としています。