2014-12-03 137 views
0

我正在做一個口袋妖怪類型的枚舉。每種類型都具有強度,弱點和免疫力類型。我試圖將類型傳遞到不同的數組中,這些數組存儲每種類型的優點和缺點。如何將數組傳遞給枚舉中的構造函數?

我知道我可以定義數組作爲

int [] x = {1, 2, 3, 88}; 

而且我試過同爲傳遞一個arrray: -

// For the sake of simplicity, all types have the same parameters for now... 
    public enum Type { 

    NORMAL ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    FIGHTING ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    FLYING ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    POISON ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    GROUND ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    ROCK ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    BUG ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    GHOST ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    STEEL ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    FIRE ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    WATER ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    GRASS ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    ELECTRIC ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    PHYSIC ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    ICE ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    DRAGON ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    DARK ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}), 
    FAIRY ({Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}); 

    private Type [] weak, strong, ineffective; 

    Type (Type [] weak, Type [] strong, Type [] ineffective) { 

     this.weak = weak; 
     this.strong = strong; 
     this.ineffective = ineffective; 

    } 

} 

我的IDE,BlueJ的,說:「非法啓動表達」。

如果不是這樣,我怎麼能將數組傳遞給枚舉的構造函數?

+1

你可能應該看一下'EnumSet' – 2014-12-03 06:59:06

回答

2

你必須使用new

new Type[]{Type.STEEL, Type.PHYSIC} 
1

這是由@Ruslan Ostafiychuk說,但我想澄清他是什麼意思。將這些集合定義爲Type []將解決您的問題。要做到這一點,你需要更改您的代碼:

// For the sake of simplicity, all types have the same parameters for now... 
    public enum Type { 

    NORMAL (new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}), 
    FIGHTING (new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}), 
    FLYING (new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}), 
    . 
    . 
    . 
    FAIRY (new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}); 

    private Type [] weak, strong, ineffective; 

    Type (Type [] weak, Type [] strong, Type [] ineffective) { 

     this.weak = weak; 
     this.strong = strong; 
     this.ineffective = ineffective; 

    } 

} 

此外,觸及enumsets的角度來看,它似乎像使用這可以幫助您組織的發展。我不太熟悉enumsets,但我會採取一個刺戳你如何使用它。例如:

EnumSet<Type> normal = EnumSet.of(Type.NORMAL.weak, type.NORMAL.strong, type.NORMAL.ineffective);