磨刀不误砍柴工

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

   ::  ::  ::  :: 管理
Enum

 1 package com.study.enums;
 2 
 3 
 4 
 5 public enum SexEnum {
 6     FEMAIL("00"),
 7     MAIL("01");
 8     
 9     private String value;
10     private SexEnum(String value) {
11         this.value = value;
12     }
13     
14     public String getValue(){
15         return value;
16     }
17     
18     public static SexEnum fromValue(String value){
19         SexEnum[] enums = SexEnum.values();
20         
21         if(enums!=null){
22             for(SexEnum item:enums){
23                 if(item.getValue().equals(value))
24                     return item;
25             }
26         }
27         
28         return null;
29     }
30 }
31 

 1 package com.study.enums;
 2 
 3 import junit.framework.Assert;
 4 
 5 import org.junit.Test;
 6 
 7 public class SexEnumTest {
 8 
 9     @Test
10     public void testGetValue() {
11         System.out.println(SexEnum.FEMAIL.getValue());
12         
13         System.out.println(SexEnum.MAIL.getValue());
14         
15         System.out.println(SexEnum.fromValue("00").getValue());
16         
17         System.out.println(SexEnum.fromValue("01").getValue());
18         
19         
20         Assert.assertEquals(SexEnum.FEMAIL, SexEnum.fromValue("00"));
21         
22         Assert.assertEquals(SexEnum.MAIL, SexEnum.fromValue("01"));
23     }
24 
25 }
26 


结果:

00
01
00
01


posted on 2011-01-13 11:47 liwei5891 阅读(355) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: