2014-09-26 101 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