Java 小例子:如何使用 Enum

Java 在 1.5 以前,常量是个头疼的问题。因为一些参数只能取那么几个值,而从参数类型上又看不出来有哪些值可以取。从 1.5 开始 Java 引进了 Enum 枚举类型,并根据 Java 自身的特点将其进行了强化。实际上现在所有的常量都可以通过 Enum 来定义了。下面是一个例子,演示了 Enum 的基本用法,以及如何向 Enum 类型加入自定义属性。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  import  java.util.Calendar;
 2   
 3  /**
 4   * 演示如何使用 Enum
 5    */
 6  public   class  EnumValue {
 7   
 8       public   static   void  main(String[] args) {
 9   
10           //  演示如何使用 Weekday
11          Weekday w  =  Weekday.Monday;
12          System.out.println( " w =  "   +  w.toString());
13          System.out.println( " Monday compares to Friday =  "
14                   +  Weekday.Monday.compareTo(Weekday.Friday));
15   
16          System.out.println( " Enum value of \ " Sunday\ "  is  "   +  Weekday.valueOf( " Sunday " ));
17   
18           try  {
19              System.out.println( " Enum value of \ " AnotherDay\ "  is  "
20                       +  Weekday.valueOf( " AnotherDay " ));  //  这里将会抛出 IllegalArgumentException
21          }  catch  (IllegalArgumentException e) {
22              System.out.println( " exception:  "   +  e.getMessage());
23          }
24          System.out.println();
25   
26           //  --------------------------------
27           //  演示如何使用 Month
28          System.out.println( " The first month is  "   +  Month.Jan);
29          System.out.println( " The full name of the first month is  "   +  Month.Jan.getFullName());
30          System.out.println( " The fifth month is  "   +  Month.valueByIndex( 5 ).getFullName());
31          System.out.println( " Index of August is  "   +  Month.valueByFullName( " August " ).getIndex());
32          System.out.println( " Now is  "   +  Month.thisMonth().getFullName());
33      }
34  }
35   
36  /**
37   * 一个表示星期几的 enum (这是一个简单的例子)
38    */
39  enum  Weekday {
40      Monday, Tuesday, Wednesday, Thirsday, Friday, Saturday, Sunday
41  }
42   
43  /**
44   * 一个表示月份的 enum (这是一个稍微复杂的例子)
45    */
46  enum  Month {
47   
48       //  十二个月份
49      Jan( " January " ),  Feb( " Febrary " ),   Mar( " March " ),
50      Apr( " April " ),    May( " May " ),       June( " June " ),
51      July( " July " ),    Aug( " August " ),    Sept( " September " ),
52      Oct( " October " ),  Nov( " November " ),  Dec( " December " );
53   
54       //  全名
55       private  String fullName;
56   
57       //  构造方法
58      Month(String fullName) {
59           this .fullName  =  fullName;
60      }
61   
62       public  String getFullName() {
63           return  fullName;
64      }
65   
66       //  获取当前 Enum 值是第几个月份。一月份则返回 1。
67       public   int  getIndex() {
68           return  ordinal()  +   1 ;
69      }
70   
71       //  根据月数获得 enum 对象
72       public   static  Month valueByIndex( int  index) {
73           for  (Month month : Month.values()) {
74               if  (month.getIndex()  ==  index) {
75                   return  month;
76              }
77          }
78           return   null ;
79      }
80   
81       //  根据全名获得 enum 对象
82       public   static  Month valueByFullName(String fullName) {
83           for  (Month month : Month.values()) {
84               if  (month.getFullName().equals(fullName)) {
85                   return  month;
86              }
87          }
88           return   null ;
89      }
90   
91       public   static  Month thisMonth() {
92           //  Calendar 的月份从 0 算起,而 Month 的月份从 1 算起
93           int  index  =  Calendar.getInstance().get(Calendar.MONTH)  +   1 ;
94           return  valueByIndex(index);
95      }
96  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值