2016-04-16 76 views
-1

我很難過。我在(BedRoom)類中創建了一個名爲「makeBed」的方法,該方法調用名爲「make」的(Bed)類中的另一個方法。但是當我嘗試通過bedroom.makeBed()調用方法時;這是行不通的。我把//放在我想要工作的部分。從對象-java調用另一個方法

主要

public class Main { 
Wall wall1 = new Wall("West"); 
Wall wall2 = new Wall("East"); 
Wall wall3 = new Wall("South"); 
Wall wall4 = new Wall("North"); 

Ceiling ceiling = new Ceiling(12, 55); 

Bed bed = new Bed("Modern", 4, 3, 2, 1); 

Lamp lamp = new Lamp("classic", false, 75); 

BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp); 

bedroom.makeBed(); //bedroom.makeBed(); does not work 
//It does not show the public method "makeBed" in the BedRoom class 
} 

臥室類

public class BedRoom { 
private String name; 
private Wall wall1; 
private Wall wall2; 
private Wall wall3; 
private Wall wall4; 
private Ceiling ceiling; 
private Bed bed; 
private Lamp lamp; 

public BedRoom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, Ceiling ceiling, Bed bed, Lamp lamp) { 
    this.name = name; 
    this.wall1 = wall1; 
    this.wall2 = wall2; 
    this.wall3 = wall3; 
    this.wall4 = wall4; 
    this.ceiling = ceiling; 
    this.bed = bed; 
    this.lamp = lamp; 
} 

public Lamp getLamp(){ 
    return this.lamp; 
} 

public void makeBed(){    //This is the method I'm trying to access 
    System.out.println("Bedroom -> Making bed"); 
    bed.make(); 
} 

} 

牀類

public class Bed { 
private String style; 
private int pillows; 
private int height; 
private int sheets; 
private int quilt; 

public Bed(String style, int pillows, int height, int sheets, int quilt) { 
    this.style = style; 
    this.pillows = pillows; 
    this.height = height; 
    this.sheets = sheets; 
    this.quilt = quilt; 
} 

public void make(){ 
    System.out.println("Bed -< Making"); //Method I'm trying to call 
} 

public String getStyle() { 
    return style; 
} 

public int getPillows() { 
    return pillows; 
} 

public int getHeight() { 
    return height; 
} 

public int getSheets() { 
    return sheets; 
} 

public int getQuilt() { 
    return quilt; 
} 
} 
+0

你什麼意思,你看不出來?你的代碼看起來很好,應該編譯和運行。 –

+0

@RobertMoskal當我輸入臥室。該方法不顯示 – PrQ

+0

這是一個編輯器特定的問題,與代碼完成有關。你必須告訴我們你正在使用的編輯器以及它是如何設置的。 –

回答

2

我想說這是它...你沒有你的代碼裹成方法,所以它不編譯權。

public class Main { 
public static void main(String[] args) { 
Wall wall1 = new Wall("West"); 
Wall wall2 = new Wall("East"); 
Wall wall3 = new Wall("South"); 
Wall wall4 = new Wall("North"); 

Ceiling ceiling = new Ceiling(12, 55); 

Bed bed = new Bed("Modern", 4, 3, 2, 1); 

Lamp lamp = new Lamp("classic", false, 75); 

BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp); 

bedroom.makeBed(); //bedroom.makeBed(); does not work 
//It does not show the public method "makeBed" in the BedRoom class 
} 
} 

也許這將工作。

這是假設,這是要用來初始化從整個腳本類,如果不改變public static void main(String[] args){}public Main() {}

+0

哇...我忘了把公共靜態void main ...我一直在尋找我做錯了什麼...有時候,答案是正確的在你面前。謝謝Matt – PrQ

+0

哈哈,沒關係。我們都犯了我們的新手錯誤。獲得一些Java,繼續前進! ;)不要忘記把它作爲正確的答案! – Matthew

+0

知道了謝謝你:) – PrQ

相關問題