【发布时间】:2009-11-30 09:18:20
【问题描述】:
您好,我无法概括我为特定枚举编写的函数:
public static enum InstrumentType {
SPOT {
public String toString() {
return "MKP";
}
},
VOLATILITY {
public String toString() {
return "VOL";
}
};
public static InstrumentType parseXML(String value) {
InstrumentType ret = InstrumentType.SPOT;
for(InstrumentType instrumentType : values()) {
if(instrumentType.toString().equalsIgnoreCase(value)) {
ret = instrumentType;
break;
}
}
return ret;
}
}
我希望在函数中添加一个新参数来表示任何枚举。我知道我应该使用模板,但我不能在函数代码中使用函数“values()”。 基本上我想要的是一个 valueOf 函数,它使用我定义的 toString() 值。
提前致谢。
【问题讨论】:
-
将枚举命名为 MKP 和 VOL 会不会更容易,这样您就可以免费解析字符串?
-
我同意,但我也希望它选择一个默认值,并且我不希望 ENUM 的名称依赖于它打印的字符串。