为什么不能在 Java 中保护或公开枚举构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3664077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't enum constructors be protected or public in Java?
提问by Bad Request
The whole question is in the title. For example:
整个问题都在标题中。例如:
enum enumTest {
TYPE1(4.5, "string1"), TYPE2(2.79, "string2");
double num;
String st;
enumTest(double num, String st) {
this.num = num;
this.st = st;
}
}
The constructor is fine with the default or private
modifier, but gives me a compiler error if given the public
or protected
modifiers.
构造函数可以使用默认值或private
修饰符,但如果给定public
或protected
修饰符,则会出现编译器错误。
采纳答案by jjnguy
Think of Enums as a class with a finite number of instances. There can never be any different instances beside the ones you initially declare.
将枚举视为具有有限数量实例的类。除了您最初声明的实例之外,永远不会有任何不同的实例。
Thus, you cannot have a public or protected constructor, because that would allow more instances to be created.
因此,您不能拥有公共或受保护的构造函数,因为这将允许创建更多实例。
Note: this is probably not the official reason. But it makes the most sense for me to think of enums
this way.
注意:这可能不是官方原因。但这样想对我来说是最有意义的enums
。
回答by Cristian Sanchez
Because you cannot call the constructor yourself.
因为你不能自己调用构造函数。
Here is what the tutorials on Enumshas to say:
Note:The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.
注意:枚举类型的构造函数必须是包私有或私有访问。它会自动创建在枚举体开头定义的常量。您不能自己调用枚举构造函数。
回答by harto
Enums contain a fixed set of values, which must all be known at compile-time. It doesn't make sense to create new literals at run-time, which would be possible if the constructor were visible.
枚举包含一组固定的值,这些值必须在编译时全部已知。在运行时创建新文字是没有意义的,如果构造函数可见,这将是可能的。
回答by Rahul Prajapat
The key point to remember is that an enums that is not enclosed in a class can be declared with only the publicor defaultmodifier, just like a non-inner class.
要记住的关键点是,未包含在类中的枚举可以仅使用public或default修饰符声明,就像非内部类一样。
回答by Praveen Kishor
This is because enum is Java contains fixed constant values. So, there is no point in having public or protected constructor as you cannot create instance of enum.
这是因为 enum 是 Java 包含固定常量值。因此,拥有公共或受保护的构造函数是没有意义的,因为您无法创建枚举的实例。
Also, note that internally enum is converted to class as below.
另外,请注意,内部枚举被转换为类,如下所示。
enum Color {
RED, BLUE, YELLOW;
}
This will internally converted to:
这将在内部转换为:
public final class Color {
private Color() {}
public static final Color RED = new Color();
public static final Color BLUE = new Color();
public static final Color YELLOW = new Color();
}
So, every enum constant is represented as an object of type enum. As we can't create enum objects explicitly hence we can't invoke enum constructor directly.
因此,每个枚举常量都表示为枚举类型的对象。由于我们无法显式创建枚举对象,因此我们无法直接调用枚举构造函数。