次のように、すべて同じコンストラクターと属性を持つ複数の列挙型があります。
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;
}
}