【发布时间】:2013-02-15 14:42:55
【问题描述】:
public enum Batman
{
Rat, Cat, Bat;
private boolean isMatch;
// Constructor
Batman()
{
this.isMatch = (this.compareTo(Bat) == 0) ? true : false;
}
public boolean isMatch()
{
return this.isMatch;
}
}
对于构造函数行,我收到错误: 无法在初始化程序中引用静态枚举字段 Batman.Bat
我主要是想弄清楚是否可以在构造函数中识别特定的 ENUM。
另外我想保存“isMatch”值的原因是我不想每次都评估它应该是什么。
我从一开始就知道,所以我只想保存值,因此当调用时它不是评估
但只是将值传回
我知道还有其他方法可以解决这个问题:
-
修改构造函数接受参数:
老鼠(假),猫(假),蝙蝠(真);
// Constructor Batman(boolean isMatch) { this.isMatch = isMatch; } 更改 isMatch()
public boolean isMatch() { return (this.compareTo(Bat) == 0) ? true : false; }
任何建议都会很棒。
谢谢
【问题讨论】:
-
您不必在第二个示例中更改
isMatch()。 -
您的
isMatch应该只返回您为枚举设置的布尔字段的值。看起来你正试图让你的生活变得不必要地困难。 -
你做不到。您正在尝试与尚不存在的事物进行比较。我喜欢你的选项2。
-
这个问题没有意义!!!
标签: java constructor enums