爲什麼當我打電話System.out.println(classroom.toStringLong())
我得到了:教室:一個大空的演講廳,零空值爲零?爲什麼我得到空?
正確的輸出應該是:教室:與去外面門大講堂到人行道
public class Main {
public static void main(String[] args) {
Space classroom = new Space();
classroom.setName("classroom");
classroom.setDescription("a large lecture hall");
Space sidewalk = new Space();
sidewalk.setName("sidewalk");
sidewalk.setDescription("a plain concrete sidewalk with weeds growing through the cracks");
Portal door = new Portal();
door.setName("door");
door.setDirection("outside");
door.setDestination(sidewalk);
classroom.setPortal(door);
System.out.println(classroom.toStringLong());
}
}
public class Space {
private String _name;
private String _description;
private Portal _portal;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDescription() {
return _description;
}
public void setDescription(String _description){
this._description=_description;
}
public Portal getPortal() {
return _portal;
}
public void setPortal(Portal _portal){
this._portal=_portal;
}
public String toString(){
return _name;
}
public String toStringLong(){
if (_portal!= null){
Portal p= new Portal();
p.toStringLong();
String Longcombined=_name + ": " + _description+" with a "+p.toStringLong();
return Longcombined;
}
else{
String Long=_name + ": " + _description;
return Long;
}
}
}
public class Portal {
private String _name;
private String _direction;
private Space _destination;
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDirection(){
return _direction;
}
public void setDirection(String _direction){
this._direction=_direction;
}
public Space getDestination(){
return _destination;
}
public void setDestination(Space _destination){
this._destination=_destination;
}
public String toString(){
String combined=_name+ " that goes "+_direction;
return combined;
}
public String toStringLong(){
Space space=new Space();
String combined=toString() + " to " + space.getDescription() ;
return combined;
}
}
這可以很容易地用最基本的調試技巧解決。 – 2014-09-29 03:47:46