@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;

为了从枚举日获取值“ Sunday”,我们编写了day.value();。

为了从枚举日获取值“ SUNDAY”,我们编写day.name();。

我们如何从枚举日获得值“ SUNDAY.ON”?

如果我的值是“ Sunday”,我们如何获得相应的“ SUNDAY.ON”?

最佳答案

您将需要将其存储在类似于value的单独变量中。

@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);
}
}

07-28 14:08