如何在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() } }