user28061
user28061

Reputation: 354

"extending" enum

I have multiple enum's that all have the same constructor and attributes, like this:

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

and so on... Unfortunately Enum1 and Enum2 already extend Enum, so it isn't possible to write a superclass they could extend. Is there another way to archive this?

Update: here comes a "real-world" example. Think of a classic rpg, where you have items, armour, weapons etc. which give you a bonus.

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

Upvotes: 2

Views: 741

Answers (4)

Michael Gordon
Michael Gordon

Reputation: 11

You can try to use this and than add flag to your enum:



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


Using is simple:


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

Upvotes: 0

Rob Trickey
Rob Trickey

Reputation: 1311

Enums extend Enum. They cannot also extend something else. However, they can implement interfaces.

You can make them both implement a common interface, and put your getA(), getB() methods on the interface.

Upvotes: 1

amicngh
amicngh

Reputation: 7899

No, you can't extend enums in Java.

As Peter mentioned you can combine them.

My be this can help you.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533790

You have to combine them (or not if thats not a good idea)

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

Upvotes: 3

Related Questions