读书人

为什么会调用toString()方法?解决思路

发布时间: 2012-03-03 15:33:02 作者: rapoo

为什么会调用toString()方法????
package org.ngweb.java.chapt04.lang;

public class ObjectDemo {
public static void main(String[] args) {
Student student = new Student();
student.name = "张明";
System.out.println("学生类是: " + student);
// 字符串连接符 + 调用的是类的toString()方法
}
}

class Student {
String name;

public String toString() {// 覆盖Object类的toString()方法
return name;
}
}

[解决办法]
When we try printing an object, it internally calls the Object’s toString() method as we have not overridden the java toString() method.
Class Name, “@”, and the hex version of the object’s hashcode concatenated into a string.

The default hashCode method in Object is typically implemented by converting the memory address of the object into an integer.
[解决办法]
it internally calls the Object’s toString() method as we have not overridden the java toString() method.
println会默认调用toString()方法,所以如果重写了toString(),也会调用吧。。。
[解决办法]

探讨

it internally calls the Object’s toString() method as we have not overridden the java toString() method.
println会默认调用toString()方法,所以如果重写了toString(),也会调用吧。。。

[解决办法]
探讨
it internally calls the Object’s toString() method as we have not overridden the java toString() method.
println会默认调用toString()方法,所以如果重写了toString(),也会调用吧。。。

[解决办法]
探讨

引用:
it internally calls the Object’s toString() method as we have not overridden the java toString() method.
println会默认调用toString()方法,所以如果重写了toString(),也会调用吧。。。

+1

[解决办法]
探讨

引用:

引用:
it internally calls the Object’s toString() method as we have not overridden the java toString() method.
println会默认调用toString()方法,所以如果重写了toString(),也会调用……

读书人网 >Eclipse开发

热点推荐