我经常使用这种技术,但我不知道该怎么称呼它。我称之为关联枚举。那是对的吗?
例子:
public enum Genders {
Male("M"), Female("F"), Transgender("T"), Other("O"), Unknown("U");
private String code;
Genders(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public static Genders get(String code) {
for (Genders gender : values()) {
if (gender.getCode().equalsIgnoreCase(code)) {
return gender;
}
}
return null;
}
}