我的程序代码:
public class TestApp {
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.prt();
}
}class Test1{
String s = "Test1";
void prt(){
System.out.println(this.toString() + s);
}
}class Test2 extends Test1{
String s = "Test2";
void prt(){
System.out.println(this.toString() + s);
super.prt();
}
}
输出:
Test2@1fb8ee3Test2
Test2@1fb8ee3Test1
为什么this.toString()都是Test2呢?中间的@1fb8ee3是什么?
public class TestApp {
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.prt();
}
}class Test1{
String s = "Test1";
void prt(){
System.out.println(this.toString() + s);
}
}class Test2 extends Test1{
String s = "Test2";
void prt(){
System.out.println(this.toString() + s);
super.prt();
}
}
输出:
Test2@1fb8ee3Test2
Test2@1fb8ee3Test1
为什么this.toString()都是Test2呢?中间的@1fb8ee3是什么?
而且Test2里有两个s,一个是自己的,一个是继承自Test1的
所以Test2的prt就等于:
System.out.println(this.toString() + "Test2");
System.out.println(this.toString() + "Test1");
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
所以Test2就是类名,中间的@1fb8ee3就是@加上16进制的哈希码