家长在Java中的枚举(Parent's Enum in Java)

编程入门 行业动态 更新时间:2024-10-28 17:28:49
家长在Java中的枚举(Parent's Enum in Java)

在下面的代码示例中,我试图测试父类中枚举的值。 我得到的错误是“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.)

更多推荐

本文发布于:2023-08-05 02:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1426142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:家长   Java   Enum   Parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!