class A
{
class B
{ }
}
class C extends A.B
{
C(A a)
{
a.super();
}
public static void main(String args[])
{
A a=new A();
C c=new C(a);
}
}
上面的程序是继承内部类的一个示例,看了好多书,都是这么写的,但都没有说明原因.
虽然说如果C类想继承内部类B必有先有A类的实例对象才可以,这一点我能够理解,但是
为什么还要在C类中的构造函数再一次的调用a.super()这句话才不会提示错误呢?令人
费解,请教各位帮我分析一下.

解决方案 »

  1.   

    看到好多人问这个问题...我看到很多次了...我有thinking in java的电子版本..正好copy给你看..Inheriting from inner classesBecause the inner class constructor must attach to a reference of the
    enclosing class object, things are slightly complicated when you inherit
    from an inner class. The problem is that the “secret” reference to the
    enclosing class object must be initialized, and yet in the derived class
    there’s no longer a default object to attach to. The answer is to use a
    syntax provided to make the association explicit: //: c08:InheritInner.java
    // Inheriting an inner class.
    class WithInner {
    class Inner {}
    }
    public class InheritInner extends WithInner.Inner {
    //! InheritInner() {} // Won't compile
    InheritInner(WithInner wi) {
    wi.super();
    }
    public static void main(String[] args) {
    WithInner wi = new WithInner();
    InheritInner ii = new InheritInner(wi);
    }
    } ///:~
    You can see that InheritInner is extending only the inner class, not the
    outer one. But when it comes time to create a constructor, the default one
    is no good and you can’t just pass a reference to an enclosing object. In
    addition, you must use the syntax FeedbackenclosingClassReference.super();inside the constructor. This provides the necessary reference and the
    program will then compile.其实这段我自己都还没看到,我正好看到这章,看了下目录有讲这个的...就copy来咯.
      

  2.   

    Since it the rule, simply follow it.