好处:

1.可以直接使用switch

2.可以实现toString()方法

 

笔记:

1.枚举类头部定义的成员变量,可以看做是枚举类的一个实例

 

public enum Color { 
        RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);         // 成员变量

        private String name; 
        private int index;  
        // 构造方法 
        private Color(String name, int index) {             this.name = name;             this.index = index;         }  
        // 普通方法 
        public static String getName(int index) {             for (Color c : Color.values()) {                 if (c.getIndex() == index) {                     return c.name;                 }             } 
            return null;         }  
        // get set 方法 
        public String getName() {             return name;     }
}   

 

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2022-01-16
  • 2021-12-11
  • 2022-01-10
  • 2021-04-06
猜你喜欢
  • 2021-07-10
  • 2021-05-17
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案