枚举类在同一文件夹层级中不可见。

huangapple go评论63阅读模式
英文:

Enum Class not visible in the same folder level

问题

我正在尝试从一个与我要访问它的类位于同一文件夹级别的单独文件中访问枚举类型。

以下是这两个文件:

Number.java:

public enum Number {
    ONE, TWO, THREE
}

mainClass.java:

public class mainClass {

    private Number numVar;

    mainClass(Number num) {
        numVar = num;
    }
}

然而,mainClass无法将'Number'识别为有效的类型。具体来说,'Number'无法解析为类型。有任何想法是什么原因吗?

英文:

I'm trying to access an enum type from a separate file, which is in the same folder level as the class I'm trying to access it from.

These are the two files:

Number.java:

public enum Number {
    ONE, TWO, THREE
}

mainClass.java:

public class mainClass {

    private Number numVar;

    mainClass(Number num) {
        numVar = num;
    }
}

However, mainClass doesn't recognise 'Number' as a valid type. Specifically 'Number' cannot be resolved to a type. Any idea why?

答案1

得分: 3

Number是Java中的一个标准类。您应该重新命名您的枚举或者通过以下方式引用它:

private you_package.Number numVar;

但是我建议您重新命名您的枚举,并且不要将任何类或枚举命名为标准类的名称。这是官方文档链接。

英文:

Number is a standard class in Java.
You should rename your Enum or reference it through

private you_package.Number numVar;

But I suggest you to rename you Enum and never name any class/enum as a standard one. Here's the official documentation of this class.

huangapple
  • 本文由 发表于 2020年9月23日 20:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定