原来是tij的一个例子,这么做是为了获得宿主对象的reference来初始化内部类,但.super的语法的确比较怪: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: Feedback //: 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 Feedback enclosingClassReference.super();inside the constructor. This provides the necessary reference, and the program will then compile. Feedback
WithInner() {System.out.println("withInner");}
class Inner{
Inner() {System.out.println("Inner");}
}
}
class InheritInner extends WithInner.Inner
{
InheritInner(WithInner wi) {
wi.super();
System.out.println("InheritInner");
} //请问wi.super()怎么理解
}
public class q{
public static void main(String[] args){
WithInner wi = new WithInner();
System.out.println("XXXXXXXXXXXXX");
InheritInner ii = new InheritInner(wi);
}
}
/*****************************************************************************/
Console:withInner
XXXXXXXXXXXXX
Inner
InheritInner---------------------------------------------------------------------------
super:子类继承父类的构造方法时,子类如果使用父类的构造方法必须在子类的构造方法中用super关键字.这里的super这是调用InheritInner 父类的构造方法.只不过Inner 是WithInner的内隐类.所以用WithInner的对象来调用super
//: 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 Feedback
enclosingClassReference.super();inside the constructor. This provides the necessary reference, and the program will then compile. Feedback