- 浏览: 245232 次
- 性别:
- 来自: LA
文章分类
- 全部博客 (84)
- java 技术 (16)
- spring MVC (1)
- spring framework (5)
- 正则 (1)
- Rest (1)
- log4j (1)
- java 线程 (7)
- 算法 (1)
- 版本控制 (7)
- WEB 前端 (4)
- 搜索 (3)
- 系统架构 (3)
- 设计模式 (2)
- 杂 (2)
- SOA (3)
- JAVA 数据库 (1)
- android (3)
- 数据库挖掘 (2)
- linux (3)
- web server (2)
- 分布式 (1)
- hadoop 相关 (3)
- ant&&maven (1)
- 性能调优 (2)
- javascript (2)
- sql (1)
- cookie (1)
- java 调试 (1)
- MYSQL (2)
- 数据库 (3)
最新评论
-
wangtuda:
git commit -amend是git commit -- ...
git 修改 已经提交了的注释 -
threenoodles:
...
java enum -
songjiesdnu:
...
java enum -
xy2401:
前面还好,看到后面好乱
spring annotation -
wf6916311:
Cookie
关键字: 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逻辑运算符&&||&|区别
2012-09-19 09:37 1414逻辑运算符 Logical Operator 逻辑运算符只对 ... -
安装Eclipse反编译插件
2012-03-30 10:14 1580一、前提: 1、已经安装了Eclipse,如我的Eclip ... -
java list to array
2012-03-13 19:41 1115/** * cast list to array ... -
java 动态代理
2012-02-04 20:37 1107(3.) 基于Proxy的动态代理: JAVA 自带的动态代理 ... -
java Class 及 RTTI 相关
2012-02-04 19:52 1441运行时类型信息(RunTime Type Informatio ... -
java classloader
2011-12-24 17:29 1285java.lang.NoClassDefFoundError: ... -
Java内存管理的9个小技巧
2011-12-20 17:28 7511、别用new Boolean()。 ... -
java enum
2011-12-16 14:36 9980public enum Operation { P ... -
【转】Java中的Enum的使用与分析
2011-12-05 20:05 1575示例: public enum EnumTest { ... -
java Map 遍历 Map.Entry
2011-11-23 14:00 2047package com.talent.platform.dem ... -
笔记一
2011-11-22 20:34 889/** * The minimum prio ... -
【转】instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
2011-11-22 20:29 1581编程的时候可能会遇到一个不知道它属于哪个类的对象,我们可以用下 ... -
【转】同步块和同步方法
2011-11-08 11:08 1063打个比方:一个object就 ... -
java Date
2011-11-06 12:10 10561.计算某一月份的最大天数 Calendar tim ... -
【转】浅析 Java Thread.join()
2011-11-04 10:32 845一、在研究join的用法之前,先明确两件事情。 1.join ...
相关推荐
以下是对`javaenum学习.pdf`中提到的Java枚举相关知识点的详细说明: 1. **枚举元素定义**: - 枚举元素的列表必须写在枚举类的开头,元素之间用逗号分隔。如果元素列表后面没有其他内容,可以不加分号;如果有...
"Java Enum 枚举学习资料" Java Enum 枚举学习资料是 Java 编程语言中的一种特殊类型,它主要用来实现一组固定的常量。 Enum 枚举类型是 Java 5 中引入的一种新特性,旨在解决传统的 int 枚举常量的缺陷。 1. 枚举...
在Java编程语言中,`enum`(枚举)是一种特殊的类,用于定义一组固定的常量。...在阅读《https://bijian1013.iteye.com/blog/2309669》这篇博客时,你可以深入学习更多关于Java `enum`的高级用法和技巧。
Java枚举(enum)是Java语言提供的一种特殊的数据类型,用于定义一组有限的常量,这些常量在程序中作为固定的值使用。枚举在Java中是类的子类型,因此,它们具备类的一些特性,比如可以有方法、变量以及实现接口。...
通过本文的学习,我们了解了Java中枚举类型的基本概念、特点以及如何使用。枚举类型不仅提供了更清晰的代码可读性和可维护性,还确保了类型的安全性。在实际开发中,合理利用枚举类型能够有效提升代码质量和程序的...
简单查询算法,供学习参考。 代码中描述了Enum和EnumMap类的使用。
本资源包提供了一些关于`Enum`枚举的深入学习材料,包括实例、常见问题以及相关的文档,非常适合Java开发者进行高级编程的学习和复习。 首先,枚举的基本语法是定义一个公共类,前面加上关键字`enum`。例如: ```...
Java 枚举(enum)是 Java 语言中的一个重要特性,自 Java 5 开始引入。枚举用于定义一组相关的常量,这些常量通常代表一个有限的、预定义的值集合。枚举类是一种特殊的类,它具有以下特点: 1. **枚举类的声明**:...
switch语句是JAVA中的一个选择结构,类型可以是int, short, byte, char, enum这五个类型,enum必须是JDK5.0。 JAVA学习笔记涵盖了JAVA的基础知识和面向对象编程思想,帮助学习者更好地理解和使用JAVA语言。
### Java中的Enum枚举用法详解 #### 一、引言 在Java编程语言中,枚举(Enum)是一种特殊的数据类型,它被用来表示一组固定的常量集合。相比于使用常量接口或字符串来表示固定集合,枚举提供了更加安全且易于管理的...
15. **枚举(Enum)**:Java 5.0引入的枚举类型,用于定义一组固定的常量。 16. **包装类**:Java为每种基本数据类型提供了对应的包装类,如Integer、Double等,它们提供了许多有用的方法。 17. **JDBC**:Java ...
本学习笔记旨在帮助新手全面理解并掌握Java 5.0的关键知识点。 一、泛型(Generics) Java 5.0引入了泛型,它允许在定义类、接口和方法时指定类型参数,从而实现类型安全的集合。泛型消除了强制类型转换的需要,...
在Java编程语言中,`enum`(枚举)和`switch`语句是两个非常重要的概念,它们在软件开发中有着...通过学习和实践这些源代码,你可以提升你的Java编程技能,尤其在处理多分支逻辑时,能够写出更加清晰、易维护的代码。
"java学习资料.zip"包含了两本经典书籍的中文版本:《Effective Java》的第一版和第二版,这些都是Java开发者深入理解语言特性和最佳实践的重要资源。 《Effective Java》是由Joshua Bloch编写的,他是一位经验丰富...
总的来说,学习Java枚举不仅能够帮助我们编写更清晰、更安全的代码,还能提高程序设计的效率。通过深入理解枚举的特性和用法,可以更好地应对各种编程场景,尤其是在处理固定值集合或需要类型安全的场合。在Java EE...
在Java编程语言中,枚举(Enum...学习并熟练使用枚举是提升Java编程能力的关键步骤。通过下载提供的"Java 实例 - Enum(枚举)构造函数及方法的使用源代码-详细教程.zip"资源,你可以进一步实践和掌握枚举的相关知识。
Java枚举(enum)是Java语言中的一种特殊数据类型,用于定义一组有限的常量,这些常量在程序中作为固定的值使用。枚举在Java中被引入,目的是为了更好地管理和使用常量,提高代码的可读性和安全性。在本实例代码中,...
《java jdk 7学习笔记》是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习java时遇到的概念、操作、应用或认证考试等问题及解决方案。 《java jdk 7学习笔记》针对java se 7新功能全面改版,无论是...
Java枚举(Enum)是Java语言中的一个特殊类,它在`java.lang`包下,是面向对象编程中的一种常量表示方式。本章节将深入探讨Java枚举的源码和《Thinking in Java》第十九章中关于枚举的内容。 在Java中,枚举是一种...