Mon problème en bref:C#: Héritant membres statiques séparés pour les classes dérivées
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)
}
Je veux que tous mes classe dérivée d'avoir une donnée partagée qui est partagée pour cette classe pariculier, mais ont une signature commune définie dans la classe de base. Je crée un simple jeu RTS pour m'amuser pendant mon temps libre. Il existe plusieurs types d'unités (vaisseaux spatiaux, bâtiments, etc.) qui ont des attributs de base. Ces unités peuvent être mis à jour par les joueurs (toutes les unités du même type appartenant au même joueur sont mis à jour par exemple. Le joueur A met à niveau les armures des chars signifie que tous ses réservoirs auront une meilleure armure.)
Voici comment j'ai essayé de achive ceci:
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;}
//...
}
Je pensais que sur l'ajout d'une clé supplémentaire à mon dictonary (en utilisant le dictionnaire imbriqué): structures de type comme clé et modifier le code comme ceci:
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;
//...
}
Mais je ' Je ne suis pas sûr que cela fonctionne comme prévu.
La solution est probablement simple mais je n'ai aucune idée de comment résoudre ce problème maintenant.