为什么枚举无法在 JAVA 中解析?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6544054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:17:38  来源:igfitidea点击:

why enum could not be resolved in JAVA?

javaenums

提问by huahsin68

I was using J2EE Eclipse Indigo, and I have three class declare like this:

我使用的是 J2EE Eclipse Indigo,我有三个类声明如下:

public interface ClassA {
   public static enum TYPE { TYPE1, TYPE2 };
}

public interface ClassB extends ClassA {

}

public class ClassC implements ClassB {
   System.out.println(TYPE.TYPE1);
}

There was a compilation error on TYPE in ClassC. It complain that "enum cannot be resolved to a type". And also a warning for enum in ClassA, it complain that:

ClassC 中的 TYPE 存在编译错误。它抱怨“枚举无法解析为类型”。还有一个对 ClassA 中的 enum 的警告,它抱怨说:

Multiple markers at this line
 - 'enum' should not be used as an identifier, since it is a reserved keyword from source level 1.5 on
 - enum cannot be resolved to a type
 - Syntax error, insert ";" to complete FieldDeclaration

May I know what cause the error in this code?

我可以知道是什么导致了这段代码中的错误吗?

回答by stuhpa

I had a similar problem:

我有一个类似的问题:

enum can't be resolved to a type

枚举无法解析为类型

Eclipse offered to import Enuminstead.

Eclipse 提议改为导入Enum

I went to

我去了

  • Preferences->Java->Installed_JREs->Execution_environment;
  • Selected JavaSE-1.6 in "Execution Environments" pane; and
  • Checked jre6 in Compatible JREspane.
  • 首选项->Java->Installed_JREs->Execution_environment;
  • 在“执行环境”窗格中选择 JavaSE-1.6;和
  • Compatible JREs窗格中检查 jre6 。

After rebuild enumwas recognized properly.

重建enum后被正确识别。

回答by Bozho

  • the first compiles fine. Check your compiler level (should be at least 1.5)
  • inteRface
  • you cannot put code in the method body, as you did in ClassC. It should be in a method or in a block
  • 第一个编译正常。检查您的编译器级别(应至少为 1.5)
  • inteRface
  • 您不能像在ClassC. 它应该在方法或块中

回答by Roland Illig

You mistyped intefacefor interface.

你打错intefaceinterface

Maybe your compiler is too old, so that it doesn't know that enumis a keyword.

也许你的编译器太旧了,以至于它不知道这enum是一个关键字。

回答by AlexR

You must write code either in a method or in a staticblock (assigning static values). Your System.out.println()is written into a class. Create method and put System.out.println()there.

您必须在方法或static块中编写代码(分配静态值)。你的System.out.println()被写入一个class. 创建方法并放在System.out.println()那里。