2015年9月18日 星期五

Enum 使用方式

Java 的 Enum:
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);
        
    }
}