枚举类基本使用

枚举类的定义

 1 package FactoryPattern;
 2 
 3 public enum Season {
 4 
 5     MON(1,"mon"),
 6     TUS(2,"tus"),
 7     WED(3,"wed"),
 8     THU(4,"thu"),
 9     FRI(5,"fri"),
10     SAT(6,"sat"),
11     SUN(7,"sun");
12 
13     private int value;
14     private String label;
15 
16     private Season(int value,String label){
17         this.value = value;
18         this.label = label;
19     }
20 
21     public int getValue() {
22         return value;
23     }
24 
25     public void setValue(int value) {
26         this.value = value;
27     }
28 
29     public String getLabel() {
30         return label;
31     }
32 
33     public void setLabel(String label) {
34         this.label = label;
35     }
36 }

枚举类的使用

 1 import FactoryPattern.Season;
 2 import FactoryPattern.Shape;
 3 import FactoryPattern.ShapeFacotory;
 4 
 5 public class Client {
 6     public static void main(String[] args) {
 7         System.out.println("值:"+Season.MON);
 8         System.out.println("自定义属性:"+Season.MON.getValue()+"-"+Season.MON.getLabel());
 9         System.out.println("默认编号"+Season.MON.ordinal());//默认编号
10 
11         System.out.print("循环:");
12         for (Season s:Season.values()){
13             System.out.print(s+" ");
14         }
15         System.out.println();
16 
17         System.out.println("比较次序:"+Season.MON.compareTo(Season.SAT));// -5比较默认次序
18 
19         System.out.println(Season.valueOf("mon".toUpperCase()));//MON
20 //        System.out.println(Season.valueOf("mon"));//No enum constant FactoryPattern.Season.mon
21     }
22 }

 

上一篇:枚举


下一篇:枚举类和注解