1

だから今、同じパッケージに含まれているBreakoutCourt.javaで使用したいLevels列挙型を含むBrick.javaがあります。これは(デフォルトパッケージ)です。

import Brick.level; と書くと、BreakoutCourt で、The Import Brick Cannot Be Resolved というメッセージが表示されます。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);
        }

私は何を間違っていますか?お手伝いありがとう!

4

2 に答える 2

4

クラスのメンバーをインポートする場合は、 static importを使用する必要があります。したがって、次のことができます。

import static Brick.Level;

ただし、注意してください。リンクされたページに記載されているように、静的インポートは控えめに使用する必要があります。静的インポートなしでそれを行う別の方法は、外部クラス名を使用することです。例:Brick.Level.LUNATICその理由は、大規模なプロジェクトでは Level 列挙型を持つ複数のクラスが存在する可能性があり、どれが使用されているかを確認するためにインポートを確認する必要があるためです。

于 2012-04-25T03:37:38.943 に答える
0

Enum をどのようにインポートしていますか?

次のようなインポートステートメントが必要です

import static Brick.Level

関連する質問

于 2012-04-25T03:38:10.760 に答える