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