Emun 简单实例

public enum PlayerType {
    ANONYMOUS(1, "AnonymousPatron"),
    PATRON(2, "Patron"),
    UNREGISTERED(3, "UnregisteredPlayer");
   
    private int code;
    private String description;

    private static final Map<Integer, PlayerType> codeToEnum = new HashMap<Integer, PlayerType>();
   
    static {
        for (PlayerType PlayerType : values()) {
            codeToEnum.put(PlayerType.getCode(), PlayerType);
        }
    }

    PlayerType(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public static PlayerType fromCode(int code){
        return codeToEnum.get( code );
    }
}

你可能感兴趣的:(实例)