1
public class TestingClass {


    public static void main(String[] args) {

        int numberRooms = 6;
        double totalSpace;
        Room[] rooms = new Room[numberRooms];
        rooms[0] = new Room(20, 20);
        rooms[1] = new Room(20, 20);
        rooms[2] = new Room(22, 20);
        rooms[3] = new Room(20, 20);
        rooms[4] = new Room(25, 20);
        rooms[5] = new Room(15, 13);

         Garage garage = new Garage(20, "Cement", 1, 20, 1, 4);

        for (int i = 0; i < numberRooms; i++) {
            totalSpace = totalSpace + calculateRoomSpace(rooms[i]);
        }

        System.out.println("Total room space is " + totalSpace);

        foo(garage);

    }

    public static double calculateRoomSpace(Room roomSpace) {
        double newRoomSpace = roomSpace.calcFloorSpace();
        return newRoomSpace;
    }

    public static void foo(Building myBuilding) {
        System.out.println("Total floor space is " + myBuilding.calcFloorSpace());
    }
}

このコードを入力すると、エラーが発生します

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type Garage

    at TestingClass.main(TestingClass.java:17)"

ここで正確に何が問題なのですか?

編集 **これがGarageクラスです**

abstract public class Garage extends Building {


    private int cars;
    private String floorType;
    private int garageLength;
    private int garageWidth;

    public Garage(int floors, int windows, int cars, String floorType,
            int garageLength, int garageWidth) {
        super(floors, windows);
        this.cars = cars;
        this.floorType = floorType;
        this.garageLength = garageLength;
        this.garageWidth = garageWidth;
    }

    @Override
public double calcFloorSpace() {
    int floorSize;
    floorSize = garageLength * garageWidth;
    return floorSize;
}

    public int getCars() {
        return cars;
    }

    public void setCars(int cars) {
        this.cars = cars;
    }

    public String getFloorType() {
        return floorType;
    }

    public void setFloorType(String floorType) {
        this.floorType = floorType;
    }

    public int getGarageLength() {
        return garageLength;
    }

    public void setGarageLength(int garageLength) {
        this.garageLength = garageLength;
    }

    public int getGarageWidth() {
        return garageWidth;
    }

    public void setGarageWidth(int garageWidth) {
        this.garageWidth = garageWidth;
    }

    @Override
    public String toString() {
        return "Garage [cars=" + cars + ", floorType=" + floorType
                + ", garageLength=" + garageLength + ", garageWidth="
                + garageWidth + "]";
    }

}

お役に立てれば。これの専門家ではなく、これを理解するのにかなりの時間を費やしました。ありがとう

4

4 に答える 4

2

あなたの Garage クラスは抽象クラスとして宣言されており、インスタンス化できません。詳細については、これを確認してください。抽象クラスはコンストラクタを持つことができますか?

于 2013-07-23T04:54:23.047 に答える