磨刀不誤砍柴工

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

             ::  ::  ::  :: 管理
          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 閱讀(375) 評論(0)  編輯  收藏 所屬分類: Java

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 内乡县| 阿合奇县| 都兰县| 雷州市| 五峰| 乌拉特前旗| 成武县| 大新县| 苗栗县| 文昌市| 莒南县| 吉水县| 绥德县| 宜黄县| 东乡县| 广灵县| 资中县| 文成县| 纳雍县| 靖安县| 平罗县| 崇文区| 绩溪县| 雅江县| 左权县| 兰溪市| 东丰县| 聊城市| 监利县| 咸丰县| 阿图什市| 叶城县| 伊宁县| 武定县| 老河口市| 应城市| 凌源市| 江北区| 临武县| 岢岚县| 天门市|