Java 枚举:理解枚举本质(二)

2014-11-24 09:14:55 · 作者: · 浏览: 1

public static enum Color {
RED("red color", 0), GREEN("green color", 1),
BLUE("blue color", 2), YELLOW("yellow color", 3);

private String _name;
private int _id;

private Color(String name, int id) {
this._name = name;
this._id = id;
}

public String getName() {
return this._name;
}

public int getId() {
return this._id;
}
}

可以看出,编译器淘气的为其构造方法加上了 private,那么也就是说,我们无法实例化枚举。


所有枚举类都继承了 Enum 类的方法,包括 toString 、equals、hashcode 等方法。

因为 equals、hashcode 方法是 final 的,所以不可以被枚举重写(只可以继承)。

但是,可以重写 equals 方法。

关于 Enum 源码,详见附录!

那么,使用 Java 的不同类来模拟一下枚举,大概是这个样子


[java]
package mark.demo;

import java.util.ArrayList;
import java.util.List;

public class Color {
private static final Color RED = new Color("red color", 0);
private static final Color GREEN = new Color("green color", 1);
private static final Color BLUE = new Color("blue color", 2);
private static final Color YELLOW = new Color("yellow color", 3);

private final String _name;
private final int _id;

private Color(String name, int id) {
_name = name;
_id = id;
}

public String getName() {
return _name;
}

public int getId() {
return _id;
}

public static List values() {
List list = new ArrayList();
list.add(RED);
list.add(GREEN);
list.add(BLUE);
list.add(YELLOW);
return list;
}

@Override
public String toString() {
return "the color _name=" + _name + ", _id=" + _id;
}

}


附录

Enum.java


[java]
/*
* %W% %E%
*
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.lang;

import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;

/**
* This is the common base class of all Java language enumeration types.
*
* @author Josh Bloch
* @author Neal Gafter
* @version %I%, %G%
* @since 1.5
*/
public abstract class Enum>
implements Comparable, Serializable {
/**
* The name of this enum constant, as declared in the enum declaration.
* Most programmers should use the {@link #toString} method rather than
* accessing this field.
*/
private final String name;

/**
* Returns the name of this enum constant, exactly as declared in its
* enum declaration.
*
* Most programmers should use the {@link #toString} method in
* preference to this one, as the toString method may return
* a more user-friendly name.
This method is designed primarily for
* use in specialized situations where correctness depends on getting the
* exact name, which will not vary from release to release.
*
* @return the name of this enum constant
*/
public final String name() {
return name;
}

/**
* The ordinal of this enumeration constant (its position
* in the enum declaration, where the initial constant is assigned
* an ordinal of zero).
*
* Most programmers will have no use for this field. It is designed
* for use by sophisticated enum-based data structures, such as
* {@link java.util.EnumSet} and {@link java.util.EnumMap}.
*/
private final