Java基础之枚举类型Enum的使用

Java基础之枚举类型Enum的使用

定义
public enum AccruedCleanEnum {

   SPREAD("1","发票"),OTHER("0","收入"),NON_BUSINESS("2","支出");

    private String index;
private String name; AccruedCleanEnum(String index, String name) {
this.index = index;
this.name = name;
} public static String getName(String index){ for(AccruedCleanEnum accruedCleanEnum : AccruedCleanEnum.values()){
if(accruedCleanEnum.getIndex().equals(index)){
return accruedCleanEnum.name;
}
}
return "";
} public String getIndex() {
return index;
} public String getName() {
return name;
} }
样例
/**
* Test <br>
* 〈〉
*
* @author XiaoQiang
* @create 2019-7-14
* @since 1.0.0
*/
public class Test { @org.junit.Test
public void Test(){ AccruedCleanEnum.OTHER.getIndex();//获取键
AccruedCleanEnum.OTHER.getName(); //获取值
}
}
上一篇:【转】Java基础笔记 – 枚举类型的使用介绍和静态导入--不错


下一篇:C参数计算