1. 基础知识
// 创建枚举 enum
enum Apple {
// 枚举常量:
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo {
public static void main(String[] args) {
// 将 ap 声明为 Apple 枚举类型的变量:(不能使用new实例化)
Apple ap;
// 赋值
// 只能别赋值为(或包含)在 Apple 枚举中定义的值
ap = Apple.RedDel;
System.out.println("ap的值为:" + ap);
System.out.println();
ap = Apple.GoldenDel;
// 比较相等性
if (ap == Apple.GoldenDel)
System.out.println("ap contains GoldenDel.\n");
// 输出枚举常量的名称
System.out.println(Apple.Winesap);
System.out.println();
switch (ap) {
case Jonathan:
System.out.println("Jonathan is red.");
break;
case GoldenDel:
System.out.println("Golden Delicious is yellow.");
break;
case RedDel:
System.out.println("Red Delicious is red.");
break;
case Winesap:
System.out.println("Winesap is red.");
break;
case Cortland:
System.out.println("Cortland is red.");
break;
}
}
}
Output:
ap的值为:RedDel
ap contains GoldenDel.
Winesap
Golden Delicious is yellow.
2. values() 和 valueOf() 方法
一般形式:
public static enum-type[] values()
public static enum-type valueOf(String str)
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
public class EnumDemo2 {
public static void main(String[] args) {
Apple ap;
System.out.println("Apple的全部常量:");
// values()方法返回一个包含枚举常量列表的数组
Apple[] allapples = Apple.values();
for (Apple a : allapples) {
System.out.println(a);
}
// 以上等价于:
// for (Apple a : Apple.values())
System.out.println();
// valueOf()方法返回与传入参数str的字符串相对应的枚举常量
ap = Apple.valueOf("Winesap");
System.out.println("ap contains " + ap);
}
}
3. Java 枚举是类类型
enum Apple {
// 没有为RedDel提供参数,会调用默认构造函数
Jonathan(10), GoldenDel(9), RedDel, Winesap(15), Cortland(8);
private int price;
// 构造函数
Apple(int p) {
price = p;
}
// 默认构造函数
Apple() {
price = -1;
}
int getPrice() {
return price;
}
}
public class EnumDemo03 {
public static void main(String[] args) {
Apple ap;
System.out.println("Winesap costs " + Apple.Winesap.getPrice() + " cents.\n");
System.out.println("All apple prices:");
for (Apple a : Apple.values())
System.out.println(a + " costs " + a.getPrice() + " cents.");
}
}
Output:
Winesap costs 15 cents.
All apple prices:
Jonathan costs 10 cents.
GoldenDel costs 9 cents.
RedDel costs -1 cents.
Winesap costs 15 cents.
Cortland costs 8 cents.
限制:
- 枚举不能继承其他类
- 枚举不能是超类
- 每个枚举常量都是定义它的类的对象
4. 枚举继承自Enum类
- 所有枚举自动继承超类
java.lang.Enum
final int ordinal()
final int compareTo(enum-type e)
equals()
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
public class EnumDemo4 {
public static void main(String[] args) {
Apple ap, ap2, ap3;
System.out.println("Here are all apple constants" + " and their ordinal values: ");
for (Apple a : Apple.values())
// final int ordinal() 获取用于只是枚举常量再常量列表中位置的值,被称为枚举常量的序数值
System.out.println(a + " " + a.ordinal());
ap = Apple.RedDel;
ap2 = Apple.GoldenDel;
ap3 = Apple.RedDel;
System.out.println();
// final int compareTo(enum-type e) 比较相同类型的两个枚举常量的序数值
if (ap.compareTo(ap2) < 0)
System.out.println(ap + " comes before " + ap2);
if (ap.compareTo(ap2) > 0)
System.out.println(ap2 + " comes before " + ap);
if (ap.compareTo(ap3) == 0)
System.out.println(ap + " equals " + ap3);
System.out.println();
// equals() 比较枚举常量和其他对象的相等性
// 只有当两个对象都引用同一个枚举中的相同常量时,才相等
if (ap.equals(ap2))
System.out.println("Error!");
if (ap.equals(ap3))
System.out.println(ap + " equals " + ap3);
// == 比较两个枚举引用的相等性
if(ap == ap3)
System.out.println(ap + " == " + ap3);
}
}
Output:
Here are all apple constants and their ordinal values:
Jonathan 0
GoldenDel 1
RedDel 2
Winesap 3
Cortland 4
GoldenDel comes before RedDel
RedDel equals RedDel
RedDel equals RedDel
RedDel == RedDel
5. 决策生成器
import java.util.Random;
enum Answers {
NO, YES, MAYBE, LATER, SOON, NEVER
}
class Question {
Random rand = new Random();
Answers ask() {
int prob = (int) (100*rand.nextDouble());
if (prob<15)
return Answers.MAYBE;
else if (prob<30)
return Answers.NO;
else if (prob<60)
return Answers.YES;
else if (prob<75)
return Answers.LATER;
else if (prob<98)
return Answers.SOON;
else
return Answers.NEVER;
}
}
public class AskMe {
static void answer (Answers result) {
switch(result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}
public static void main(String[] args) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}