gpt4 book ai didi

java - Java 中的父级枚举

转载 作者:行者123 更新时间:2023-11-29 08:17:33 25 4
gpt4 key购买 nike

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

最佳答案

只需将其更改为:

if (p.enumValue == theEnum.VAL1) { }

没有必要限定它。

(仅供引用,如果您在问题区域之外制作像这样的示例编译会有所帮助 - 在我可以之前,我必须对上面的示例进行相当多的更改 aside让它编译。)

关于java - Java 中的父级枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com