2016-02-24 70 views
0

我只是在我的類中進行繼承,這是我遇到的第一個錯誤。除了在錯誤部分中引發標題的構造函數外,大部分代碼正在工作。隱式超級構造函數。必須明確調用另一個構造函數

public class CellDoor extends CellPassage { 

    // TODO: instance variable(s)! 
    private String imageOpen,imageClosed; 
    private boolean locked, occupied; 
    private Item item; 

    // Create a new cell in the dungeon with the specified image. 
    // The CellDoor class represents an door that can be closed (locked) or open. 
    // NOTE: an open door is allowed to hold an item (e.g. a gem or key). 
    public CellDoor(String imageOpen, String imageClosed, boolean locked) { 
     this.imageOpen = imageOpen; 
     this.imageClosed = imageClosed; 
     this.locked = locked; 
    } 

的cellPassage構造函數是:

public CellPassage(String image) { 
    this.image = image; 
} 

能有人給我一些這方面的指針?

+0

在'CellPassage'類是可用什麼構造?請包括他們。 – rgettman

回答

0

您可以在CellPassage類中有一個構造函數,它不是默認的類。這意味着Java不能通過調用默認的超級構造函數來創建您的CellDoor對象。您必須在構造函數主體的第一行添加super(...),其中...是CellPassage類中的構造函數的參數。

public CellDoor(String imageOpen, String imageClosed, boolean locked) 
{ 
    super(imageOpen); 
    this.imageOpen = imageOpen; 
    this.imageClosed = imageClosed; 
    this.locked = locked; 
} 

如果您提供從類CellPassage的代碼,我們可以很容易確定應該如何準確地寫你CellDoor構造

+0

完美謝謝。我對這個繼承很新穎,所以它讓我有點失落。 – Kevin

+0

我編輯了答案,以便這應該現在:) –

+0

所有的好。如果您需要更多的澄清,請告訴我:)如果您的答案適合您,您可以標記答案,以便其他人也可以找到它。 –

相關問題