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(); //获取值
    }
}
posted @ 2019-07-14 20:20  指尖逆苍穹  阅读(7477)  评论(0编辑  收藏  举报
AmazingCounters.com