2014-09-26 104 views
0

對於入門Java,我創建了一個Door類和一個DoorTester類。基本上,我們正在試驗實例變量並創建公共方法。我所做的大門類,如下所示,但我DoorTester返回 「空」 時,它看起來.getState方法返回null [簡單]

Door.java

public class Door { 
// Create instance variables of type String 
private String name; 
private String state; 

// Declare method 'open' and 'close' 
public void open() { 
    state = "open"; 
} 
public void close() { 
    state = "closed"; 
} 

// Add a constructor for the Door class 
public Door(String name, String state) { 
} 

// Create an accessor of 'state' 
public String getState() { 
    return name; 
} 

// Set the state 
public void setState(String newState) { 
    state = newState; 
} 

}

DoorTester.java

public class DoorTester { 
public static void main(String[] args) { 
    Door frontDoor = new Door("Front", "open"); 
    System.out.println("The front door is " + frontDoor.getState()); 
    System.out.println("Expected: open"); 
    Door backDoor = new Door("Back", "closed"); 
    System.out.println("Expected: closed");   
    // Use the mutator to change the state variable 
    backDoor.setState("open"); 
    System.out.println("The back door is " + backDoor.getState()); 
    System.out.println("Expected: open"); 
    // Add code to test the setName mutator here 
    } 

}

+0

你沒有在你的構造函數中設置私有變量..... – Sebas 2014-09-26 04:44:03

+0

你從來沒有在你的構造函數中分配你的變量。 – theGreenCabbage 2014-09-26 05:33:56

回答

0

您的getState()方法不返回state,它返回name

// Create an accessor of 'state' 
public String getState() { 
    return name; // <-- Simply change this 
} 

此外,您的構造函數不設置字段。你需要做這樣的事情:

// Add a constructor for the Door class 
public Door(String name, String state) { 
    this.name = name; 
    this.state = state; 
} 
+0

我改變它爲狀態,null仍然返回。 – mshades 2014-09-26 04:48:16

+0

將名稱更改爲狀態????它不會刪除錯誤 – nobalG 2014-09-26 04:50:30

+0

@mshades:這只是兩個問題之一。你需要做我提到的兩個更正。 – Keppil 2014-09-26 04:50:56

1

你將不得不修改Door類的構造函數一樣

public Door(String name, String state) { 
this.name=name; 
this.state=state; 
} 

其實namestate沒有得到初始化。 也看到這個What is the meaning of "this" in Java?


修改代碼段:

public class Door { 
// Create instance variables of type String 
private String name; 
private String state; 

// Declare method 'open' and 'close' 
public void open() { 
    state = "open"; 
} 
public void close() { 
    state = "closed"; 
} 

// Add a constructor for the Door class 
public Door(String name, String state) { 
this.name=name; 
this.state=state; 
} 

// Create an accessor of 'state' 
public String getState() { 
    return state;   //<<<<<<<----------also make an Edit here 
} 

// Set the state 
public void setState(String newState) { 
    state = newState; 
} 
} 
+0

'這'是什麼意思? - Java新手 - – mshades 2014-09-26 04:48:42

+0

這意味着您想要引用封閉類的上下文 – nobalG 2014-09-26 04:49:16

+0

http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java – nobalG 2014-09-26 04:49:32

0

,我們在您Door

重要性
public class Door { 
private String name; 
private String state; 

public void open() { 
    state = "open"; 
} 

public void close() { 
    state = "closed"; 
} 

public Door(String name, String state) { // argument passed here need to set 
// set like 
    this.name=name; 
    this.state=state; 
} 

public String getState() { 
    return name; 
} 

public void setState(String state) { 
    state = state; // you need to use this.state=state 
} 
} 

問題關鍵字Java

+0

否由於參數名爲'newState',因此需要使用'this.state'。 – Keppil 2014-09-26 04:56:03

+0

有沒有辦法可以做到這一點沒有「這個」? @Keppil – mshades 2014-09-26 04:57:06

+0

@mshades:當然,只需使用不同的域名和參數名稱,例如'state'和'newState'。 – Keppil 2014-09-26 05:55:15