为什么会调用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(),也会调用吧。。。
[解决办法]
[解决办法]
[解决办法]
[解决办法]