日期:2014-05-20  浏览次数:20689 次

求助!简单的继承的问题


public class TestQuestion1 {
public static void main(String[] args) {
new c().visit();
}

}
class b{
int a = 4;
public void v() {
System.out.println(this.a);
System.out.println(this.getClass());
}
}
class c extends b {
int a = 6;
public void visit() {
super.v();
}
}

为什么输出会是a 和class c呢?

------解决方案--------------------
http://bbs.csdn.net/topics/390324008
和楼主的问题类似。
------解决方案--------------------
this是本类对象的引用,因此this.a返回的是本类的a
getClass返回的是此 Object 的运行时类。所以返回的是运行的对象c的类。