鉴于以下内容:
enum Moving {DownRight , UpRight ,DownLeft ,UpLeft ,Up , Down ,Left ,Right};
Moving m_moving;
如果我这样做:
m_moving = Moving.DownRight;
和
m_moving.toString();
然后我会得到:
DownRight
那么,如何为枚举中的每个条目添加描述?
例如,如果我m_moving.toString();
,那么我想提交FOO BAR
。
由于
答案 0 :(得分:7)
从我们的应用程序中查看此代码
public enum RegimeTributario {
SIMPLES_NACIONAL("SI"),
LUCRO_PRESUMIDO("LP"),
LUCRO_REAL("LR");
private String sigla;
private RegimeTributario(String sigla) {
this.sigla = sigla;
}
public String getSigla() {
return sigla;
}
}
枚举也接受字段
答案 1 :(得分:3)
关注this帖后,这是我的最终答案(也非常感谢@Luiz E.):
enum DirectionsEnum {
North("North"),
NorthEast("North East"),
East("East"),
SouthEast("South East"),
South("South"),
SouthWest("South West"),
West("West"),
NorthWest("North West");
private String value;
DirectionsEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return this.getValue();
}
public static DirectionsEnum getEnum(String value) {
if(value == null)
throw new IllegalArgumentException();
for(DirectionsEnum v : values())
if(value.equalsIgnoreCase(v.getValue())) return v;
throw new IllegalArgumentException();
}
}