如何在 Java 中使用枚举复制构造函数?
我正在尝试完成一个项目,尽管我已经尝试过,但我似乎无法做到这一点。这是枚举:
public enum Symbols {
/**
* The Seven
*/
SEVEN(12,"images/seven.jpg"),
/**
* The Watermelon
*/
WATERMELON(10,"images/watermelon.jpg"),
/**
* The Orange
*/
ORANGE(8,"images/orange.jpg"),
/**
* The Plum
*/
PLUM(6,"images/plum.jpg"),
/**
* The Lemon
*/
LEMON(4,"images/lemon.jpg"),
/**
* The Cherry
*/
CHERRY(2,"images/cherry.jpg");
private int payout; // Symbol payout value
private BufferedImage image; // Symbol image
private String icon; // Symbol file name
/** Constructor - Payout must be positive and even, file name must not be null
* @param payout - Symbol payout amount
* @param icon - Symbol image file name
*/
Symbols(int payout, String icon){
this.payout = payout;
this.icon = icon;
loadImage(icon);
}
/** Copy Constructor - Symbol must not be null
* @param s - A single symbol
*/
Symbols(Symbols s){
payout = s.payout;
icon = s.icon;
loadImage(icon);
}
现在我可以进入 main 并创建一个名为“s”的符号,如下所示:
Symbols s = Symbols.CHERRY;
但是我在使用给定的复制构造函数制作“s”的副本时遇到了麻烦:
Symbols t = Symbols(s);
如果我尝试这样做,我会得到错误:“Symbols 类型的方法 Symbols(Symbols) 未定义。”
谢谢
I'm trying to complete a project, and though I have tried, I can't seem to do this. Here's the enum:
public enum Symbols {
/**
* The Seven
*/
SEVEN(12,"images/seven.jpg"),
/**
* The Watermelon
*/
WATERMELON(10,"images/watermelon.jpg"),
/**
* The Orange
*/
ORANGE(8,"images/orange.jpg"),
/**
* The Plum
*/
PLUM(6,"images/plum.jpg"),
/**
* The Lemon
*/
LEMON(4,"images/lemon.jpg"),
/**
* The Cherry
*/
CHERRY(2,"images/cherry.jpg");
private int payout; // Symbol payout value
private BufferedImage image; // Symbol image
private String icon; // Symbol file name
/** Constructor - Payout must be positive and even, file name must not be null
* @param payout - Symbol payout amount
* @param icon - Symbol image file name
*/
Symbols(int payout, String icon){
this.payout = payout;
this.icon = icon;
loadImage(icon);
}
/** Copy Constructor - Symbol must not be null
* @param s - A single symbol
*/
Symbols(Symbols s){
payout = s.payout;
icon = s.icon;
loadImage(icon);
}
Now I can go into main and create a symbol named "s" like this:
Symbols s = Symbols.CHERRY;
But I'm having trouble making a copy of "s" using the copy constructor given:
Symbols t = Symbols(s);
If I try to do this, I get the error: "The method Symbols(Symbols) is undefined for the type Symbols."
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,你需要将其公开,但仍然行不通。 Java 中没有复制构造函数;你不能“新建”一个枚举,这是下一个错误;无论如何,Java 都会竭尽全力确保每个 JVM 中每个枚举值只有一份副本。您认为需要复制构造函数到底有什么用?
解决方案:
Well you need to make it public but it still isn't going to work. There are no copy constructors in Java; you can't 'new' an Enum, which is the next error; and in any case Java goes to great lengths to ensure there is only one copy of each enum value per JVM. What exactly do you think you need a copy constructor for?
Solution:
枚举应该是一个不可变的集合 - 你不应该能够创建新的集合。并且构造函数被强制为私有(即使没有标记为私有)。
因为它们是不可变的,所以你总是应该保证,如果你有一个 Symbols.CHERRY 对象的实例,它总是同一个实例,所以你可以做诸如测试对象等价而不是 equals 之类的事情 - 即你可以做
而不是
因为如果你必须 CHERRY,它们保证是同一个对象。
这更倾向于成为 Typesafe Enum 模式的强制版本:
enums are supposed to be an immutable set - you're not supposed to be able to make new ones. And the constructors are enforced as private (even though not marked as such).
Since they're immutable you are always supposed to be guaranteed that if you have an instance of a Symbols.CHERRY object it's always the same instance, so you can do things like test for object equivalence instead of equals - i.e. you can do
instead of
because if you have to CHERRYs they're guaranteed to be the same object.
This is more intended to be an enforced version of the Typesafe Enum pattern:
好吧,只有当您调用
new
关键字时才会调用构造函数:如果没有
new
,它会认为您正在尝试调用名为Symbols
的方法。但这在这种情况下是无关紧要的,因为您无法在常量定义之外实例化枚举。如果可以的话,它就不会是一个枚举。
让我们来看看为什么您想要拥有一个枚举的副本。如果您需要副本,那一定是因为您说枚举是有状态的,但这一定不是真的。枚举必须是不可变的。由于它们必须是不可变的,因此您可以在任何地方共享实例。
Well, constructors are only called when you invoke the
new
keyword:Without
new
it thinks you're trying to invoke a method namedSymbols
.But that's irrelevant in this case because you can't instantiate an enum outside of the constant definitions. If you could, it wouldn't be an enum.
Let's examine why you'd ever want to have a copy of an enum. If you need a copy, it's necessarily because you're saying that the enum is stateful, which must not be true. Enums must be immutable. And since they must be immutable, you can share the instances anywhere.
您使用类似这样的枚举:
其他答案很好地解释了为什么不能使用复制构造函数。基本上,枚举类的“实例”(SEVEN 和 WATERMELON)是不可变的,因此您不能在运行时在其中指定任何其他值,也不能创建新值。
You use ENUMs something like this:
Other answers gave a nice explanation on why you cannot have copy constructors. Basically the "instances" (SEVEN and WATERMELON) of your enum class are immutable, so you cannot specify any other value inside them at run time and you cannot create new ones.
枚举不是普通的类。您无法手动构建它们。枚举“类”本身基本上是一个“抽象”类。每个值都作为枚举“类”的单独特定子类实现。
enums are not normal classes. you cannot manually construct them. the enum "class" itself is basically an "abstract" class. each value is implemented as a separate specific subclass of the enum "class".
枚举是不允许创建新对象的类。所以你的 Symbol 类(枚举)只有六个实例。您无法调用枚举构造函数,但可以拥有为六个实例创建的引用的副本。
另一方面,如果你想要一个高的Java构造函数,你需要使用关键字“new”。
Enum are classes that don't let the creation of new objects. So your Symbol class (enum) only has six instances. You can't call a enum constructor, but you can have a copy of the references created for the six instances.
In the other hand if you want tall a constructor in Java you need to use the keyword "new".