当前位置: 代码迷 >> Eclipse >> 为什么会调用toString()方法?解决思路
  详细解决方案

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

热度:43   发布时间:2016-04-23 14:11:24.0
为什么会调用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(),也会调用……
  相关解决方案