[英]Import cannot be resolved?
所以現在,我有一個Brick.java,其中包含我要在BreakoutCourt.java中使用的Levels枚舉,它包含在同一個包中,即(默認包)。
當我寫入導入Brick.level; 在BreakoutCourt中,我收到一條消息,指出導入塊無法解析。 即使我寫了import static Brick.Level,我也會得到那條消息!
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
我在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);
}
我究竟做錯了什么? 感謝您的幫助!
如果要導入類的成員,則需要使用靜態導入 。 所以你可以這樣做:
import static Brick.Level;
但要小心。 應該謹慎使用靜態導入,如鏈接頁面所述。 在沒有靜態導入的情況下執行此操作的另一種方法是使用外部類名。 例如: Brick.Level.LUNATIC
原因是在較大的項目中,您可能有多個具有Level枚舉的類,您必須查看導入以查看正在使用的導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.