覆盖枚举

时间:2013-06-01 01:02:08

标签: c# enums xna override

我知道之前已经回答了这个问题,但我似乎无法找到我想要的答案,其他人太具体了。这是我的一般性问题;你如何覆盖枚举?这会有用吗?

//Enum in the main class it is defined in
public enum GameDifficulty
{
   i1,
   i2,
   i3
}


//Enum class being imported:
using game1.classwithenum;

//Would this override the enum and replace i1, i2, and i3 with f1, f2, and f3?
public enum GameDifficulty
{
   f1,
   f2,
   f3
}

2 个答案:

答案 0 :(得分:2)

您粘贴的代码无法正常工作......

唯一可以做到这一点的方法是将每个Enumeration放在自己的命名空间

但即便如此,就类型强制而言,i1与f1不同。

正如JW所说,您可以使用New关键字覆盖名称,但您仍然必须通过命名空间访问它,而GameDifficulty.i1将不等于classwithenum.GameDifficulty.i1

我强烈认为有更好的方法可以做到这一点,你能澄清一下你想要实现的目标,并提供一个例子吗?

答案 1 :(得分:1)

您无法重命名或扩展现有枚举,但是您可以在创建自己的enum时从中借用值:

public enum MyGameDifficulty {
  Recruit = GameDifficulty.Easy,
  Soldier = GameDifficulty.Normal,
  Commander = GameDifficulty.Hard,
  Veteran }