在下面的代码中,枚举值的成员字段在枚举定义之外不可访问:ColoursGREEN
public class Test {
enum Colours {
RED,
GREEN {
public static final int hex = 0x00ff00;
public final int hex2 = 0x00ff00; // Try it without static, just in case...
void f() {
System.out.println(hex); // OK
System.out.println(hex2); // OK
}
},
BLUE
}
public static void main(String[] args) {
System.out.println(Colours.GREEN.hex); // COMPILE ERROR
System.out.println(Colours.GREEN.hex2); // COMPILE ERROR
}
}
有问题的行会导致以下编译器错误:
Error:(38, 41) java: cannot find symbol
symbol: variable hex
location: variable GREEN of type Test.Colours
任何想法为什么这不起作用?我假设Java标准禁止它,但为什么呢?
PIPIONE
互换的青春
相关分类