我有多个枚举,它们都具有相同的构造函数和属性,如下所示:
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;
}
}