鉴于以下内容:
enum Moving {DownRight , UpRight ,DownLeft ,UpLeft ,Up , Down ,Left ,Right};
Moving m_moving;
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
m_moving = Moving.DownRight;
Run Code Online (Sandbox Code Playgroud)
和
m_moving.toString();
Run Code Online (Sandbox Code Playgroud)
然后我会得到:
DownRight
Run Code Online (Sandbox Code Playgroud)
那么,如何为枚举中的每个条目添加描述?
例如,如果我这样做,m_moving.toString();那么我想要呈现FOO BAR.
谢谢
从我们的应用程序中查看此代码
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;
}
}
Run Code Online (Sandbox Code Playgroud)
枚举也接受字段
在这篇文章之后,这是我的最终答案(也非常感谢@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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4623 次 |
| 最近记录: |