一、 每一个枚举对象都是java.lang.Enum<E extends Enum<E>>抽象类的子类
举个例子: 策略枚举
1) 代码
package com.javase00001.myenum;
public enum EnumDemo {
ADD("+") {//每一个常量都是public static final
@Override
public int apply(int a, int b) {
return a + b;
}
},
SUB("-") {
@Override
public int apply(int a, int b) {
return a - b;
}
},
MUT("*") {
@Override
public int apply(int a, int b) {
return a * b;
}
},
DIV("/") {
@Override
public int apply(int a, int b) throws RuntimeException{
int result = 0;
try {
result = a / b;
} catch (ArithmeticException e) {
System.out.println("除数不可以为0");
throw e;
}
return result;
}
};
public abstract int apply(int a, int b);
private EnumDemo(String op) { //只能是私有的
this.op = op;
}
@Override
public String toString() {
return this.op;
}
// Enum 中final method
/* @Override
public boolean equals(Object obj) {
return true;
}*/
private String op;
}
2) 测试
@Test
public void enumDemoTest {
int a, b;
a = 2;
b = 1;
System.out.println(""+ a + EnumDemo.ADD + b + " = "
+ EnumDemo.ADD.apply(a, b));
System.out.println(""+a + EnumDemo.DIV + b + " = "
+ EnumDemo.DIV.apply(a, b));
System.out.println(""+a + EnumDemo.MUT + b + " = "
+ EnumDemo.MUT.apply(a, b));
System.out.println(""+a + EnumDemo.SUB + b + " = "
+ EnumDemo.SUB.apply(a, b));
}
反编译
javap -v EnumDEmo
Compiled from "EnumDemo.java"
public abstract class com.javase00001.m
yenum.EnumDemo extends java.lang.Enum
SourceFile: "EnumDemo.java"
InnerClass:
#17; //class com/javase00001/myenum/EnumDemo$1
#28; //class com/javase00001/myenum/EnumDemo$2
#36; //class com/javase00001/myenum/EnumDemo$3
#44; //class com/javase00001/myenum/EnumDemo$4
{
public static final com.javase00001.myenum.EnumDemo ADD;
public static final com.javase00001.myenum.EnumDemo SUB;
public static final com.javase00001.myenum.EnumDemo MUT;
public static final com.javase00001.myenum.EnumDemo DIV;
static {};
public abstract int apply(int, int);
public java.lang.String toString();
public static com.javase00001.myenum.EnumDemo[] values();
public static com.javase00001.myenum.EnumDemo valueOf(java.lang.String);
com.javase00001.myenum.EnumDemo(java.lang.String, int, java.lang.String, com.javase00001.myenum.EnumDemo);
}
二、问题?
Q:Enum 类中没有 values() : EnumDemo[] 和v alueOf(String)方法?它们从何而来?
A:
In addition, if E
is the name of an enum
type, then that type has the following implicitly declaredstatic
methods:
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);