作为一个内部类,居然还要玩继承我也是醉了
它的继承会变得有些不一样,因为它是内部的,所以构造器必须连接到其外围类对象的引用。上代码
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
// No enclosing instance of type WithInner is available due to some intermediate constructor invocation
// public InheritInner() {
// }
public InheritInner(WithInner wi, int i)
{
wi.super();//不能够没有
}
public InheritInner(WithInner wi) {
wi.super();//不能没有
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner inner = new InheritInner(wi);
}
}
类WithInner有一个内部类是Inner
InheritInner继承了上面那个内部类,这个是可以的,但是InheritInner的构造函数就不是那么简单的了
从代码中可以看到如果写一个无参数构造函数的话,会提示警告
No enclosing instance of type WithInner is available due to some intermediate constructor invocation
仅仅是有参数也不行,参数还必须有一个是WithInner的引用,而且里面还必须调用super(),要不然还会提示同样的警告,导致编译通不过。