在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的。
例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的“数据集”。
在java中如何更好的使用这些“数据集”呢?因此枚举便派上了用场,以下代码详细介绍了枚举的用法。
-
-
-
-
- public class TestEnum {
-
-
-
-
- public enum ColorEnum {
- red, green, yellow, blue;
- }
-
-
-
-
-
- public enum SeasonEnum {
-
- spring, summer, autumn, winter;
-
- private final static String position = "test";
-
- public static SeasonEnum getSeason() {
- if ("test".equals(position))
- return spring;
- else
- return winter;
- }
- }
-
-
-
-
-
-
-
- public enum Gender{
-
-
- MAN("MAN"), WOMEN("WOMEN");
-
- private final String value;
-
-
- Gender(String value) {
- this.value = value;
- }
-
- public String getValue() {
- return value;
- }
- }
-
-
-
-
-
-
-
- public enum OrderState {
-
- CANCEL {public String getName(){return "已取消";}},
-
- WAITCONFIRM {public String getName(){return "待审核";}},
-
- WAITPAYMENT {public String getName(){return "等待付款";}},
-
- ADMEASUREPRODUCT {public String getName(){return "正在配货";}},
-
- WAITDELIVER {public String getName(){return "等待发货";}},
-
- DELIVERED {public String getName(){return "已发货";}},
-
- RECEIVED {public String getName(){return "已收货";}};
-
- public abstract String getName();
- }
-
- public static void main(String[] args) {
-
- ColorEnum colorEnum = ColorEnum.blue;
- switch (colorEnum) {
- case red:
- System.out.println("color is red");
- break;
- case green:
- System.out.println("color is green");
- break;
- case yellow:
- System.out.println("color is yellow");
- break;
- case blue:
- System.out.println("color is blue");
- break;
- }
-
-
- System.out.println("遍历ColorEnum枚举中的值");
- for(ColorEnum color : ColorEnum.values()){
- System.out.println(color);
- }
-
-
- System.out.println("ColorEnum枚举中的值有"+ColorEnum.values().length+"个");
-
-
- System.out.println(ColorEnum.red.ordinal());
- System.out.println(ColorEnum.green.ordinal());
- System.out.println(ColorEnum.yellow.ordinal());
- System.out.println(ColorEnum.blue.ordinal());
-
-
- System.out.println(ColorEnum.red.compareTo(ColorEnum.green));
-
- System.out.println("===========");
- System.err.println("季节为" + SeasonEnum.getSeason());
-
- System.out.println("===========");
- for(Gender gender : Gender.values()){
- System.out.println(gender.value);
- }
-
- System.out.println("===========");
- for(OrderState order : OrderState.values()){
- System.out.println(order.getName());
- }
- }
-
- }
java.lang中Enum.class文件的内容
-
- public abstract class Enum<E extends Enum<E>>implements Comparable<E>, Serializable {
- /*枚举的name*/
- private final String name;
- /*枚举的name通过name()方法获取*/
- public final String name() {
return name;
}
- }
-
enum 的语法结构尽管和 class 的语法不一样,但是经过编译器编译之后产生的是一个class文件。该class文件经过反编译可以看到实际上是生成了一个类,该类继承了java.lang.Enum<E>。所以,实际上 enum 就是一个 class,只不过 java 编译器帮我们做了语法的解析和编译而已。可以把 enum 看成是一个普通的 class,它们都可以定义一些属性和方法,不同之处是:enum 不能使用 extends 关键字继承其他类,因为 enum 已经继承了 java.lang.Enum(java是单一继承)。
向枚举中添加新方法
如果打算自定义自己的方法,那么必须在enum实例序列的最后添加一个分号。而且 Java 要求必须先定义 enum 实例。
- public enum Color {
- RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
-
- private String name;
- private int index;
-
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
-
- public static String getName(int index) {
- for (Color c : Color.values()) {
- if (c.getIndex() == index) {
- return c.name;
- }
- }
- return null;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- }