Java之枚举类范例
代码如下:
1 package catf.component.http.model; 2 3 /** 4 * @Auther:gongxingrui 5 * @Date:2018-04-17 6 * @Description: 规范结果匹配的类型 7 **/ 8 public enum CheckType { 9 EQUALS("equals", 1), 10 CONTAINS("contains", 2), 11 REGEX("regex", 3); 12 13 private String value; 14 private int id; 15 16 private CheckType(String value, int id) { 17 this.value = value; 18 this.id = id; 19 } 20 21 public String getValue() { 22 return value; 23 } 24 25 public int getId() { 26 return id; 27 } 28 29 @Override 30 public String toString() { 31 return "CheckType{" + 32 "value='" + value + '\'' + 33 ", id=" + id + 34 '}'; 35 } 36 }
测试类:
1 package catf.component.http.model; 2 3 import org.junit.Test; 4 5 public class TestCheckType { 6 @Test 7 public void test() { 8 System.out.println("equals:" + CheckType.EQUALS.getId() + " " + CheckType.EQUALS.getValue()); 9 CheckType[] checkTypes = CheckType.values(); 10 for (CheckType c : checkTypes) { 11 System.out.print(c.toString() + "、"); 12 } 13 CheckType c = CheckType.CONTAINS; 14 System.out.println("\n" + c.getValue()); 15 } 16 }