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

请问大侠们一个java语言的问题. 如果访问基类的基类的版本的方法?
有这样一组类.

class A {
int i = 1;
public void f() {
System.out.println("f() in A, i = " + i);
}
}

class B extends A {
int i = 2;
public void f() {
System.out.println("f() in B, i = " + i);
}
}

class C extends B {
int i = 3;
public void f() {
System.out.println("f() in C, i = " + i);
}

public void pr() {
f(); //此处打印出"f() in C, i = 3"这个字符串
super.f(); //此处打印出"f() in B, i = 2"这个字符串
//这里我想加一句调用A类中的f函数,打印出"f() in A, i = 1"这个字符串,该如何写?
}
}

------解决方案--------------------
不能直接访问到。没有super.super.xxx

通过其他方式,比如

class A {
    public void method() { }
}

class B extends A {
    public void method() { }
    protected void superMethod() {
         super.method();
    }
}

class C extends B {
    public void method() { }

    void test() {
        method();          // C.method()
        super.method();    // B.method()
        superMethod();     // A.method()
    }
}