5

我的问题简述:

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)
}

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

我正在创建一个简单的 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;}
    //...
}

我考虑在我的字典中添加一个额外的键(使用嵌套字典):将结构类型作为键并修改如下代码:

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

但我不确定这是否按预期工作。

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

4

3 回答 3

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";
于 2009-11-21T20:41:33.777 回答
0

如果通过字典词典您的意思大致类似于Dictionary<Player, Dictionary<Type, Upgrade>>then,是的,这将按您的预期工作,并且是解决上述问题的好方法。如果您的最大玩家数量较少,您也可以使用字典数组,因为散列 4 个值并没有真正得到任何过于有用的东西。(如果您最多有 4 名玩家)

于 2009-11-21T19:02:47.250 回答
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()从另一个类访问。

于 2011-11-18T08:00:19.730 回答