编译器错误“找不到符号-类ENUM_NAME" [英] Compiler error "cannot find symbol - class ENUM_NAME"
问题描述
我已经定义了一个枚举.当我尝试写:
I have defined an enum. When I try to write:
private ENUM_NAME variableName;
作为类的实例变量,编译器告诉我:
as the instance variable of a class, the compiler tells me:
cannot find symbol - class ENUM_NAME
这阻止了我继续前进.
如果使用的话,我正在使用BlueJ IDE.所有文件都包含在一个程序包中,并且位于我的计算机上的同一文件夹中.我从另一个类(工作类的测试驱动程序)内部的枚举开始,然后在工作类中遇到了所述错误.然后,我尝试使枚举成为其自己的类,但出现了同样的错误.
I am using the BlueJ IDE, if that matters. All files are contained within one package, and are in the same folder on my computer. I began with the enum inside of another class (a test driver for my working class), and I got the stated error in my working class. Then I tried making the enum its very own class, and I got the same error.
测试驱动程序类:
public class BicycleMainClass
{
enum BICYCLE_TYPE_ENUM {STANDARD, RACER, MOUNTAIN, BMX};
//main method below
}
工作"类:
public class BicycleClass
{
private BICYCLE_TYPE_ENUM bicycleType;
//this line gives me the error "cannot find symbol - class BICYCLE_TYPE_ENUM"
}
我希望它可以编译而不会出错,因为枚举是有效的变量类型.我不明白什么?
I would expect it to compile without error, because enums are a valid variable type. What am I not understanding?
推荐答案
The problem is that you have declared your enum type as a nested type of BicycleMainClass
, so you need to include the outer type when referring to it:
class BicycleClass
{
private BicycleMainClass.BICYCLE_TYPE_ENUM bicycleType;
}
另一种解决方案是简单地将您的枚举类型声明为顶级类型:
Another solution is simply to declare your enum type as a top-level type:
public enum BICYCLE_TYPE_ENUM {
STANDARD, RACER, MOUNTAIN, BMX;
}
然后您可以直接参考它:
Then you can refer to it directly:
private BICYCLE_TYPE_ENUM bicycleType;
这篇关于编译器错误“找不到符号-类ENUM_NAME"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!