在下面的代码示例中,我试图测试父类中枚举的值。 我得到的错误是“p.theEnum无法解析或不是字段。”,但它是我在父类中用来测试值的完全相同的代码(显然没有p。)。
我哪里错了? :)
public class theParent { protected static enum theEnum { VAL1, VAL2, VAL3 }; private theEnum enumValue = theEnum.VAL1; theParent() { this.theChild = new theChild(this); this.theChild.start(); } class theChild { private parentReference p; public theChild (theParent parent) { this.p = parent; } public void run() { // How do I access theEnum here? if (p.enumValue == p.theEnum.VAL1) { } } } }In the code example below, I'm trying to test the value of an enum in the parent class. The error I get is "p.theEnum cannot be resolved or is not a field.", but it's the exact same code I use in the parent class to test the value (without the p. obviously).
Where am I going wrong? :)
public class theParent { protected static enum theEnum { VAL1, VAL2, VAL3 }; private theEnum enumValue = theEnum.VAL1; theParent() { this.theChild = new theChild(this); this.theChild.start(); } class theChild { private parentReference p; public theChild (theParent parent) { this.p = parent; } public void run() { // How do I access theEnum here? if (p.enumValue == p.theEnum.VAL1) { } } } }最满意答案
只需将其更改为:
if (p.enumValue == theEnum.VAL1) { }没有必要对其进行限定。
(就像一个FYI一样,如果你将这样的样本编译成问题区域,它会有所帮助 - 除了上面那个之外我还要做一些改变才能编译它。)
Just change it to:
if (p.enumValue == theEnum.VAL1) { }There's no need to qualify it.
(Just as an FYI, it would help if you'd make samples like this compile apart from the problem area - I had to make quite a few changes aside from the one above before I could make it compile.)
更多推荐
发布评论