gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:48:49 27 4
gpt4 key购买 nike

我的问题简述:

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

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

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

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

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

最佳答案

    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";

关于C#:继承派生类的单独静态成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1776369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com