Java 枚举内部类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34476392/
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-08-11 15:41:21  来源:igfitidea点击:

Enum Inside Class

javaenums

提问by coolDude

I am trying to define an enum within a class but Eclipse continues to underline my code in the lovely red color. Here is what my code looks like for the most part:

我试图在一个类中定义一个枚举,但 Eclipse 继续用可爱的红色在我的代码下划线。这是我的代码的大部分内容:

package campusvilleFoodBank;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class FoodArrayList {

    public enum Days {SUN, MON, TUE, WED, THUR, FRI, SAT}

    private Food [] foodArray;
    private int iterator;
    private boolean startIndexing;
    private static final int MAX = 24;

    public FoodArrayList(){
        foodArray = new Food [MAX];
        iterator = 0;
    }

    public FoodArrayList(int arraySize){
        foodArray = new Food [arraySize];
        iterator = 0;
    }
//Rest of code, with more complicated methods here
}

I must be missing something very simple, it's likely to be a syntax error.... The code that follows involves methods such as my toString(), some sorting methods for the foodArrayinstance variable, and things along those lines. I do not have a main method defined. I know that I can define an enum within a class definition and all of the code I've seen elsewhere is written nearly exactly the same as to what I have. However, Eclipse does not seem to recognize even the enumdeclaration and I don't believe I need to import anything.

我一定遗漏了一些非常简单的东西,它很可能是语法错误....后面的代码涉及诸如 my 之类的toString()方法、foodArray实例变量的一些排序方法以及这些方面的事情。我没有定义主要方法。我知道我可以在类定义中定义一个枚举,并且我在其他地方看到的所有代码的编写方式几乎与我所拥有的完全相同。但是,Eclipse 似乎甚至无法识别enum声明,我认为我不需要导入任何内容。

Here is what I see in Eclipse for the code I have an error with:

这是我在 Eclipse 中看到的错误代码:

Code with error

Code with error

For comparison, here is some enum code from another, separate .java file that appears to be without error:

为了进行比较,这里有一些来自另一个似乎没有错误的单独 .java 文件的枚举代码:

Code without error

Code without error

Any assistance on this would be appreciated. Let me know if further clarification is needed.

对此的任何帮助将不胜感激。让我知道是否需要进一步澄清。

采纳答案by esteban rincon

It could be a couple of things but not related to enum, this is fine.

这可能是几件事,但与 无关enum,这很好。

  1. Seems to me that unless Fooddoes exist, it MUSTbe created
  2. If it doesexist, then it must reside in a different package, which in turn you have to importit like this

    import package_name.package2.Food;

  3. It could be that your classname is not identicalto you filename

    FoodArrayList

  4. Sometimes your IDEin this case Eclipseneeds some help, try saving the file, closing it and opening it back up.

  1. 在我看来,除非Food确实存在,否则必须创建它
  2. 如果它确实存在,那么它必须驻留在不同的包中,反过来你必须import像这样

    import package_name.package2.Food;

  3. 可能是你的class名字和你的名字不一样file

    FoodArrayList

  4. 有时您IDE在这种情况下Eclipse需要一些帮助,请尝试保存文件,关闭它并重新打开它。

回答by harini

you have a semicolon missing at the end of the enum in the FoodArrayList class

您在 FoodArrayList 类中的枚举末尾缺少分号