java枚举应用

枚举实现常量映射,应用场景:字典

public enum EnumDemo {
STOCK("1",""),DEBT("2",""),FORWARD("3",""),COMPL("4","),
OTHER("5","");
private String name;
private String index;
// 构造方法
private EnumDemo(String index, String name) {
this.name = name;
this.index = index;
}

public static String getName(String index){
for(EnumDemo c:EnumDemo .values()){
if(c.index.equals(index)){
return c.name;
}
}
return null;
}
}

 

枚举定义以实现不同的方法,应用场景:版本控制

public enum EnumTest {
MON(1), TUE(2), WED(3), THU(4), FRI(5), SAT(6) {
@Override
public boolean isRest() {
return false;
}
},
SUN(0) {
@Override
public boolean isRest() {
return true;
}
};

private int value;

private EnumTest(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public boolean isRest() {
return false;
}
public static void main(String[] args) {
System.out.println("EnumTest.FRI 的 value = " + EnumTest.FRI.getValue()+" "+EnumTest.FRI.isRest());
}
}
posted @ 2017-06-29 13:11  cindy_zhu  阅读(186)  评论(0编辑  收藏  举报