枚举的常用方法:ordinal(下标) ,values(遍历)
枚举与witch组合
package com.yjq.cn.initialize;
public class EnumTest {
//辣
public enum Spiciness {
// 柔和 中等 炽烈
NOT, MILD, MEDIUM, HOT, FLAMING
}
public static void main(String[] args) {
//遍历与ordinal方法
for(Spiciness s : Spiciness.values()) {
System.out.println(s + ", ordinal " + s.ordinal());
}
System.out.println("------------------");
new EnumTest().describe(Spiciness.MILD);
}
/**
* 与switch结合使用
*/
public void describe(Spiciness d) {
System.out.print("[switch] this degree is ");
switch(d) {
case NOT :
System.out.println("not spick at all.");
break;
case MILD :
case MEDIUM : System.out.println("a little hot.");
break;
case HOT :
case FLAMING :
default : System.out.println("maybe too hot.");
}
}
}
//output
NOT, ordinal 0
MILD, ordinal 1
MEDIUM, ordinal 2
HOT, ordinal 3
FLAMING, ordinal 4
------------------
[switch] this degree is a little hot.