简体   繁体   English

Java - 非静态枚举

[英]Java - Non-static Enums

Say, I have some classes: 说,我有一些课程:

class NPC {
    Attributes attributes;
    Skills skills;

    void doStuff() { //doStuffff }
}

class Player {
    Attributes attributes;
    Skills skills;

    void doStuff() { //doStuffff }
}

And an Enum for these classes: 这些课程的枚举:

enum Attributes {
STRENGTH, INTELLIGENCE, AGILITY...//etc
}

enum Skills {
GUNS, MELEE, ATHLETICS...//etc
}

Now, let's say I wanted to make it so that each instance of either class Player or NPC could actually have their own separate instance of Attributes or Skills which could add and subtract values of the enums themselves. 现在,假设我想这样做,以便Player类或NPC类的每个实例都可以拥有自己独立的Attributes或Skills实例,它们可以添加和减去枚举本身的值。 Is this possible? 这可能吗? If not, would it be possible to use methods within an innter nested enum to manipulate the field values of a class it resides in? 如果没有,是否可以使用内部嵌套枚举中的方法来操作它所驻留的类的字段值?

This may sound somewhat insane, ridiculous, and just down right premadonnic, but when you have to find the right design solution...well, sometimes you just have to think outside the box. 这可能听起来有点疯狂,荒谬,而且恰到好处,但是当你必须找到合适的设计解决方案时......好吧,有时你只需要在盒子外思考。

Edit: 编辑:

I found an interesting solution, though I doubt it's as good as the map idea. 我找到了一个有趣的解决方案,但我怀疑它和地图的想法一样好。

I figured it'd just be interesting to post :D 我觉得发帖很有意思:D

public final class StatControl {

    int agility;
    int endurance;
    int intelligence;
    int intuition;
    int luck;
    int speed;
    int strength;

}

enum Attributes {
    AGILITY {
        void add(int value) {
            test.agility += value;
        }
    },
    ENDURANCE {},
    INTELLIGENCE {},
    INTUITION {},
    LUCK {},
    SPEED {},
    STRENGTH {};

    abstract void add(int value);

    StatControl test;

    void receiveObj(StatControl test) {
        this.test = test;
    }

}

As you can see, a class holds an integer representation of the enums, but the enum itself acts as a control center which can manipulate whatever instance of the StatControl object passed to it. 如您所见,类包含枚举的整数表示,但枚举本身充当控制中心,可以操纵传递给它的StatControl对象的任何实例。

Maps are probably better though - I could see where this could easily get messy. 虽然地图可能更好 - 我可以看到这可能很容易变得混乱。

On further consideration, I think that perhaps the answer to your problem is to use the same enums for all but different EnumMaps for your different classes if you want each enum to have a different value of some sort associated with it. 在进一步考虑时,我认为你的问题的答案可能就是如果你希望每个枚举具有与之相关的某种不同的值,则为不同的类使用相同的枚举,但不同的EnumMaps。 If this answer is way out in left field, sorry, but then please clarify your question. 如果这个答案在左栏中显示出来,抱歉,那么请澄清你的问题。 :) :)

For more on EnumMaps, please look here: 有关EnumMaps的更多信息,请查看此处:
Taming Tiger: Beyond the basics of enumerated types 驯服老虎:超越枚举类型的基础知识
EnumMap API EnumMap API

You can use EnumMaps like 你可以像使用EnumMaps一样

abstract class Actor {
    final Map<Attribute, Integer> attributes = new EnumMap<Attribute, Integer>();
    final Map<Skill, Integer> skills = new EnumMap<Attribute, Integer>();

    abstract void doStuff();
}

class NPC extends Actor {
    void doStuff() { }
}

class Player extends Actor {
    void doStuff() { }
}

You can add attributes to every instance of an Enum. 您可以向Enum的每个实例添加属性。 See the Planets example: 请参阅行星示例:

http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM