Java-Object类-toString()方法
Object类里的toString()这样说——
举例:
package com.cn;
public class Test {
public static void main(String args[]) {
A a = new A();
System.out.println("a:" + a);//相当于调用a.toString()方法,返回一个(“以文本方式表示”此对象的)字符串,再与字符串“a:”连接
System.out.println("toString():" + a.toString());
}
}
class A {
//默认都继承了父类Object类里面的所有
}
输出:
a:[email protected]
toString():[email protected]
A类重写父类Object里的toString():
class A {
public String toString() {
return "I am a boy!";
}
}
输出:
a:I am a boy!
toString():I am a boy!