public enum PowerState { OFF("This System is off"), ON("This System is on"), SUSPEND("This System is Suspend"); private String description; private PowerState(String d){ description = d; } public String getDescription(){ return this.description; } }
測試一下:
public class Computer { void setState(PowerState powerState) { switch(powerState){ case OFF: System.out.println(powerState.getDescription()); break; case ON: System.out.println(powerState.getDescription()); break; case SUSPEND: System.out.println(powerState.getDescription()); break; } } }
public class Demo { public static void main(String[] args){ Computer comp = new Computer(); comp.setState(PowerState.OFF); } }