如何调用“toString”的默认实现?
如果未定义,则 Java 打印带有一些哈希值的类名。如果已定义,如何访问此功能?toString
toString
package tests.java.lang;
public class Try_ToString {
public static class MyClass {
protected int value;
public MyClass(int value) {
this.value = value;
}
}
public static class MyClass2 extends MyClass {
public MyClass2(int value) {
super(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
public static void main(String[] args) {
MyClass a = new MyClass(12);
MyClass b = new MyClass2(12);
System.out.println("a = " + a.toString());
System.out.println("b = " + b.toString());
}
}