码迷,mamicode.com
首页 > 编程语言 > 详细

enum的java例子

时间:2016-04-30 12:51:31      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

Grade.java

 1 package com.zhiyuan.myenum;
 2 
 3 /**
 4  * <p>Title: 枚举类
 5  * <p>Company: 
 6  * @description Enum
 7  * @author LIANG ZHIYUAN
 8  * @date 2016年4月7日下午11:38:25
 9  * @version 1.0
10  */
11 public enum Grade {
12     
13     // 枚举值
14     //A("100-90"),B("89-75"),C("74-60"),D("59-0");
15     
16     A("100-90"){
17         @Override
18         public String localValue() {
19             return "优";
20         }
21     },
22     B("89-75"){
23         @Override
24         public String localValue(){
25             return "良";
26         }
27     },
28     C("74-60"){
29         @Override
30         public String localValue(){
31             return "中";
32         }
33     },
34     D("59-0"){
35         @Override
36         public String localValue(){
37             return "差";
38         }
39     };
40     
41     /**
42      * 成绩值
43      */
44     private String value;
45     
46     /**
47      * 私有(必需)构造函数
48      * @param value 传入的值
49      */
50     private Grade(String value){
51         this.value=value;
52     }
53     
54     /**
55      * 得到值
56      * @return57      */
58     public String getValue(){
59         return this.value;
60     }
61     
62     /**
63      * 得到值
64      * @return65      */
66     public abstract String localValue();
67     
68 }

GradeTest.java

 1 package com.zhiyuan.myenum.test;
 2 
 3 import org.junit.Test;
 4 
 5 import com.zhiyuan.myenum.Grade;
 6 
 7 public class GradeTest {
 8 
 9     public static void print(Grade grade){
10         String value=grade.localValue();
11         System.out.println(value);
12     }
13 
14     @Test
15     public void testGrade() {
16         print(Grade.C);
17     }
18 
19 }

 


Week.java

 1 package com.zhiyuan.myenum;
 2 
 3 /**
 4  * <p>Title: 枚举类
 5  * <p>Company: 
 6  * @description Enum
 7  * @author LIANG ZHIYUAN
 8  * @date 2016年4月8日下午2:30:46
 9  * @version 1.0
10  */
11 public enum Week {
12     // 枚举值
13      Mon("Monday",1){
14         @Override
15         public String localValue() {
16             return "星期一";
17         }
18      },
19      Tue("Tuesday",2){
20          @Override
21         public String localValue() {
22             return "星期二";
23         }
24      },
25      Wed("Wednesday",3){
26          @Override
27         public String localValue() {
28             return "星期三";
29         }
30      },
31      Thu("Thursday",4){
32          @Override
33         public String localValue() {
34             return "星期四";
35         }
36      },
37      Fri("Friday",5){
38          @Override
39         public String localValue() {
40             return "星期五";
41         }
42      },
43      Sat("Saturday",6){
44          @Override
45         public String localValue() {
46             return "星期六";
47         }
48      },
49      Sun("Sunday",7){
50          @Override
51         public String localValue() {
52             return "星期日";
53         }
54      };
55      /**
56      * 星期数
57      */
58     private String value;
59      /**
60      * 索引
61      */
62     private Integer index;
63      private Week(String value,Integer index){
64          this.value=value;
65          this.index=index;
66      }
67      public String getValue(){
68          return this.value+"_"+this.index;
69      }
70      public abstract String localValue();
71      @Override
72     public String toString() {
73         return value+"_"+index;
74     }
75 }

WeekTest.java

 1 package com.zhiyuan.myenum.test;
 2 
 3 import org.junit.Test;
 4 
 5 import com.zhiyuan.myenum.Week;
 6 
 7 public class WeekTest {
 8     
 9     
10 
11     @Test
12     public void testWeek() {
13         print(Week.Sat);
14         //loopPrint();
15     }
16 
17     public void print(Week week) {
18         //String value=week.getValue();
19         String value=week.localValue();
20         System.out.println(value);
21     }
22     
23     public void loopPrint(){
24         for(Week week:Week.values()){
25             System.out.println(week.toString());
26         }
27     }
28 
29 }

enum的java例子

标签:

原文地址:http://www.cnblogs.com/LEARN4J/p/5448425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!