我正在尝试声明这个枚举:
public enum Month {
1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12;
}
但是当我尝试编译时它不起作用。这是因为常量是整数吗?
最佳答案
是的——枚举的值必须是有效的标识符。毕竟它们基本上是静态字段 - 您实际上是在尝试声明:
public static Month 1 = new Month();
这显然是无效的。
参见 Java Language Specification section 8.9有关详细信息,但特别是此产品:
EnumConstant:
Annotations Identifier Argumentsopt ClassBodyopt
关于java - 在枚举类型中使用整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6954225/