簡単で私の問題のために別々の静的メンバを継承:の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:タイプの構造をキーとして、このようなコードを変更します'これが期待どおりに機能しているかわかりません。
解決策はおそらく単純ですが、私は今これをどのように解決するかという手がかりがありません。