作者chessjim (jim)
看板java
標題[問題] this與toString的誤用
時間Fri Apr 8 00:55:16 2011
問題(Question):
我覆寫了一個toString()方法
public String toString(){
// System.out.print(this);
// this.toString();
System.out.print("\n"+"V");
return "abc";
}
其中註解的地方就是程式執行錯誤的地方
我看不懂錯誤訊息
預期的正確結果(Expected Output):
能印出horseA@1fb8ee3
錯誤結果(Wrong Output):
at horse.toString(horse.java:11)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
一直重複出現
程式碼(Code):(請善用置底文網頁, 記得排版)
共三部分
1http://pastie.org/1768498
2http://pastie.org/1768502
3http://pastie.org/1768506
補充說明(Supplement):
我是新手
簡單問題請海函
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.123.130.19
推 asilzheng:你在toString裡呼叫this.toString ?? 04/08 01:02
→ chessjim:請問為什麼這樣做不行呢? 04/08 01:07
→ chessjim:對齁,因為我複寫了它了.感謝 04/08 01:09
推 LPH66:其實你在 print 裡丟 this 進去也會呼叫 toString()... 04/08 01:47