English 中文(简体)
Java enums: 收集来自另一层的胎儿
原标题:Java enums: Gathering info from another enums
  • 时间:2010-03-23 23:00:56
  •  标签:
  • java
  • enums
最佳回答
问题回答

我认为,最好将此类财产储存在大概情况下,即本身。

public enum Animal {
  DOG(NOT_GRAY),
  ELEPHANT(GRAY),
  WHALE(GRAY),
  SHRIMP(NOT_GRAY),
  BIRD(NOT_GRAY),
  GIRAFFE(NOT_GRAY);

  private static boolean GRAY = true;
  private static boolean NOT_GRAY = !GRAY;

  private Animal(boolean isGray) {
    // snip
  }
}

你们甚至可以将几处羊毛的生物编码为一种沥青(而不是使用BitSet);

public enum Animal {
  DOG(),
  ELEPHANT(GRAY | BIG),
  WHALE(GRAY | BIG),
  SHRIMP(),
  BIRD(),
  GIRAFFE(BIG);

  private static byte GRAY = 0x01;
  private static byte BIG = GRAY << 1;

  private final byte _value;

  private Animal() {
    this(0x00);
  }

  private Animal(byte value) {
    _value = value;
  }

  public boolean isGray() {
    return _value & GRAY != 0x00;
  }

  public boolean isBig() {
    return _value & BIG != 0x00;
  }
}

然而,只是这样做:

public class GrayAnimal {
  public static final Animal ELEPHANT = Animal.ELEPHANT;
  public static final Animal WHALE = Animal.WHALE;
}

或类似于

public enum Animal {
  DOG,
  ELEPHANT,
  WHALE,
  SHRIMP,
  BIRD,
  GIRAFFE;

  // thanks to Mihir, I would have used a regular HashSet instead
  public static final Set<Animal> GRAY = Collections.unmodifiableSet(EnumSet.of(ELEPHANT, WHALE));
}

如果你需要区分你的代码版面中的物体,除非这些物体可以按代码分类,否则就毫无用处。

这一点很重要,因为你正在将内容引入你的软件中,从长远来看,这些软件将变成坏的代码。

例如,除发言如:

if(critter.type == WHALE)
    critter.movement=WATER;
else if(critter.type == ELEPHANT)

这应即刻提醒任何联络处方案管理员-证人,他们都是坏的代码轮).,因为他们几乎总是显示联络处设计不良。

另一种选择是,建立一套最后的物体,最初采用数据,最好是不使用密码。

你可能会有itter鱼的特性,如鲸鱼的特性,则会使用水处理器,而ele鱼则含有并使用土地运动。

总的来说,在业务处,方案拟定工作,而不是使用开关和 en,将造成大量编码的瘫痪。

每当你写一个方法时,都会记住门话。 “没有要求标的,然后在标的上操作,而是要求标的为你操作”。

我不知道,你为什么想把它放在另一个头上,你可以把它放在这一职能上:

public boolean isGray() {
     return this == WHALE || this == ELEPHANT;
}

也许可以这样说:

package p;
import java.util.*;
enum Type {
    small,big,grey;
}
enum Animal {
    bird(EnumSet.of(Type.small)),whale(EnumSet.of(Type.big, Type.grey)),elephant(EnumSet.of(Type.big, Type.grey));
    Animal(final EnumSet<Type> types) { this.types=types; }
    EnumSet<Type> types=EnumSet.noneOf(Type.class);
    boolean is(final Type type) { return types!=null?types.contains(type):false; }
    public static void main(String[] arguments) {
        for(Animal a:values()) {
            System.out.println(a+" "+a.types);
        }
    }
}

我认为,最好不要以这种分类来污染你。 最好把这几类人从头上解脱出来,以便你能够更晚地补充,而不影响你的头脑。 这遵循了对班级设计的关切和单一责任原则。

为了做到这一点,仅使用EnumSet来控制这种情况,即:

public enum Animal {
  DOG,
  ELEPHANT,
  WHALE,
  SHRIMP,
  BIRD,
  GIRAFFE;
}

public static final EnumSet<Animal> GRAY_ANIMALS = EnumSet.of(ELEPHANT, WHALE);

如果你想增加功能,而不是简单成员,或想让更多的yn糖延伸到EnumSet

public class GrayAnimals extends EnumSet<Animal> {
    public static final GrayAnimals INSTANCE = new GrayAnimals(ELEPHANT, WHALE);
    private GrayAnimals(Animal...animals) {
        Collections.addAll(this, animals);
    }
    public boolean isGray(Animal animal) { return contains(animal); }
    // ... other methods
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签