public enum SharpShooter_4 {
LOCKED("锁定目标"),
AIM("瞄准目标"),
SHOOT("射击");
private String desc; //枚举说明
private SharpShooter_4(String desc){
this.desc=desc;
}
public String getDesc(){
return desc;
}
}
public class TestDemo_4 {
public static void main(String args[]) {
for (SharpShooter_4 enumSS : SharpShooter_4.values()) {
System.out.println(enumSS + " " + enumSS.getDesc());
}
}
}
emnu还能继承接口,抽象方法
Season.SPRING.name()可以得到该对象的字符串,即“SPRING”;反之,由Season.valueOf(“SPRING”)则可以得到该对象,即Season.SPRING。
enum Season {
SPRING, SUMMER, AUTUMN, WINTER;
public static Season valueOf(int ordinal) {
if (ordinal < 0 || ordinal >= values().length) {
throw new IndexOutOfBoundsException("Invalid ordinal");
}
return values()[ordinal];
}
}