1.枚举无变量参数
public enum SeasonEnum{ SPRING,SUMMER,AUTUMN,WINTER; } public void test() { SeasonEnum se = SeasonEnum.SPRING; switch(se) { case SPRING: System.out.println("spring"); break; case SUMMER: System.out.println("summer"); break; case AUTUMN: System.out.println("autumn"); break; case WINTER: System.out.println("winter"); break; } }
2.只传一个值
public enum SeasonEnum{ SPRING("1"),SUMMER("2"),AUTUMN("3"),WINTER("4"); private String val; SeasonEnum (String val){ this.val = val; } public String getVal() { return val; } } public void test() { SeasonEnum se = SeasonEnum.SUMMER; System.out.println(se.getVal()); // 2 }
3.传入键值
public enum SeasonEnum{ SPRING(0,"spring"),SUMMER(1,"summer"),AUTUMN(2,"autumn"),WINTER(3,"winter"); private int key; private String val; public int getKey() { return key; } public String getVal() { return val; } SeasonEnum (int key, String val){ this.key = key; this.val = val; } } public void test() { SeasonEnum se = SeasonEnum.WINTER; System.out.println(se.getKey()+" -> " + se.getVal()); // 3 -> winter }