在Class A 里声明一个Class B, 默认情况下为包继承

解决方案 »

  1.   

    但是为什么需要new A().super();这句呢?
      

  2.   

    <think in java>第三版(第八章),第一二版本(第七章)里面有提到过.
    摘录如下:
    Inheriting from inner classes
    Because 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: 
    -----这里是代码演示,和楼主的代码差不多--------
    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:A().super();inside the constructor. This provides the necessary reference and the program will then compile. 大概意思就是:内部类构造方法必须同封装类对象的一个句柄联系到一起,所以要建立这种联系,就要采用这种指定的语法,类似于:A().super();这样提供了必要的句柄,可以访问了,编译才能通过.就是这样.总之一句话:如果出现了这样的情况,就使用A().super()这种形式,背下就行了,实际中,用到这种复杂结构的机会还是很小di.......