2012-07-10 134 views
2

我有多個枚舉的所有具有相同的構造和屬性,就像這樣:「擴展」枚舉

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

爲什麼你有獨立的'Enum'類具有非常枚舉相同的數據?爲什麼'C'或'D'不能進入'Enum1'?我認爲給我們具體的情況可能有助於接近真正的解決方案。 – 2012-07-10 09:40:45

+0

請參閱[Java擴展枚舉](http://stackoverflow.com/questions/1414755/java-extend-enum)。 – Lion 2012-07-10 09:47:14

+1

對不起,但這種設計看起來不正確。當然YMMV,但這些屬性更好的「個人」盾牌,靴子等。我建議有「項目」有一個類型字段,而這又將是一個枚舉*沒有*屬性。當然,還有其他方法可以做到這一點,但請將這些信息作爲類屬性的一部分(從配置驅動)提供,而不是讓它們「硬編碼」。 – 2012-07-10 09:54:41

回答

1

枚舉擴展枚舉。他們不能再擴展別的東西。但是,他們可以實現接口。

您可以使它們都實現一個通用接口,並將getA(),getB()方法放在接口上。

3

你必須給他們(或不如果多數民衆贊成不是一個好主意)結合

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

對不起,但那些枚舉仍然必須分開。 – user28061 2012-07-10 09:42:08

+1

在這種情況下,你必須有一個Enum1,就像你現在有一個Enum1一樣。爲什麼你不能有一個組合的枚舉類型? – 2012-07-10 09:54:14

1
No, you can't extend enums in Java. 

正如彼得指出,可以將它們結合起來。

我的是this可以幫到你。

0

你可以嘗試使用這個比添加標誌,以您的枚舉:



    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 
`