【问题标题】:"extending" enum“扩展”枚举
【发布时间】:2012-07-09 18:18:52
【问题描述】:

我有多个枚举,它们都具有相同的构造函数和属性,如下所示:

enum Enum1 {
    A(1,2),
    B(3,4);

    public int a, b;
    private Enum1(int a, int b) {
        this.a = a;
        this.b = b;
    }
}


enum Enum2 {
    C(6,7),
    D(8,9);

    public int a, b;
    private Enum1(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

等等…… 不幸的是,Enum1 和 Enum2 已经扩展了 Enum,因此不可能编写它们可以扩展的超类。 还有其他方法可以存档吗?

更新:这里有一个“真实世界”的例子。想想一个经典的 rpg,你有物品、盔甲、武器等,它们会给你带来奖励。

enum Weapon {
    SWORD(3,0,2),
    AXE_OF_HEALTH(3,4,1);

    // bonus for those weapons
    public int strength, health, defense;
    private Weapon(int strength, int health, int defense) {
        this.strength = strength;
        this.health = health;
        this.defense = defense;
    }
}

enum Armour {
    SHIELD(3,1,6),
    BOOTS(0,4,1);

    // bonus
    public int strength, health, defense;
    private Weapon(int strength, int health, int defense) {
        this.strength = strength;
        this.health = health;
        this.defense = defense;
    }
}

【问题讨论】:

  • 为什么你有单独的Enum 类用于具有几乎相同数据的枚举?为什么CD 不能进入Enum1?我认为向我们提供具体场景可能有助于更接近真正的解决方案。
  • 很抱歉,这个设计看起来不太对劲。当然是 YMMV,但这些属性在“个人”盾牌、靴子等上会更好。我建议使用具有类型字段的“项目”,而这又是一个枚举 没有 属性。当然还有其他方法可以做到这一点,但请让信息作为类属性的一部分(由配置驱动)而不是让它们“硬编码”。
  • @SanjayT.Sharma 所以你会这样做:see pastebin。那我怎么能得到例如一个随机的后缀?
  • @user28061:不,看起来还是不对。为什么你如此专注于使用“枚举”?为什么这些实体不能单独成为一个不同的类(例如GameEntity 而不是enum?将自己与通常非常动态的东西(游戏实体)的枚举捆绑在一起,具有行为并且是其中的一部分逻辑层次结构(即可能需要继承功能)是 IMO 的一个大错误。从常规类开始并编写解决直接问题(工作游戏)的东西,而不是与枚举限制作斗争。:)

标签: java enums


【解决方案1】:

您可以尝试使用它,然后将标志添加到您的枚举中:



    public class ExtendetFlags : Attribute
    {
        #region Properties

        /// 
        /// Holds the flagvalue for a value in an enum.
        /// 
        public string FlagValue { get; protected set; }

        #endregion

        #region Constructor

        /// 
        /// Constructor used to init a FlagValue Attribute
        /// 
        /// 
        public ExtendetFlags(string value)
        {
            this.FlagValue = value;
        }

        #endregion
    }

    public static class ExtendetFlagsGet
    {
        /// 
        /// Will get the string value for a given enums value, this will
        /// only work if you assign the FlagValue attribute to
        /// the items in your enum.
        /// 
        /// 
        /// 
        public static string GetFlagValue(this Enum value)
        {
            // Get the type
            Type type = value.GetType();

            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            ExtendetFlags[] attribs = fieldInfo.GetCustomAttributes(
                typeof(ExtendetFlags), false) as ExtendetFlags[];

            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].FlagValue : null;
        }
    }


使用很简单:


`            [ExtendetFlags("test1")]
            Application = 1,
            [ExtendetFlags("test2")]
            Service = 2
`

【讨论】:

    【解决方案2】:

    枚举扩展枚举。他们也不能扩展其他东西。但是,它们可以实现接口。

    您可以让它们都实现一个通用接口,并将您的 getA()、getB() 方法放在接口上。

    【讨论】:

      【解决方案3】:
      No, you can't extend enums in Java.
      

      正如彼得所说,您可以将它们组合起来。

      我的 this 可以帮助你。

      【讨论】:

        【解决方案4】:

        您必须将它们组合起来(如果这不是一个好主意,则不要)

        enum Enum1 {
            A(1,2),
            B(3,4),
            C(6,7),
            D(8,9);
        

        【讨论】:

        • 抱歉,这些枚举仍然必须分开。
        • 在这种情况下,您必须像现在一样拥有一个 Enum1 和一个组合的 Enum2。为什么不能有一种组合枚举类型?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多