简体   繁体   English

导入无法解决?

[英]Import cannot be resolved?

So right now, I have a Brick.java which contains a Levels enum that I want to use in BreakoutCourt.java, which is contained in the same package, which is (default package). 所以现在,我有一个Brick.java,其中包含我要在BreakoutCourt.java中使用的Levels枚举,它包含在同一个包中,即(默认包)。

When I write import Brick.level; 当我写入导入Brick.level; in BreakoutCourt , I get a message that says The Import Brick Cannot Be Resolved. 在BreakoutCourt中,我收到一条消息,指出导入块无法解析。 I get that message even if I write import static Brick.Level! 即使我写了import static Brick.Level,我也会得到那条消息!

The levels enum contained in Brick.java looks like this: Brick.java中包含的级别枚举如下所示:

public class Brick {
   public static final int BWIDTH = 60;
   public static final int BHEIGHT = 20;
   private int xPos, yPos; 
   private Level brickLevel;

    //This sets up the different levels of bricks.
   enum Level{
    LUNATIC (4, 40, Color.MAGENTA),
    HARD (3, 30, Color.PINK), 
    MEDIUM (2, 20, Color.BLUE),
    EASY (1, 10, Color.CYAN),
    DEAD (0, 0, Color.WHITE);
    private int hitpoints;
    private int points;
    private Color color;

    Level(int hitpoints, int points, Color color){
        this.hitpoints = hitpoints;
        this.points = points;
        this.color=color;
        }
    public int getPoints(){
        return points;
        }
    public Color getColor(){
        return color;
        }
}

//rest of brick class goes under the enum

And I use it in BreakoutCourt like this: 我在BreakoutCourt中使用它像这样:

    //Generates the bricks.
    for(int i = 0; i < 8; ++i){
        ArrayList<Brick> temp = new ArrayList<Brick>();
        Level rowColor = null;
        switch(i){
        //There are two rows per type of brick.
            case 0:
            case 1:
                rowColor = Level.EASY;
                break;
            case 2:
            case 3:
                rowColor = Level.HARD;
                break;
            case 4:
            case 5:
                rowColor = Level.LUNATIC;
                break;
            case 6:
            case 7:
            default:
                rowColor = Level.MEDIUM;
                break;
        }
        for(int j = 0; j < numBrick; j++){
            Brick tempBrick = new Brick((j * Brick.BWIDTH), ((i+2) * Brick.BHEIGHT), rowColor);
            temp.add(tempBrick);
        }

What am I doing wrong? 我究竟做错了什么? Thank you for the help! 感谢您的帮助!

If you want to import a member of a class, you need to use a static import . 如果要导入类的成员,则需要使用静态导入 So you could do: 所以你可以这样做:

import static Brick.Level;

Be careful though. 但要小心。 Static imports should be used sparingly, as noted by that linked page. 应该谨慎使用静态导入,如链接页面所述。 Another way to do it without a static import is to use the outer class name. 在没有静态导入的情况下执行此操作的另一种方法是使用外部类名。 For example: Brick.Level.LUNATIC The reason is that in a larger project you might have multiple classes with a Level enum, and you would have to look at the import to see which one is being used. 例如: Brick.Level.LUNATIC原因是在较大的项目中,您可能有多个具有Level枚举的类,您必须查看导入以查看正在使用的导入。

How are you importing Enum ? 你是如何进口Enum的?

You should have an import statement like 你应该有像这样的import语句

import static Brick.Level

Related question 相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM