假如我建了一个层次结构的类,第三层的类想直接访问第一层中的一个变量可以吗?这个变量在所有层中均被覆盖了
super.super好像没有这东西吧

解决方案 »

  1.   

    没有super.super,不过可以采取一些其它方法.
    比如下面的代码所示:public class Test
    {
    public static void main(String[] args)
    {

    MoreDerived  m = new MoreDerived(10);

    System.out.println(m.getBaseX());
    System.out.println(m.getX());
    }
    }class Base
    {
    public Base()
    {
    }
    public Base(int x)
    {
    this.x = x;
    }

    public int getX(){
    return x;
    }
    protected int x;
    }class Derived extends Base
    {
    public Derived()
    {
    }
    public Derived(int x)
    {
    super(x);               // 调用直接基类的构造函数
    }
    public int getBaseX(){
    return super.x;           // 返回Base中的x的值
    }
    protected int x;            // 此x覆盖了直接基类Base中的x
    }class MoreDerived extends Derived
    {
    public MoreDerived(int x){
    super(x);                 // 调用直接基类的构造函数

    public int getX(){
    return this.x;            // 返回Derived中新的x变量
    }
    public int getBaseX()
    {
    return super.getBaseX();  // 调用Derived中的getBaseX方法
    }
    }