public enum Direction{initial, up, down}
public static void main(String[] args) {
for (Direction d: Direction.values())
System.out.println(d.name());
// EnumSet
EnumSet eSet = EnumSet.allOf(Direction.class);
for (Direction d: eSet)
System.out.println(d.name());
// EnumMap
EnumMap eMap = new EnumMap(Direction.class);
eMap.put(Direction.initial, "to be expanded");
eMap.put(Direction.up, "go upwards");
eMap.put(Direction.down, "go downwards");
for (Direction d: Direction.values())
System.out.println(d.name() + ": " + eMap.get(d));
// assignment
Direction d = Direction.initial;
// in switch
switch(d) {
case initial:
...
case up:
...
case down:
...
}
}
1. Enum can be used instead of public static final constants sometimes...
2. Enum can not extend or be extended...
没有评论:
发表评论