enum究竟在开发中是怎么用的,怎么没发现有什么方便的呢,请赐教!

解决方案 »

  1.   

    枚举(enum)是一些值的集合,可以约束某些特定的值不能随意改变
      

  2.   

    类型安全的常量
    单例
    switch
    ...
      

  3.   

    类型安全的常量
    单例
    switch
    ...
      

  4.   


    public enum Education {
    BACHELOR(1,"本科"),
    MASTER(2,"硕士"),
    DOCTOR(3,"博士");
    private int num;
    private String name;
    Education(int num,String name){
    this.num = num;
    this.name = name;
    } public static String getName(Integer num){
    if(num == null){
    return "无";
    }
    switch(num){
    case 1:
    return "本科";
    case 2:
    return "硕士";
    case 3:
    return "博士";
    default:
    return "无";
    }
    }
    }这是我的代码,感觉不如把它放在一个Map中,通过key取值HashMap<Integer,String> education = new HashMap<Integer,String>();
    education.put(1,"本科");
    education.put(2,"硕士");
    education.put(3,"博士");System.out.println(education.get(1));