听起来有点愚蠢,但我的toString()方法需要帮助,这非常令人讨厌.我尝试在网上查找,因为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
Run Code Online (Sandbox Code Playgroud)
好吧所以我上面的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
Run Code Online (Sandbox Code Playgroud)
我尝试研究多个构造函数,它确实没有帮助.我尝试了研究toString()方法,并尝试使用toString()我之前创建的以前的方法逻辑,但这是全新的,所以它从未奏效.
救命?
ada*_*shr 126
本toString应该返回String.
public String toString() {
return "Name: '" + this.name + "', Height: '" + this.height + "', Birthday: '" + this.bDay + "'";
}
Run Code Online (Sandbox Code Playgroud)
我建议您使用IDE的功能来生成toString方法.不要手工编码.
例如,如果只需右键单击源代码并选择,Eclipse就可以这样做 Source > Generate toString
Lin*_* Le 10
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
Run Code Online (Sandbox Code Playgroud)
您可以在上面的示例#1中看到.打印s1和s2打印对象的Hashcode值,但我想打印这些对象的值.由于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
Run Code Online (Sandbox Code Playgroud)
请注意,toString()主要与Java中的多态性概念有关.在Eclipse中,尝试单击toString()并右键单击它.然后,单击Open Declaration并查看Superclass toString()的来源.
正如其他人所解释的,这toString不是实例化类的地方。相反,该toString方法旨在构建一个表示类实例值的字符串,至少报告存储在该对象中的最重要的数据字段。在大多数情况下,toString用于调试和日志记录,而不是用于您的业务逻辑。
要生成表示对象值的文本以显示给用户,请添加另一个方法。人们通常将方法命名为getDisplayName. 例如,DayOfWeek::getDisplayName和Month::getDisplayName。
StringJoiner从 Java 8 及更高版本开始,最现代的实现方式是toString使用StringJoiner类。正如文档所说:
StringJoiner 用于构造由分隔符分隔的字符序列,并且可以选择以提供的前缀开头并以提供的后缀结尾。
像这样使用:
@Override
public String toString ()
{
return new StringJoiner( // In Java 8 and later, StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
" | " , // Delimiter
Person.class.getSimpleName() + "[ " , // Prefix
" ]" // Suffix
)
.add( "name=" + name ) // Append
.add( "phone=" + phone ) // Append
.toString(); // Convert entire sequence to a single `String` object.
}
Run Code Online (Sandbox Code Playgroud)
人[名字=爱丽丝| 电话=555.867.5309]
recordJava 16 引入了一种新方法来简要定义一个类,该类的主要目的是透明且不可变地通信数据:记录。
您只需列出每个成员字段的类型和名称即可定义记录。编译器隐式地创建构造函数、getter、equals&hashCode和toString。
toString的默认实现toString包括每个成员字段。
public Kid { String name , double height , LocalDate birthDate ) {}
Run Code Online (Sandbox Code Playgroud)
像任何其他对象一样实例化。
Kid alice = new Kid( "Alice" , 6.1d , LocalDate.of( 2019 , Month.April , 23 ) ) ;
String output = alice.toString() ;
Run Code Online (Sandbox Code Playgroud)
您可以选择用自己的实现覆盖默认实现。考虑到记录作为简单数据载体的目的,通常不需要覆盖。