object类之toString方法

object是所有类的基类

如果没有使用extends关键字指明其基类,则默认基类为object类

public class Person{

  ........

}

等价于:

public class Person extends Object{

  .......

public class TestToString {
	public static void main(String[] args) {
		Dog d = new Dog();
		System.out.println("d:=" + d);
	}
}

class Dog {
	public String toString() {
		return "I'm a cool dog!";
	}
}

在代码第四行中,默认调用toString 方法

默认的定义是前面是类的名字后面是hash编码

原文地址:https://www.cnblogs.com/white-the-Alan/p/10172937.html