2014-09-29 232 views
-3

爲什麼當我打電話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; 
    } 

} 
+2

這可以很容易地用最基本的調試技巧解決。 – 2014-09-29 03:47:46

回答

3

您正在創建一個新的對象的空間並打印其描述爲空 將您的toStringLong()方法重寫爲

In class Space 

public String toStringLong(){ 
if (_portal!= null) 
{ 
// comment this Portal p= new Portal(); 
_portal.toStringLong(); 
String Longcombined=_name + ": " + _description+" with a "+_portal.toStringLong(); 
return Longcombined; 
} 
else 
{ 
String Long=_name + ": " + _description; 
return Long; 
} 
} 


Class Portal -> 

public String toStringLong() 
{ 
String combined=toString() + " to " + _destination.getDescription() ; 
return combined; 
} 

希望這能解決您的問題。

+1

在代碼中有toStringLong()嗎? – 2014-09-29 03:56:06

+0

我的+1在哪裏?大聲笑 – 2014-09-29 04:07:42

+0

謝謝!這解決了它! – 2014-09-29 20:58:41

0

當運行該代碼「空間空間=新的空間();」在toStringLogn()方法的內部,會創建新的空間對象,並且此對象的所有即時變量都將使用其默認值進行初始化。該字符串的默認值爲「null」。這就是爲什麼當你使用這個對象時你會得到空值。

這是更好地重新定義toStringLong()如下,

字符串組合=的toString()+ 「至」 + _destination.getDescription();
返回組合;