2013-07-23 91 views
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)" 

有什麼問題就在這裏到底是什麼?

編輯 **這裏是車庫類**

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

請問你'Garage'類是什麼樣子? –

+0

你的車庫班在哪裏? – Nargis

+0

''Garage'是否位於'TestingClass'目錄之外?如果是這樣,你需要'輸入'它。 – mattbdean

回答

2

你不能實例化一個抽象類。

你有兩個選擇:

  • 從類聲明中刪除abstract,或
  • 再創建一個類來擴展Garage,它可以被實例化。
0

車庫是一個抽象類,所以它不能被實例化。只有它的子類可以被實例化。

1

您不能實例化abstract類的實例。你需要讓你的類具體化(刪除類的abstract修飾符併爲所有方法提供一個實現),或者擴展抽象類來創建一個非抽象類。

當您刪除abstract修飾符時,編譯器會告訴您缺少哪些抽象方法(如果有)。