java 中覆 写tostring_如何在Java中正确覆盖toString()?
如何在Java中正確覆蓋toString()?
聽起來有點愚蠢,但我需要幫助我的toString()方法,這是非常irking。 我嘗試在網上查找,因為toString是搞砸了,“沒有找到Kid構造函數#2”,即使它在那里,我甚至會做別的,它不工作。 好的,這是我的代碼:
import java.util.*; class Kid { String name; double height; GregorianCalendar bDay; public Kid () { this.name = "HEAD"; this.height = 1; this.bDay = new GregorianCalendar(1111,1,1); } public Kid (String n, double h, String date) { // method that toString() can't find somehow StringTokenizer st = new StringTokenizer(date, "/", true); n = this.name; h = this.height; } public String toString() { return Kid(this.name, this.height, this.bDay); } } //end class
好吧所以我上面的toString(我知道,我的第三個參數是關閉的,應該是一個字符串)是關閉的。 如果我硬編碼價值的第三件事情,它不合時宜,說它無法找到(上面)。 那么我怎樣才能得到日期并分解呢?
下面是調用它的類
class Driver { public static void main (String[] args) { Kid kid1 = new Kid("Lexie", 2.6, "11/5/2009"); System.out.println(kid1.toString()); } //end main method } //end class
我試圖研究多個構造函數,它真的沒有幫助。 我試圖研究toString()方法,并嘗試使用之前創建的toString()方法邏輯,但是這是全新的,所以它從來沒有工作。
幫幫我?
toString應該返回一個String 。
public String toString() { return "Name: '" + this.name + "', Height: '" + this.height + "', Birthday: '" + this.bDay + "'"; }
我建議你使用你的IDE的特性來生成toString方法。 不要手動編碼。
例如,Eclipse可以這樣做,只要右鍵單擊源代碼并選擇Source > Generate toString
Java toString()方法
如果要將任何對象表示為一個字符串,則toString()方法就會存在。
toString()方法返回對象的字符串表示形式。
如果打印任何對象,java編譯器會在內部調用對象上的toString()方法。 所以重寫toString()方法,返回所需的輸出,它可以是一個對象的狀態等,取決于你的實現。
Java的toString()方法的優點
通過覆蓋Object類的toString()方法,我們可以返回對象的值,所以我們不需要編寫太多的代碼。
不帶toString()方法的輸出
class Student{ int id; String name; String address; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; } public static void main(String args[]){ Student s1=new Student(100,”Joe”,”success”); Student s2=new Student(50,”Jeff”,”fail”); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } Output:Student@2kaa9dc Student@4bbc148
你可以在上面的例子#1中看到。 打印s1和s2打印的對象的哈希碼值,但我想打印這些對象的值。 由于java編譯器內部調用了toString()方法,覆蓋此方法將返回指定的值。 讓我們用下面的例子來理解它:
Example#2 Output with overriding toString() method class Student{ int id; String name; String address; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; } //overriding the toString() method public String toString(){ return id+" "+name+" "+address; } public static void main(String args[]){ Student s1=new Student(100,”Joe”,”success”); Student s2=new Student(50,”Jeff”,”fail”); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } Output:100 Joe success 50 Jeff fail
請注意,toString()大多與Java中的多態的概念有關。 在Eclipse中,嘗試單擊toString()并右鍵單擊它。然后單擊“打開聲明”并查看超類toString()的來源。
你可以在toString()中創建新的對象。 使用
return "Name = " + this.name +" height= " + this.height;
代替
return Kid(this.name, this.height, this.bDay);
您可以根據需要更改返回字符串。 還有其他的方式來存儲日期而不是calander。
你不能像一個普通的方法那樣調用一個構造函數,你只能用new調用它來創建一個新的對象:
Kid newKid = new Kid(this.name, this.height, this.bDay);
但是從你的toString()方法構造一個新的對象并不是你想要做的。
以下代碼是一個示例。 基于相同的問題,而不是使用基于IDE的轉換,是否有一個更快的方式來實現,以便將來的變化發生時,我們不需要一遍又一遍地修改值?
@Override public String toString() { return "ContractDTO{" + "contractId='" + contractId + '\'' + ", contractTemplateId='" + contractTemplateId + '\'' + '}'; }
那么其實你需要返回這樣的東西,因為toString必須返回一個字符串
public String toString() { return "Name :" + this.name + "whatever :" + this.whatever + ""; }
而你實際上在構造函數中做了一些錯誤的事情,你把用戶設置的變量設置為名字,而你需要做相反的事情。 你不應該做什么
n = this.name
你應該做什么
this.name = n
希望這有助于感謝
我們甚至可以這樣寫:在類中創建一個新的String對象,然后在構造方法中指定它,然后在toString方法中返回
public class Student{ int id; String name; String address; String details; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; this.details=id+" "+name+" "+address; } //overriding the toString() method public String toString(){ return details; } public static void main(String args[]){ Student s1=new Student(100,"Joe","success"); Student s2=new Student(50,"Jeff","fail"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } }
總結
以上是生活随笔為你收集整理的java 中覆 写tostring_如何在Java中正确覆盖toString()?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TP-LINK 上架新款 AX1500
- 下一篇: java applet 游戏_Java