定义一个内部类时,这个内部类会有一个隐式引用(implicit reference)指向外部类的实例。例如下面的代码:
class WithInner { class Inner {} }其实,内部类
Inner
默认会有一个带参数的构造函数,我们通过反射来看一下。
import java.lang.reflect.Constructor; import java.lang.reflect.Field; class WithInner { class Inner { public void getConstructors() { for (Constructor<?> cons : getClass().getDeclaredConstructors()) { StringBuilder sb = new StringBuilder(); sb.append("constructor: ").append(cons.getName()).append("("); for (Class<?> param : cons.getParameterTypes()) { sb.append(param.getSimpleName()).append(", "); } if (sb.charAt(sb.length() - 1) == ‘ ‘) { sb.replace(sb.length() - 2, sb.length(), ")"); } else { sb.append(‘)‘); } System.out.println(sb); } } } } public class InheritInner { public static void main(String[] args) { WithInner wi = new WithInner(); WithInner.Inner i = wi.new Inner(); i.getConstructors(); } }输出结果为:
constructor: WithInner$Inner(WithInner)
InheritInner
继承WithInner.Inner
。
class WithInner { class Inner {} } public class InheritInner extends WithInner.Inner { public static void main(String[] args) { } }无法通过编译:
InheritInner.java:5: an enclosing instance that contains WithInner.Inner is required public class InheritInner extends WithInner.Inner { ^ 1 error
报错信息提示我们InheritInner
需要一个WithInner
实例。
那我们给InheritInner
加上我们反射出的带参数的构造函数。照样编译通不过,而且会报同样的错误。这是因为语法规定:在继承内部类的时候,在构造函数中必须要添加enclosingClassReference.super()
。
class WithInner { class Inner {} } public class InheritInner extends WithInner.Inner { public InheritInner(WithInner wi) { wi.super(); } public static void main(String[] args) { WithInner wi = new WithInner(); InheritInner ii = new InheritInner(wi); } }
OK,编译通过,运行也正常。但是为什么要这么做,我现在也不是很清楚。
参考资料
Written with StackEdit.
原文地址:http://blog.csdn.net/feelang/article/details/39403027