Java枚举类型中的受保护变量是什么意思?
Java what does a protected variable mean in enum type?
在Java9中,我可以这样写代码:
enum Abc {
A, B, C;
static protected int foo = 4; // what is the purpose of the protected variables like this in enum?
}
我认为这没有意义,因为我们不能继承或实现枚举。
编辑:这个问题与Why are protected members allowed in final java classes?相同
这意味着它始终意味着:只能从同一包中的子classes 或classes 访问。你是对的,因为你不能继承 class,它在实践中与 package-private 字段没有什么不同。
在Java9中,我可以这样写代码:
enum Abc {
A, B, C;
static protected int foo = 4; // what is the purpose of the protected variables like this in enum?
}
我认为这没有意义,因为我们不能继承或实现枚举。
编辑:这个问题与Why are protected members allowed in final java classes?相同
这意味着它始终意味着:只能从同一包中的子classes 或classes 访问。你是对的,因为你不能继承 class,它在实践中与 package-private 字段没有什么不同。