enum
声明的类,自动继承自
java.lang.Enum
不能显式继承其他类(因为 Java 只允许单继承,而
enum
已继承自
Enum
)
枚举构造器只能是
private
枚举常量
RED, GREEN, BLUE
每个常量都是
Color
类型的实例,系统会自动帮你生成:
public static final Color RED = new Color("RED", 0);
public static final Color GREEN = new Color("GREEN", 1);
public static final Color BLUE = new Color("BLUE", 2);
// 这些实例在类加载时就会创建(饿汉式单例)
二、枚举高级用法
#
枚举带构造方法和属性
#
public enum Color {
RED("红色"), GREEN("绿色"), BLUE("蓝色");
private String desc;
Color(String desc) {
this.desc = desc;
public String getDesc() {
return desc;
枚举实现接口
#
interface Printable {
void print();
public enum Status implements Printable {
public void print() { System.out.println("OK"); }
FAIL {
public void print() { System.out.println("FAIL"); }
枚举方法重写(每个枚举单独实现方法)
#
public enum Operation {
PLUS {
public int apply(int a, int b) { return a + b; }
MINUS {
public int apply(int a, int b) { return a - b; }
public abstract int apply(int a, int b);
枚举与 switch 配合
#
Color c = Color.RED;
switch (c) {
case RED:
System.out.println("红色");
break;
枚举遍历
#
for (Color color : Color.values()) {
System.out.println(color.name() + " : " + color.ordinal());
三、枚举底层机制
#
private
Supplier<Shape> factory;
ShapeType(Supplier<Shape> factory) {
this
.
factory
= factory; }
public
Shape create() {
return
factory.
get
(); }
配合 Map 使用(高性能替代 if-else/switch)
#
public enum Command {
LOGIN, LOGOUT;
private static final Map<String, Command> map = new HashMap<>();
static {
for (Command c : Command.values()) {
map.put(c.name(), c);
public static Command from(String name) {
return map.get(name);
五、常见坑与注意事项
#
public
Handler getInstance()
throws
Exception {
return
clazz.
getDeclaredConstructor
().
newInstance
();
七、最佳实践
#
推荐枚举代替常量类
不要依赖
ordinal()
做数据库映射
多用属性字段,提升可扩展性
配合接口+工厂,让枚举更灵活
配合
Map
替代复杂的
if-else
八、高级枚举知识点
#
枚举单例(线程安全的懒加载单例神器)
#
public enum Singleton {
INSTANCE;
public void doSomething() {
System.out.println("做事");
推荐理由:
JVM 保证枚举单例是线程安全的
防止反射、反序列化攻击
这是 Joshua Bloch(Effective Java 作者)推荐的单例模式首选
反射枚举实例(受限但可以)#
Color color = Color.valueOf("RED");
通过反射无法创建新的枚举实例(JVM 限制)
但可以通过 Enum.valueOf()
获取已有实例
反序列化安全#
枚举的反序列化自动保证单例,不会像普通类一样生成新对象
不需要重写 readResolve()
枚举类 Class 特性#
Color.class.getSuperclass(); // java.lang.Enum
Color.class.isEnum(); // true
枚举泛型#
public enum ResultCode implements EnumInterface<String> {
SUCCESS("200"),
FAIL("500");
private String code;
枚举扩展接口、行为(推荐用于策略、工厂)#
public interface IBehavior {
void act();
public enum Animal implements IBehavior {
CAT {
public void act() { System.out.println("喵喵"); }
DOG {
public void act() { System.out.println("汪汪"); }
九、枚举源码内部机制解密#
源码行为说明