请看这段代码,并回答问题,谢谢!
public class ShadowTest {    public static void main(String s[]){                new STChild().demo();    }}class STGrandParent {    double wealth = 50000.00;    public double getWealth() {                System.out.println("GrandParent-" + wealth);                return wealth;    }}class STParent extends STGrandParent {    double wealth = 100000.00;    public double getWealth() {                System.out.println("Parent-" + wealth);                return wealth;    }}class STChild extends STParent {    double wealth = 200000.00;    public double getWealth() {                System.out.println("Child-" + wealth);                return wealth;    }    public void demo() {                getWealth(); // Calls Child method                super.getWealth(); // Calls Parent method               super.super.getWealth(); // 1  怎样才能调用父类的父类的方法?                ((STParent)this).getWealth(); // 2 。 为什么调用的是本类的而不是父类的方法?
    ((STGrandParent)this).getWealth(); // 3。为什么调用的是本类的而不是父类的父类的方法?                System.out.println(wealth);
                System.out.println(super.wealth);
                System.out.println(((STParent)(this)).wealth); // 4。为什么调用的是父类的属性?                System.out.println(((STGrandParent)(this)).wealth); //5。为什么调用的是父类的父类的属性?    }}

解决方案 »

  1.   

     System.out.println(((STParent)(this)).wealth);   //   4。为什么调用的是父类的属性? System.out.println(((STGrandParent)(this)).wealth);   //5。为什么调用的是父类的父类的属性? 
    this只本身类对象,你前面分别对它加了父亲、爷爷的强制转换,自然是调用父亲和爷爷的。向上动态绑定而已。
      

  2.   

    要用到关键字super,但关键字必须放在构造方法的第一行,也可以通过强制转换,就是定义一个父类的父类的对象肯定就能调用到。this是代表的自身,super调用的是父类,当然调用的是父类或是父类的父类。正如上楼说的方法绑定了。
      

  3.   

    刚学的,新手上路,赶上了。  1     怎样才能调用父类的父类的方法?
    用“super.父类的方法名(参数)”调用,父类的方法应该是被子类覆盖是用此法。覆盖是指子类方法的方法头和还回类型与父类一样。2   。   为什么调用的是本类的而不是父类的方法?
    因为用了this,this是指本类,一般要调用方法时直接用就可以了,不用this.
     
     3。为什么调用的是本类的而不是父类的父类的方法?
    同上在调用父类是也一样,条件是被隐藏。还有是在构造函数中调用父类的构造函数或本类的构造函数。