@XmlType(name = "WeekEnum")
@XmlEnum
public enum WeekEnum implements Serializable {
MONDAY("Monday"),
@XmlEnumValue("SUNDAY.ON")
SUNDAY("Sunday")
private final String value;
WeekEnum(String v) {
value = v;
}
public String value() {
return value;
}
public static WeekEnum fromValue(String v) {
for (WeekEnum c: WeekEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
我有一个Enum类WeekEnum,它是一个Jaxb类。
WeekEnum day = WeekEnum.SUNDAY;
获取价值" 星期日"来自Enum day 我们写 day.value();
获取价值" SUNDAY "来自Enum day 我们写 day.name();
我们如何获得价值" SUNDAY.ON "来自Eum 日 ??
如果我有价值" 星期日"我们如何得到相应的" SUNDAY.ON " ??
答案 0 :(得分:0)
您需要将其存储在与值类似的单独变量中。
@XmlType(name = "WeekEnum")
@XmlEnum
public enum WeekEnum implements Serializable {
MONDAY("Monday"),
@XmlEnumValue("SUNDAY.ON")
SUNDAY("Sunday", "SUNDAY.ON")
private final String value;
private final String xmlValue;
WeekEnum(String v) {
value = v;
}
WeekEnum(String v, String xmlValue){
this(v);
this.xmlValue = xmlValue;
}
public String value() {
return value;
}
public String getXmlValue() {
return xmlValue;
}
public static WeekEnum fromValue(String v) {
for (WeekEnum c: WeekEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
答案 1 :(得分:0)
如果您有一个名为week的枚举实例,则可以执行以下操作
week.getClass().getField(week.name()).getAnnotation(XmlEnumValue.class).value()