继承内部类

 1 package test2;
 2 
 3 public class InheritInner extends WithInner.Inner {
 4 
 5     //No enclosing instance of type WithInner is available due to some intermediate constructor invocation
 6     private WithInner.Inner wi;
 7     InheritInner(WithInner wi){
 8         
 9     //调用父类构造器,必须是第一句。形式上与通常的有点不一样(一般的为 super(),这里多了个outterclass的reference  outterclass.super())。
10         wi.super();        
11         this.wi=wi.new Inner();        //初始化InheritInner对象
12     }
13     public int i=2;
14     public void prt(){System.out.println(super.i);}
15     
16     
17     public static void main(String[] args) {
18         
19         InheritInner ii=new InheritInner(new WithInner());
20         
21         System.out.println(ii.wi);        //test2.WithInner$Inner@8ee016
22         System.out.println(ii.i);        //2
23         ii.prt();                        //1
24         
25     }
26     
27     
28     
29     
30 }
31 
32 class WithInner{
33     class Inner{
34         public int i=1;
35     }
36     private Inner in;
37     WithInner(){
38         in=this.new Inner();    //外部类new内部类 ,初始化外部类对象
39     }
40 }
原文地址:https://www.cnblogs.com/01picker/p/4317510.html