2011-08-13 98 views
0

我正在編寫一個從另一個類(HotelRoom)繼承的類(Suite)。 HotelRoom類有一個構造函數,它需要一個參數(一個int),所以在我稱之爲超級(空間)的套件構造函數中,我可以告訴它應該工作。 HotelRoom符合要求,但是Suite給出了構造函數錯誤。任何幫助將不勝感激。這裏是我下面的代碼:Java找不到符號:構造函數錯誤

public class HotelRoom 
{ 
    private int roomNumber; 
    protected double nightlyRate; 
    private final int maxRoomNumber = 999; 
    boolean didEnterCorrectRoomNumber = false; 

    public HotelRoom(int room) 
    { 

     if (room > 0 && room <= 299) 
     { 
      nightlyRate = 69.95; 
      didEnterCorrectRoomNumber = true; 
      //return didEnterCorrectRoomNumber; 
     } 
     else if (room > 299 && room <= maxRoomNumber) 
     { 
      nightlyRate = 89.95; 
      didEnterCorrectRoomNumber = true; 
      //return didEnterCorrectRoomNumber; 
     } 
     else 
     { 
      //return didEnterCorrectRoomNumber; 
     } 
    } 

    public int getRoomNumber() 
    { 
     return roomNumber; 
    } 

    public double getNightlyRate() 
    { 
     return nightlyRate; 
    } 
    public boolean getDidEnterCorrectRoomNumber() 
    { 
     return didEnterCorrectRoomNumber; 
    } 

    public void displayRoom() 
    { 
     System.out.println("Room Number: " + roomNumber); 
     System.out.format("Cost per Night: $%.2f%n", nightlyRate); 
    } 

} 

和我的子類:

public class Suite extends HotelRoom 
{ 
    private final double suiteSurchargeRate = 40.00; 
    private double nightlyRateWithSuite; 

    public Suite (int room) 
    { 
     super(room); 
     //boolean didEnterCorrectRoomNumber = super.getDidEnterCorrectRoomNumber(); 
     nightlyRateWithSuite = super.getNightlyRate() + suiteSurchargeRate; 
     //return didEnterCorrectRoomNumber; 
    } 

    public void displayRoom() 
    { 
     super.displayRoom(); 
     System.out.format("Suite Surcharge: $%.2f%n", suiteSurchargeRate); 
     System.out.format("Total Cost per Night: $%.2f%n", nightlyRateWithSuite); 
    } 

} 

精確編譯器錯誤:

 
MacBook-Air:HotelRoom Nick$ javac Suite.java 
Suite.java:12: cannot find symbol 
symbol : constructor HotelRoom(int) 
location: class HotelRoom 
    super(room); 
    ^
1 error 

我已經保存並重新編譯雙方几次,我只是得到相同結果。 HotelRoom編譯得很好,但Suite沒有。它們是目錄中唯一的兩個java文件,所以調用錯誤的類沒有任何問題。 :)

+0

你可以發佈你得到錯誤信息文本? –

+0

你發佈的代碼編譯得很好。當你沒有超級(空間)時,我懷疑你有這個錯誤。 –

+0

代碼對我來說看起來很好。您是否確定在編譯Suite之前,您沒有忘記重新編譯HotelRoom?或者你沒有其他的HotelRoom課程嗎?編譯器的確切錯誤信息是什麼? –

回答

0

我認爲它與您的Mac上的Java設置有關。運行Linux它編譯和運行我精的PC上使用Java 1.6,具有以下主要測試類

public class Main { 
    public static void main(String[] args) {   
     Suite suite = new Suite(10); 
     suite.displayRoom();  
    } 
} 

輸出:

Room Number: 0 
Cost per Night: $69.95 
Suite Surcharge: $40.00 
Total Cost per Night: $109.95