关键字: java enum 在像C这样强调数据结构的语言里,枚举是必不可少的一种数据类型。然而在java的早期版本中,是没有一种叫做enum的独立数据结构的。所以在以前的java版本中,我们经常使用interface来simulate一个enum。
java 代码
public interface Color {
static int RED = 1;
static int GREEN = 2;
static int BLUE = 3;
}
虽然这种simulation比较麻烦,但在以前也还应付的过去。可是随着java语言的发展,越来越多的呼声要求把enum这种数据结构独立出来,加入到java中。所以从java 1.5以后,就有了enum,这也是这篇blog要学习的topic。
学习的最好方式就是例子,先来一个:
java 代码
public class EnumDemo {
private enum Color {red, blue, green}//there is not a ";"
public static void main(String[] args) {
for(Color s : Color.values()) {
//enum的values()返回一个数组,这里就是Seasons[]
System.out.println(s);
}
}
}
console results
red
blue
green
注意事项已经在code中注释出,还要说明一点的是,这个java文件编译完成后不只有一个EnumDemo.class,还会有一个EnumDemo$Seasons.class,奇怪吧!
Another e.g.
java 代码
public class EnumDemo {
private enum Color {red, blue, green}//there is not a ";"
public static void main(String[] args) {
Color s = Color.blue;
switch (s) {
case red://notice: Seasons.red will lead to compile error
System.out.println("red case");
break;
case blue:
System.out.println("blue case");
break;
case green:
System.out.println("green case");
break;
default:
break;
}
}
}
这个例子要说明的就是case的情况。
就这么多吗,当然不是,我们的enum结构还可以定义自己的方法和属性。
java 代码
public class EnumDemo {
private enum Color {
red, blue, green;//there is a ";"
//notic: enum's method should be "static"
public static Color getColor(String s){
if(s.equals("red flag")){
return red;
} else if(s.equals("blue flag")){
return blue;
} else {
return green;
}
}
}//there is not ";"
public static void main(String[] args) {
EnumDemo demo = new EnumDemo();
System.out.println(demo.getFlagColor("red flag"));
}
public Color getFlagColor(String string){
return Color.getColor(string);
}
}
Ok,so much for enum. Isn't it simple?.
本文摘自IBM DW,如有转载,请声明!
枚举类型入门
----用 Java 5.0 以类型安全的方式表示常量
Tiger 中的一个重要新特性是枚举构造,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。Tiger 专家、developerWorks 的多产作者 Brett McLaughlin 将解释枚举的定义,介绍如何在应用程序中运用枚举,以及它为什么能够让您抛弃所有旧的 public static final 代码。
您已经知道,Java 代码的两个基本的构造块是 类和 接口。现在 Tiger 又引入了 枚举,一般简称它为 enum。这个新类型允许您表示特定的数据点,这些数据点只接受分配时预先定义的值集合。
当然,熟练的程序员可以用静态常量实现这项功能,如清单 1 所示:
清单 1. public static final 的常量
public class OldGrade {
public static final int A = 1;
public static final int B = 2;
public static final int C = 3;
public static final int D = 4;
public static final int F = 5;
public static final int INCOMPLETE = 6;
}
摘者注:下面这段话说明了上述编程的弊端(除了这里所说的内容外,摘者认为如果OldGrade类中只有这些常量的话,应该将其定义为接口,这样可以防止错误的实例化这个类)
然后您就可以让类接受像 OldGrade.B 这样的常量,但是在这样做的时候,请记住这类常量是 Java 中 int 类型的常量,这意味着该方法可以接受任何int类型的值,即使它和 OldGrade 中定义的所有级别都不对应。因此,您需要检测上界和下界,在出现无效值的时候,可能还要包含一个 IllegalArgumentException 。而且,如果后来又添加另外一个级别(例如 OldGrade.WITHDREW_PASSING ),那么必须改变所有代码中的上界,才能接受这个新值。
换句话说,在使用这类带有整型常量的类时,该解决方案也许可行,但并不是非常有效。幸运的是,枚举提供了更好的方法。
定义枚举
清单 2 使用了一个可以提供与清单 1 相似的功能的枚举:
清单 2. 简单的枚举类型
package com.oreilly.tiger.ch03;
public enum Grade {
A, B, C, D, F, INCOMPLETE
};
在这里,我使用了新的关键字 enum ,为 enum 提供了一个名称,并指定了允许的值。然后, Grade 就变成了一个 枚举类型,您可以按清单 3 所示的方法使用它:
清单 3. 使用枚举类型
package com.oreilly.tiger.ch03;
public class Student {
priv
ate Grade grade;
public void assignGrade(Grade grade) {
this.grade = grade;
}
public Grade getGrade() {
return grade;
}
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.A);
System.out.println(std.getGrade());
}
}
用以前定义过的类型建立一个新的枚举( grade )之后,您就可以像使用其他成员变量一样使用它了。当然,枚举只能分配枚举值中的一个(例如, A 、 C 或 INCOMPLETE )。而且,在 assignGrade() 中是没有进行错误检测的代码,也没有考虑边界情况,请注意这是如何做到。
(摘者注:摘者将上面的使用枚举类型的类代码进行了修改,将一些设置学生姓名等内容删除,并将构造函数也删除,只留下对说明枚举类型有作用的代码,并添加 了main方法来进行grade的设置和获取并打印,程序执行结果为A,由于使用枚举类型在方法assignGrade的参数中定义的是枚举类型,而不是 java的int类型,所以传递的参数必须是枚举类型参数,而枚举类型参数中定义的值都是合法的)。
使用枚举值
迄今为止,您所看到的示例都相当简单,但是枚举类型提供的东西远不止这些。您可以逐个遍历枚举值,也可以在 switch 语句中使用枚举值,枚举是非常有价值的。
遍历枚举值
下面我们用一个示例显示如何遍历枚举类型的值。清单 4 所示的这项技术,适用于调试、快速打印任务以及把枚举加载到集合(我很快将谈到)中的工具:
清单 4. 遍历枚举值
将上面的main方法修改为如下内容:
public static void main(String[] args) {
for (Grade g : Grade.values()) {
System.out.println("Allowed value: '" + g + "'");
}
}
运行这段代码,将得到清单 5 所示的输出:
清单 5. 迭代操作的输出
Allowed Value: 'A'
Allowed Value: 'B'
Allowed Value: 'C'
Allowed Value: 'D'
Allowed Value: 'F'
Allowed Value: 'INCOMPLETE'
这里有许多东西。首先,我使用了 Tiger 的新的 for/in 循环(也叫作 foreach 或 增强的 for )。另外,您可以看到 values() 方法返回了一个由独立的 Grade 实例构成的数组,每个数组都有一个枚举类型的值。换句话说, values() 的返回值是 Grade[] 。
在枚举间切换
能够在枚举的值之间移动很好,但是更重要的是根据枚举的值进行决策。您当然可以写一堆 if (grade.equals(Grade.A)) 类型的语句,但那是在浪费时间。Tiger 能够很方便地把枚举支持添加到过去的好东西 switch 语句上,所以它很容易使用,而且适合您已知的内容。清单 6 向将展示如何解决这个难题:
清单 6. 在枚举之间切换
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.INCOMPLETE);
switch (std.getGrade()) {
case A:
System.out.println("excelled with a grade of A");
break;
case B: // fall through to C
case C:
System.out.println("passed with a grade of "
+ std.getGrade().toString());
break;
case D: // fall through to F
case F:
System.out.println("failed with a grade of "+ std.getGrade().toString());
break;
case INCOMPLETE:
System.out.println("did not complete the class.");
break;
}
}
在这里,枚举值被传递到 switch 语句中(请记住, getGrade() 是作为 Grade 的实例返回的),而每个 case 子句将处理一个特定的值。该值在提供时没有枚举前缀,这意味着不用将代码写成 case Grade.A ,只需将其写成 case A 即可。如果您不这么做,编译器不会接受有前缀的值。
现在,您应该已经了解使用 switch 语句时的基本语法,但是还有一些事情您需要知道。
在使用 switch 之前进行计划
正如您所期待的,在使用枚举和 switch 时,您可以使用 default 语句。清单 7 显示了这个用法:
清单 7. 添加一个 default 块
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.INCOMPLETE);
switch (std.getGrade()) {
case A:
System.out.println("excelled with a grade of A");
break;
case B: // fall through to C
case C:
System.out.println("passed with a grade of "+ std.getGrade().toString());
break;
case D: // fall through to F
case F:
System.out.println("failed with a grade of "
+ std.getGrade().toString());
break;
case INCOMPLETE:
System.out.println("did not complete the class.");
break;
}
default:
System.out.println("has a grade of "+std.getGrade().toString());
break;
}
研究以上代码可以看出,任何没有被 case 语句处理的枚举值都会被 default 语句处理。这项技术您应当坚持采用。原因是:假设 Grade 枚举被您的小组中其他程序员修改(而且他忘记告诉您这件事)成清单 8 所示的版本:
清单 8. 给 Grade 枚举添加一个值
package com.oreilly.tiger.ch03;
public enum Grade {
A, B, C, D, F, INCOMPLETE
,
WITHDREW_PASSING, WITHDREW_FAILING
};
现在,如果使用清单 6 的代码所示的新版 Grade ,那么这两个新值会被忽略。更糟的是,您甚至看不到错误!在这种情况下,存在某种能够通用的 default 语句是非常重要的。清单 7 无法很好地处理这些值,但是它会提示您还有其他值,您需要处理这些值。一旦完成处理,您就会有一个继续运行的应用程序,而且它不会忽略这些值,甚至还会指 导您下一步的动作。所以这是一个良好的编码习惯。
枚举和集合
您所熟悉的使用 public static final 方法进行编码的那些东西,可能已经转而采用枚举的值作为映射的键。如果您不知道其中的含义,请参见清单 9,它是一个公共错误信息的示例,在使用 Ant 的 build 文件时,可能会弹出这样的消息,如下所示:
清单 9. Ant 状态码
package com.oreilly.tiger.ch03;
public enum AntStatus {
INITIALIZING,
COMPILING,
COPYING,
JARRING,
ZIPPING,
DONE,
ERROR
}为每个状态码分配一些人们能读懂的错误信息,从而允许人们在 Ant 提供某个代码时查找合适的错误信息,将这些信息显示在控制台上。这是映射(Map) 的一个绝好用例,在这里,每个映射(Map) 的键都是一个枚举值,而每个值都是键的错误信息。清单 10 演示了该映射的工作方式:
清单 10. 枚举的映射(Map)
public void testEnumMap(PrintStream out) throws IOException {
// Create a map with the key and a String message
EnumMap<AntStatus, String> antMessages =
new EnumMap<AntStatus, String>(AntStatus.class);
// Initialize the map
antMessages.put(AntStatus.INITIALIZING, "Initializing Ant...");
antMessages.put(AntStatus.COMPILING, "Compiling Java classes...");
antMessages.put(AntStatus.COPYING, "Copying files...");
antMessages.put(AntStatus.JARRING, "JARring up files...");
antMessages.put(AntStatus.ZIPPING, "ZIPping up files...");
antMessages.put(AntStatus.DONE, "Build complete.");
antMessages.put(AntStatus.ERROR, "Error occurred.");
// Iterate and print messages
for (AntStatus status : AntStatus.values() ) {
out.println("For status " + status + ", message is: " +
antMessages.get(status));
}
}
该代码使用了泛型(generics)(请参阅 参考资料)和新的 EnumMap 构造来建立新映射。而且,枚举值是通过其 Class 对象提供的,同时提供的还有映射值的类型(在该例中,它只是一个简单的字符串)。该方法的输出如清单 11 所示:
清单 11. 清单 10 的输出
For status INITIALIZING,message is: Initializing Ant...
For status COMPILING, message is: Compiling Java classes...
For status COPYING, message is: Copying files...
For status JARRING, message is: JARring up files...
For status ZIPPING, message is: ZIPping up files...
For status DONE, message is: Build complete.
For status ERROR, message is: Error occurred.
更进一步
枚举也可以与集合结合使用,而且非常像新的EnumMap构造,Tiger 提供了一套新的EnumSet实现,允许您使用位操作符。另外,可以为枚举添加方法,用它们实现接口,定义叫作特定值的类的实体,在该实体中,特定的代码 被附加到枚举的具体值上。这些特性超出了本文的范围,但是在其他地方,有详细介绍它们的文档(请参阅参考资料)。
使用枚举,但是不要滥用
学习任何新版语言的一个危险就是疯狂使用新的语法结构。如果这样做,那么您的代码就会突然之间有 80% 是泛型、标注和枚举。所以,应当只在适合使用枚举的地方才使用它。那么,枚举在什么地方适用呢?一条普遍规则是,任何使用常量的地方,例如目前用 switch代码切换常量的地方。如果只有单独一个值(例如,鞋的最大尺寸,或者笼子中能装猴子的最大数目),则还是把这个任务留给常量吧。但是,如果定 义了一组值,而这些值中的任何一个都可以用于特定的数据类型,那么将枚举用在这个地方最适合不过。
摘者将Sun Java 5.0对于类型安全的Enum的说明拷贝放在下边,以供参考:
In prior releases, the standard way represent an enumerated type was the int Enum pattern: public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL = 3;
This pattern has many problems, such as:
Not typesafe - Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which makes no sense).
No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types.
Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.
It is possible to get around these problems by using the Typesafe Enum pattern (see Effective Java Item 21), but this pattern has its own problems: It is quite verbose, hence error prone, and its enum constants cannot be used in switch statements.
In Tiger, the Java™ programming language gets linguistic support for enumerated types. In their simplest form, these enums look just like their C, C++, and C# counterparts:
enum Season { WINTER, SPRING, SUMMER, FALL }
But appearances can be deceiving. Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers. The new enum declaration defines a full-fledged class (dubbed an enum type). In addition to solving all the problems mentioned above,
it allows you to add arbitrary methods and fields to an enum type, to implement arbitrary interfaces, and more. Enum types provide high-quality implementations of all the Object methods. They are Comparable and Serializable, and the serial form is designed to withstand arbitrary changes in the enum type.
Here is an example of a playing card class built atop a couple of simple enum types. The Card class is immutable, and only one instance of each Card is created, so it need not override equals or hashCode:
import java.util.*;
public class Card {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() { return rank; }
public Suit suit() { return suit; }
public String toString() { return rank + " of " + suit; }
private static final List<Card> protoDeck = new ArrayList<Card>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Card(rank, suit));
}
public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
}
}
The toString method for Card takes advantage of the toString methods for Rank and Suit. Note that the Card class is short (about 25 lines of code). If the typesafe enums (Rank and Suit) had been built by hand, each of them would have been significantly longer than the entire Card class.
The (private) constructor of Card takes two parameters, a Rank and a Suit. If you accidentally invoke the constructor with the parameters reversed, the compiler will politely inform you of your error. Contrast this to the int enum pattern, in which the program would fail at run time.
Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.
The following example is a simple program called Deal that exercises Card. It reads two numbers from the command line, representing the number of hands to deal and the number of cards per hand. Then it creates a new deck of cards, shuffles it, and deals and prints the requested hands.
import java.util.*;
public class Deal {
public static void main(String args[]) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
List<Card> deck = Card.newDeck();
Collections.shuffle(deck);
for (int i=0; i < numHands; i++)
System.out.println(deal(deck, cardsPerHand));
}
public static ArrayList<Card> deal(List<Card> deck,
int n) {
int deckSize = deck.size();
List<Card> handView = deck.subList(deckSize-n, deckSize);
ArrayList<Card> hand = new ArrayList<Card>(handView);
handView.clear();
return hand;
}
}
$ java Deal 4 5
[FOUR of HEARTS, NINE of DIAMONDS, QUEEN of SPADES, ACE of SPADES, NINE of SPADES]
[DEUCE of HEARTS, EIGHT of SPADES, JACK of DIAMONDS, TEN of CLUBS, SEVEN of SPADES]
[FIVE of HEARTS, FOUR of DIAMONDS, SIX of DIAMONDS, NINE of CLUBS, JACK of CLUBS]
[SEVEN of HEARTS, SIX of CLUBS, DEUCE of DIAMONDS, THREE of SPADES, EIGHT of CLUBS]
Suppose you want to add data and behavior to an enum. For example consider the planets of the solar system. Each planet knows its mass and radius, and can calculate its surface gravity and the weight of an object on the planet. Here is how it looks:
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
The enum type Planet contains a constructor, and each enum constant is declared with parameters to be passed to the constructor when it is created.
Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031
The idea of adding behavior to enum consta
nts can be taken one step further. You can give each enum constant a different behavior for some method. One way to do this by switching on the enumeration constant. Here is an example with an enum whose constants represent the four basic arithmetic operations, and whose eval method performs the operation:
public enum Operation {
PLUS, MINUS, TIMES, DIVIDE;
// Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
}
}
This works fine, but it will not compile without the throw statement, which is not terribly pretty. Worse, you must remember to add a new case to the switch statement each time you add a new constant to Operation. If you forget, the eval method with fail, executing the aforementioned throw statement
There is another way give each enum constant a different behavior for some method that avoids these problems. You can declare the method abstract in the enum type and override it with a concrete method in each constant. Such methods are known as constant-specific methods. Here is the previous example redone using this technique:
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } }// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}
Here is a sample program that exercises the Operation class. It takes two operands from the command line, iterates over all the operations, and for each operation, performs the operation and prints the resulting equation:
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));
}
$ java Operation 4 2
4.000000 PLUS 2.000000 = 6.000000
4.000000 MINUS 2.000000 = 2.000000
4.000000 TIMES 2.000000 = 8.000000
4.000000 DIVIDE 2.000000 = 2.000000
Constant-specific methods are reasonably sophisticated, and many programmers will never need to use them, but it is nice to know that they are there if you need them.
Two classes have been added to java.util in support of enums: special-purpose Set and Map implementations called EnumSet and EnumMap. EnumSet is a high-performance Set implementation for enums. All of the members of an enum set must be of the same enum type. Internally, it is represented by a bit-vector, typically a single long. Enum sets support iteration over ranges of enum types. For example given the following enum declaration:
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
you can iterate over the weekdays. The EnumSet class provides a static factory that makes it easy:
for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))
System.out.println(d);
Enum sets also provide a rich,typesafe replacement for traditional bit flags:
EnumSet.of(Style.BOLD, Style.ITALIC)
Similarly, EnumMap is a high-performance Map implementation for use with enum keys, internally implemented as an array. Enum maps combine the richness and safety of the Map interface with speed approaching that of an array. If you want to map an enum to a value, you should always use an EnumMap in preference to an array.
The Card class, above, contains a static factory that returns a deck, but there is no way to get an individual card from its rank and suit. Merely exposing the constructor would destroy the singleton property (that only a single instance of each card is allowed to exist). Here is how to write a static factory that preserves the singleton property, using a nested EnumMap:
private static Map<Suit, Map<Rank, Card>> table =
new EnumMap<Suit, Map<Rank, Card>>(Suit.class);
static {
for (Suit suit : Suit.values()) {
Map<Rank, Card> suitTable = new EnumMap<Rank, Card>(Rank.class);
for (Rank rank : Rank.values())
suitTable.put(rank, new Card(rank, suit));
table.put(suit, suitTable);
}
}
public static Card valueOf(Rank rank, Suit suit) {
return table.get(suit).get(rank);
}
The EnumMap (table) maps each suit to an EnumMap that maps each rank to a card. The lookup performed by the valueOf method is internally implemented as two array accesses, but the code is much clearer and safer. In order to preserve the singleton property, it is imperative that the constructor invocation in the prototype deck initialization in Card be replaced by a call to the new static factory:
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(Card.valueOf(rank, suit));
}
It is also imperative that the initialization of table be placed above the initialization of protoDeck, as the latter depends on the former.
So when should you use enums? Any time you need a fixed set of constants. That includes natural enumerated types (like the planets, days of the week, and suits in a card deck) as well as other sets where you know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, and the like. It is not necessary that the set of constants in an enum type stay fixed for all time. The feature was specifically designed to allow for binary compatible evolution of enum types.
posted on 2009-12-21 16:41 lau 阅读(1520) 评论(0) 编辑 收藏 所属分类: J2SE
java 代码
public interface Color {
static int RED = 1;
static int GREEN = 2;
static int BLUE = 3;
}
虽然这种simulation比较麻烦,但在以前也还应付的过去。可是随着java语言的发展,越来越多的呼声要求把enum这种数据结构独立出来,加入到java中。所以从java 1.5以后,就有了enum,这也是这篇blog要学习的topic。
学习的最好方式就是例子,先来一个:
java 代码
public class EnumDemo {
private enum Color {red, blue, green}//there is not a ";"
public static void main(String[] args) {
for(Color s : Color.values()) {
//enum的values()返回一个数组,这里就是Seasons[]
System.out.println(s);
}
}
}
console results
red
blue
green
注意事项已经在code中注释出,还要说明一点的是,这个java文件编译完成后不只有一个EnumDemo.class,还会有一个EnumDemo$Seasons.class,奇怪吧!
Another e.g.
java 代码
public class EnumDemo {
private enum Color {red, blue, green}//there is not a ";"
public static void main(String[] args) {
Color s = Color.blue;
switch (s) {
case red://notice: Seasons.red will lead to compile error
System.out.println("red case");
break;
case blue:
System.out.println("blue case");
break;
case green:
System.out.println("green case");
break;
default:
break;
}
}
}
这个例子要说明的就是case的情况。
就这么多吗,当然不是,我们的enum结构还可以定义自己的方法和属性。
java 代码
public class EnumDemo {
private enum Color {
red, blue, green;//there is a ";"
//notic: enum's method should be "static"
public static Color getColor(String s){
if(s.equals("red flag")){
return red;
} else if(s.equals("blue flag")){
return blue;
} else {
return green;
}
}
}//there is not ";"
public static void main(String[] args) {
EnumDemo demo = new EnumDemo();
System.out.println(demo.getFlagColor("red flag"));
}
public Color getFlagColor(String string){
return Color.getColor(string);
}
}
Ok,so much for enum. Isn't it simple?.
本文摘自IBM DW,如有转载,请声明!
枚举类型入门
----用 Java 5.0 以类型安全的方式表示常量
Tiger 中的一个重要新特性是枚举构造,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。Tiger 专家、developerWorks 的多产作者 Brett McLaughlin 将解释枚举的定义,介绍如何在应用程序中运用枚举,以及它为什么能够让您抛弃所有旧的 public static final 代码。
您已经知道,Java 代码的两个基本的构造块是 类和 接口。现在 Tiger 又引入了 枚举,一般简称它为 enum。这个新类型允许您表示特定的数据点,这些数据点只接受分配时预先定义的值集合。
当然,熟练的程序员可以用静态常量实现这项功能,如清单 1 所示:
清单 1. public static final 的常量
public class OldGrade {
public static final int A = 1;
public static final int B = 2;
public static final int C = 3;
public static final int D = 4;
public static final int F = 5;
public static final int INCOMPLETE = 6;
}
摘者注:下面这段话说明了上述编程的弊端(除了这里所说的内容外,摘者认为如果OldGrade类中只有这些常量的话,应该将其定义为接口,这样可以防止错误的实例化这个类)
然后您就可以让类接受像 OldGrade.B 这样的常量,但是在这样做的时候,请记住这类常量是 Java 中 int 类型的常量,这意味着该方法可以接受任何int类型的值,即使它和 OldGrade 中定义的所有级别都不对应。因此,您需要检测上界和下界,在出现无效值的时候,可能还要包含一个 IllegalArgumentException 。而且,如果后来又添加另外一个级别(例如 OldGrade.WITHDREW_PASSING ),那么必须改变所有代码中的上界,才能接受这个新值。
换句话说,在使用这类带有整型常量的类时,该解决方案也许可行,但并不是非常有效。幸运的是,枚举提供了更好的方法。
定义枚举
清单 2 使用了一个可以提供与清单 1 相似的功能的枚举:
清单 2. 简单的枚举类型
package com.oreilly.tiger.ch03;
public enum Grade {
A, B, C, D, F, INCOMPLETE
};
在这里,我使用了新的关键字 enum ,为 enum 提供了一个名称,并指定了允许的值。然后, Grade 就变成了一个 枚举类型,您可以按清单 3 所示的方法使用它:
清单 3. 使用枚举类型
package com.oreilly.tiger.ch03;
public class Student {
priv
ate Grade grade;
public void assignGrade(Grade grade) {
this.grade = grade;
}
public Grade getGrade() {
return grade;
}
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.A);
System.out.println(std.getGrade());
}
}
用以前定义过的类型建立一个新的枚举( grade )之后,您就可以像使用其他成员变量一样使用它了。当然,枚举只能分配枚举值中的一个(例如, A 、 C 或 INCOMPLETE )。而且,在 assignGrade() 中是没有进行错误检测的代码,也没有考虑边界情况,请注意这是如何做到。
(摘者注:摘者将上面的使用枚举类型的类代码进行了修改,将一些设置学生姓名等内容删除,并将构造函数也删除,只留下对说明枚举类型有作用的代码,并添加 了main方法来进行grade的设置和获取并打印,程序执行结果为A,由于使用枚举类型在方法assignGrade的参数中定义的是枚举类型,而不是 java的int类型,所以传递的参数必须是枚举类型参数,而枚举类型参数中定义的值都是合法的)。
使用枚举值
迄今为止,您所看到的示例都相当简单,但是枚举类型提供的东西远不止这些。您可以逐个遍历枚举值,也可以在 switch 语句中使用枚举值,枚举是非常有价值的。
遍历枚举值
下面我们用一个示例显示如何遍历枚举类型的值。清单 4 所示的这项技术,适用于调试、快速打印任务以及把枚举加载到集合(我很快将谈到)中的工具:
清单 4. 遍历枚举值
将上面的main方法修改为如下内容:
public static void main(String[] args) {
for (Grade g : Grade.values()) {
System.out.println("Allowed value: '" + g + "'");
}
}
运行这段代码,将得到清单 5 所示的输出:
清单 5. 迭代操作的输出
Allowed Value: 'A'
Allowed Value: 'B'
Allowed Value: 'C'
Allowed Value: 'D'
Allowed Value: 'F'
Allowed Value: 'INCOMPLETE'
这里有许多东西。首先,我使用了 Tiger 的新的 for/in 循环(也叫作 foreach 或 增强的 for )。另外,您可以看到 values() 方法返回了一个由独立的 Grade 实例构成的数组,每个数组都有一个枚举类型的值。换句话说, values() 的返回值是 Grade[] 。
在枚举间切换
能够在枚举的值之间移动很好,但是更重要的是根据枚举的值进行决策。您当然可以写一堆 if (grade.equals(Grade.A)) 类型的语句,但那是在浪费时间。Tiger 能够很方便地把枚举支持添加到过去的好东西 switch 语句上,所以它很容易使用,而且适合您已知的内容。清单 6 向将展示如何解决这个难题:
清单 6. 在枚举之间切换
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.INCOMPLETE);
switch (std.getGrade()) {
case A:
System.out.println("excelled with a grade of A");
break;
case B: // fall through to C
case C:
System.out.println("passed with a grade of "
+ std.getGrade().toString());
break;
case D: // fall through to F
case F:
System.out.println("failed with a grade of "+ std.getGrade().toString());
break;
case INCOMPLETE:
System.out.println("did not complete the class.");
break;
}
}
在这里,枚举值被传递到 switch 语句中(请记住, getGrade() 是作为 Grade 的实例返回的),而每个 case 子句将处理一个特定的值。该值在提供时没有枚举前缀,这意味着不用将代码写成 case Grade.A ,只需将其写成 case A 即可。如果您不这么做,编译器不会接受有前缀的值。
现在,您应该已经了解使用 switch 语句时的基本语法,但是还有一些事情您需要知道。
在使用 switch 之前进行计划
正如您所期待的,在使用枚举和 switch 时,您可以使用 default 语句。清单 7 显示了这个用法:
清单 7. 添加一个 default 块
public static void main(String[] args) {
Student std = new Student();
std.assignGrade(Grade.INCOMPLETE);
switch (std.getGrade()) {
case A:
System.out.println("excelled with a grade of A");
break;
case B: // fall through to C
case C:
System.out.println("passed with a grade of "+ std.getGrade().toString());
break;
case D: // fall through to F
case F:
System.out.println("failed with a grade of "
+ std.getGrade().toString());
break;
case INCOMPLETE:
System.out.println("did not complete the class.");
break;
}
default:
System.out.println("has a grade of "+std.getGrade().toString());
break;
}
研究以上代码可以看出,任何没有被 case 语句处理的枚举值都会被 default 语句处理。这项技术您应当坚持采用。原因是:假设 Grade 枚举被您的小组中其他程序员修改(而且他忘记告诉您这件事)成清单 8 所示的版本:
清单 8. 给 Grade 枚举添加一个值
package com.oreilly.tiger.ch03;
public enum Grade {
A, B, C, D, F, INCOMPLETE
,
WITHDREW_PASSING, WITHDREW_FAILING
};
现在,如果使用清单 6 的代码所示的新版 Grade ,那么这两个新值会被忽略。更糟的是,您甚至看不到错误!在这种情况下,存在某种能够通用的 default 语句是非常重要的。清单 7 无法很好地处理这些值,但是它会提示您还有其他值,您需要处理这些值。一旦完成处理,您就会有一个继续运行的应用程序,而且它不会忽略这些值,甚至还会指 导您下一步的动作。所以这是一个良好的编码习惯。
枚举和集合
您所熟悉的使用 public static final 方法进行编码的那些东西,可能已经转而采用枚举的值作为映射的键。如果您不知道其中的含义,请参见清单 9,它是一个公共错误信息的示例,在使用 Ant 的 build 文件时,可能会弹出这样的消息,如下所示:
清单 9. Ant 状态码
package com.oreilly.tiger.ch03;
public enum AntStatus {
INITIALIZING,
COMPILING,
COPYING,
JARRING,
ZIPPING,
DONE,
ERROR
}为每个状态码分配一些人们能读懂的错误信息,从而允许人们在 Ant 提供某个代码时查找合适的错误信息,将这些信息显示在控制台上。这是映射(Map) 的一个绝好用例,在这里,每个映射(Map) 的键都是一个枚举值,而每个值都是键的错误信息。清单 10 演示了该映射的工作方式:
清单 10. 枚举的映射(Map)
public void testEnumMap(PrintStream out) throws IOException {
// Create a map with the key and a String message
EnumMap<AntStatus, String> antMessages =
new EnumMap<AntStatus, String>(AntStatus.class);
// Initialize the map
antMessages.put(AntStatus.INITIALIZING, "Initializing Ant...");
antMessages.put(AntStatus.COMPILING, "Compiling Java classes...");
antMessages.put(AntStatus.COPYING, "Copying files...");
antMessages.put(AntStatus.JARRING, "JARring up files...");
antMessages.put(AntStatus.ZIPPING, "ZIPping up files...");
antMessages.put(AntStatus.DONE, "Build complete.");
antMessages.put(AntStatus.ERROR, "Error occurred.");
// Iterate and print messages
for (AntStatus status : AntStatus.values() ) {
out.println("For status " + status + ", message is: " +
antMessages.get(status));
}
}
该代码使用了泛型(generics)(请参阅 参考资料)和新的 EnumMap 构造来建立新映射。而且,枚举值是通过其 Class 对象提供的,同时提供的还有映射值的类型(在该例中,它只是一个简单的字符串)。该方法的输出如清单 11 所示:
清单 11. 清单 10 的输出
For status INITIALIZING,message is: Initializing Ant...
For status COMPILING, message is: Compiling Java classes...
For status COPYING, message is: Copying files...
For status JARRING, message is: JARring up files...
For status ZIPPING, message is: ZIPping up files...
For status DONE, message is: Build complete.
For status ERROR, message is: Error occurred.
更进一步
枚举也可以与集合结合使用,而且非常像新的EnumMap构造,Tiger 提供了一套新的EnumSet实现,允许您使用位操作符。另外,可以为枚举添加方法,用它们实现接口,定义叫作特定值的类的实体,在该实体中,特定的代码 被附加到枚举的具体值上。这些特性超出了本文的范围,但是在其他地方,有详细介绍它们的文档(请参阅参考资料)。
使用枚举,但是不要滥用
学习任何新版语言的一个危险就是疯狂使用新的语法结构。如果这样做,那么您的代码就会突然之间有 80% 是泛型、标注和枚举。所以,应当只在适合使用枚举的地方才使用它。那么,枚举在什么地方适用呢?一条普遍规则是,任何使用常量的地方,例如目前用 switch代码切换常量的地方。如果只有单独一个值(例如,鞋的最大尺寸,或者笼子中能装猴子的最大数目),则还是把这个任务留给常量吧。但是,如果定 义了一组值,而这些值中的任何一个都可以用于特定的数据类型,那么将枚举用在这个地方最适合不过。
摘者将Sun Java 5.0对于类型安全的Enum的说明拷贝放在下边,以供参考:
In prior releases, the standard way represent an enumerated type was the int Enum pattern: public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL = 3;
This pattern has many problems, such as:
Not typesafe - Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which makes no sense).
No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types.
Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.
It is possible to get around these problems by using the Typesafe Enum pattern (see Effective Java Item 21), but this pattern has its own problems: It is quite verbose, hence error prone, and its enum constants cannot be used in switch statements.
In Tiger, the Java™ programming language gets linguistic support for enumerated types. In their simplest form, these enums look just like their C, C++, and C# counterparts:
enum Season { WINTER, SPRING, SUMMER, FALL }
But appearances can be deceiving. Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers. The new enum declaration defines a full-fledged class (dubbed an enum type). In addition to solving all the problems mentioned above,
it allows you to add arbitrary methods and fields to an enum type, to implement arbitrary interfaces, and more. Enum types provide high-quality implementations of all the Object methods. They are Comparable and Serializable, and the serial form is designed to withstand arbitrary changes in the enum type.
Here is an example of a playing card class built atop a couple of simple enum types. The Card class is immutable, and only one instance of each Card is created, so it need not override equals or hashCode:
import java.util.*;
public class Card {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() { return rank; }
public Suit suit() { return suit; }
public String toString() { return rank + " of " + suit; }
private static final List<Card> protoDeck = new ArrayList<Card>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Card(rank, suit));
}
public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
}
}
The toString method for Card takes advantage of the toString methods for Rank and Suit. Note that the Card class is short (about 25 lines of code). If the typesafe enums (Rank and Suit) had been built by hand, each of them would have been significantly longer than the entire Card class.
The (private) constructor of Card takes two parameters, a Rank and a Suit. If you accidentally invoke the constructor with the parameters reversed, the compiler will politely inform you of your error. Contrast this to the int enum pattern, in which the program would fail at run time.
Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.
The following example is a simple program called Deal that exercises Card. It reads two numbers from the command line, representing the number of hands to deal and the number of cards per hand. Then it creates a new deck of cards, shuffles it, and deals and prints the requested hands.
import java.util.*;
public class Deal {
public static void main(String args[]) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
List<Card> deck = Card.newDeck();
Collections.shuffle(deck);
for (int i=0; i < numHands; i++)
System.out.println(deal(deck, cardsPerHand));
}
public static ArrayList<Card> deal(List<Card> deck,
int n) {
int deckSize = deck.size();
List<Card> handView = deck.subList(deckSize-n, deckSize);
ArrayList<Card> hand = new ArrayList<Card>(handView);
handView.clear();
return hand;
}
}
$ java Deal 4 5
[FOUR of HEARTS, NINE of DIAMONDS, QUEEN of SPADES, ACE of SPADES, NINE of SPADES]
[DEUCE of HEARTS, EIGHT of SPADES, JACK of DIAMONDS, TEN of CLUBS, SEVEN of SPADES]
[FIVE of HEARTS, FOUR of DIAMONDS, SIX of DIAMONDS, NINE of CLUBS, JACK of CLUBS]
[SEVEN of HEARTS, SIX of CLUBS, DEUCE of DIAMONDS, THREE of SPADES, EIGHT of CLUBS]
Suppose you want to add data and behavior to an enum. For example consider the planets of the solar system. Each planet knows its mass and radius, and can calculate its surface gravity and the weight of an object on the planet. Here is how it looks:
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
The enum type Planet contains a constructor, and each enum constant is declared with parameters to be passed to the constructor when it is created.
Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031
The idea of adding behavior to enum consta
nts can be taken one step further. You can give each enum constant a different behavior for some method. One way to do this by switching on the enumeration constant. Here is an example with an enum whose constants represent the four basic arithmetic operations, and whose eval method performs the operation:
public enum Operation {
PLUS, MINUS, TIMES, DIVIDE;
// Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
}
}
This works fine, but it will not compile without the throw statement, which is not terribly pretty. Worse, you must remember to add a new case to the switch statement each time you add a new constant to Operation. If you forget, the eval method with fail, executing the aforementioned throw statement
There is another way give each enum constant a different behavior for some method that avoids these problems. You can declare the method abstract in the enum type and override it with a concrete method in each constant. Such methods are known as constant-specific methods. Here is the previous example redone using this technique:
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } }// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}
Here is a sample program that exercises the Operation class. It takes two operands from the command line, iterates over all the operations, and for each operation, performs the operation and prints the resulting equation:
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));
}
$ java Operation 4 2
4.000000 PLUS 2.000000 = 6.000000
4.000000 MINUS 2.000000 = 2.000000
4.000000 TIMES 2.000000 = 8.000000
4.000000 DIVIDE 2.000000 = 2.000000
Constant-specific methods are reasonably sophisticated, and many programmers will never need to use them, but it is nice to know that they are there if you need them.
Two classes have been added to java.util in support of enums: special-purpose Set and Map implementations called EnumSet and EnumMap. EnumSet is a high-performance Set implementation for enums. All of the members of an enum set must be of the same enum type. Internally, it is represented by a bit-vector, typically a single long. Enum sets support iteration over ranges of enum types. For example given the following enum declaration:
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
you can iterate over the weekdays. The EnumSet class provides a static factory that makes it easy:
for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))
System.out.println(d);
Enum sets also provide a rich,typesafe replacement for traditional bit flags:
EnumSet.of(Style.BOLD, Style.ITALIC)
Similarly, EnumMap is a high-performance Map implementation for use with enum keys, internally implemented as an array. Enum maps combine the richness and safety of the Map interface with speed approaching that of an array. If you want to map an enum to a value, you should always use an EnumMap in preference to an array.
The Card class, above, contains a static factory that returns a deck, but there is no way to get an individual card from its rank and suit. Merely exposing the constructor would destroy the singleton property (that only a single instance of each card is allowed to exist). Here is how to write a static factory that preserves the singleton property, using a nested EnumMap:
private static Map<Suit, Map<Rank, Card>> table =
new EnumMap<Suit, Map<Rank, Card>>(Suit.class);
static {
for (Suit suit : Suit.values()) {
Map<Rank, Card> suitTable = new EnumMap<Rank, Card>(Rank.class);
for (Rank rank : Rank.values())
suitTable.put(rank, new Card(rank, suit));
table.put(suit, suitTable);
}
}
public static Card valueOf(Rank rank, Suit suit) {
return table.get(suit).get(rank);
}
The EnumMap (table) maps each suit to an EnumMap that maps each rank to a card. The lookup performed by the valueOf method is internally implemented as two array accesses, but the code is much clearer and safer. In order to preserve the singleton property, it is imperative that the constructor invocation in the prototype deck initialization in Card be replaced by a call to the new static factory:
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(Card.valueOf(rank, suit));
}
It is also imperative that the initialization of table be placed above the initialization of protoDeck, as the latter depends on the former.
So when should you use enums? Any time you need a fixed set of constants. That includes natural enumerated types (like the planets, days of the week, and suits in a card deck) as well as other sets where you know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, and the like. It is not necessary that the set of constants in an enum type stay fixed for all time. The feature was specifically designed to allow for binary compatible evolution of enum types.
posted on 2009-12-21 16:41 lau 阅读(1520) 评论(0) 编辑 收藏 所属分类: J2SE