如何获取公共内部枚举的所有元素的列表?

时间:2015-01-12 15:54:36

标签: java enums inner-classes

鉴于此课程:

public class Outer {

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

}

我尝试了new Outer().Day.values();new Outer().Day.getEnumConstants();,但我得到了Day cannot be resolved or is not a field

知道如何访问它吗?

2 个答案:

答案 0 :(得分:3)

您可以访问如下:Outer.Day.values();枚举Day可以像静态变量一样公开访问。

答案 1 :(得分:1)

我使用IDEONE运行您的代码,这是我的解决方案(根据您的需求调整):

public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  }
    public static void main (String[] args) throws java.lang.Exception
    {
        Day[] d = Outer.Day.values();
        for (Day dd : d){
            System.out.println(dd);
        }
    }

链接是 - http://ideone.com/B1qLKg

这就是你想要的吗?