Java 中的enum

枚举应该是一个有限集合组成的一个对象

一个简单的例子:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

枚举最简单的就是当常量用,但是比常量安全,最常用的就是在switch里面。
其实枚举可以当个普通的类使用,可以有变量,方法等。
当普通类使用的时候,其中有几个注意点:

  • enum不可以有子类,因为enum是final的,
  • enum不是继承其它类,因为它默认已经继承了Enum,但是可以实现Interface
  • 构造方法是private或者是默认,不能是public的或者protected的

下面一个用作普通类的例子:

package tenum;

interface Next<T> {
    T next();
}

public enum Week implements Next<Week> {
    SUNDAY("Today is Sunday."), MONDAY("Today is Monday"), TUESDAY("Today is Tuesday"), WEDNESDAY(
            "Today is wednesday"), THURSDAY(
                    "Today is Thursday"), FRIDAY("Today is Firday"), SATURDAY("Today is Saturday");

    private String description;

    Week(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public Week next() {
        switch (this) {
        case SUNDAY:
            return MONDAY;
        case MONDAY:
            return TUESDAY;
        case TUESDAY:
            return WEDNESDAY;
        case WEDNESDAY:
            return THURSDAY;
        case THURSDAY:
            return FRIDAY;
        case FRIDAY:
            return SATURDAY;
        case SATURDAY:
            return SUNDAY;
        }
        return null;
    }

    public static void main(String[] args) {
        for (Week w : values()) {
            System.out.println(w.getDescription());
        }
        System.out.println("------------------------------");
        for (Week w : Week.class.getEnumConstants()) {
            System.out.println(w.getDescription());
        }
        System.out.println("------------------------------");
        //星期一之后2天是周几
        System.out.println(Week.MONDAY.next().next().getDescription());
    }

}

输出结果:

Today is Sunday.
Today is Monday
Today is Tuesday
Today is wednesday
Today is Thursday
Today is Firday
Today is Saturday
------------------------------
Today is Sunday.
Today is Monday
Today is Tuesday
Today is wednesday
Today is Thursday
Today is Firday
Today is Saturday
------------------------------
Today is wednesday

上面两种来获取所有元素,一种是调用values()方法,一种是通过class 对象的getEnumConstants()方法。
有趣的是values()方法,enum 是继承Eunm.java,但是Enum.java里面并没有values()方法。怎么来的呢?其实是编译器为我们自动生成的。如果你自己在定义这个方法,编译器就会报错“The enum Week already defines the method values() implicitly”。
让我们来看看编译器生成的class 文件。

Warning: Binary file Week contains tenum.Week
Compiled from "Week.java"
public final class tenum.Week extends java.lang.Enum<tenum.Week> implements tenum.Next<tenum.Week> {

  public static final tenum.Week SUNDAY;
  public static final tenum.Week MONDAY;
  public static final tenum.Week TUESDAY;
  public static final tenum.Week WEDNESDAY;
  public static final tenum.Week THURSDAY;
  public static final tenum.Week FRIDAY;
  public static final tenum.Week SATURDAY;
  static {};
  public java.lang.String getDescription();
  public tenum.Week next();
  public static void main(java.lang.String[]);
  public static tenum.Week[] values();
  public static tenum.Week valueOf(java.lang.String);
  public java.lang.Object next();
  static int[] $SWITCH_TABLE$tenum$Week();
}

从上面可以看到几点,1)class 是final的,2)自动生成values()方法,3)继承了Enum类

参考:
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
https://www.ibm.com/developerworks/cn/java/j-lo-enum/index.html

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 29,992评论 18 399
  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 1,480评论 0 3
  • 梦里不知身是客。一晌贪欢。
    第四根指尾凉阅读 100评论 0 0
  • 02 那我以后可以喊你男神吗 在我们学校,高三党的日子并不好过,除了像我一样的通校生,那些住宿的学生只能...
    夏暖琼阅读 87评论 0 0
  • 不一样的卡梅拉之我要救出贝里奥。 卡梅利多和卡门我了帮助贝里奥的羊毛不被剪掉,把他带到一片神秘的巨石林,没想到在那...
    葛奕轩阅读 84评论 0 0
  • 叶子睡觉前照常打开微信朋友圈准备刷下朋友们的动态,顺手点个赞评个论什么的,叶子似乎已经习惯了这种睡前和远方朋友的互...
    蓝雨坊的小幸福阅读 212评论 0 0