[問題] this與toString的誤用

看板java作者 (jim)時間14年前 (2011/04/08 00:55), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/1
問題(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

04/08 01:02, , 1F
你在toString裡呼叫this.toString ??
04/08 01:02, 1F

04/08 01:07, , 2F
請問為什麼這樣做不行呢?
04/08 01:07, 2F

04/08 01:09, , 3F
對齁,因為我複寫了它了.感謝
04/08 01:09, 3F

04/08 01:47, , 4F
其實你在 print 裡丟 this 進去也會呼叫 toString()...
04/08 01:47, 4F
文章代碼(AID): #1DdUntmb (java)