C#:为派生类继承单独的静态成员

Cal*_*ius 5 c# static class derived

我的问题简要说明:

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)
}
Run Code Online (Sandbox Code Playgroud)

所以我希望我的所有派生类都有一个为该特定类共享的共享数据,但在基类中定义了一个共同的签名.

我正在创建一个简单的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;}
    //...
}
Run Code Online (Sandbox Code Playgroud)

我想为我的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;
    //...
}
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否符合预期.

解决方案可能很简单但我现在还不知道如何解决这个问题.

Don*_*nie 0

如果你所说的字典字典的意思大致与此类似Dictionary<Player, Dictionary<Type, Upgrade>>,那么,是的,它将按你的预期工作,并且是解决所述问题的一个很好的解决方案。如果最大玩家数量较少,您也可以使用字典数组,通过散列 4 个值并不会真正获得任何过于有用的信息。(如果您最多有 4 名玩家)