class Demo
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
Student stu =new Student("lisi");
System.out.println(stu);
}
}
class Student
{
private String name;
Student(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String toString()
{
return "name="+name;
}
}
上面小程序,打印的结果是 name=lisi ,
当注释了public String toString()
{
return "name="+name;
}
打印结果Student@1fb8ee3,
问一下打印String时,是否自动调用toString()方法?
能说一下原理? |