如何将数组传递给枚举中的构造函数?

时间:2014-12-03 06:55:51

标签: java arrays enums

我正在制作口袋妖怪类型的枚举。每种类型都有力量,弱点和免疫类型。我试图将类型传递到不同的数组中,这些数组存储了每种类型的强弱。

我知道我可以将数组定义为

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,说"非法开始表达"。

如果不是这样,我怎样才能将数组传递给枚举的构造函数?

2 个答案:

答案 0 :(得分:3)

您必须使用new

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

答案 1 :(得分: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并不熟悉,但我会尝试如何使用它。类似的东西:

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