1 public class TestInnerClass { 2 public static void main(String[] args) { 3 System.out.println("测试成员内部类"); 4 // 测试成员内部类 5 Outer outer = new Outer(0); 6 outer.privateInnerGetI(); 7 Outer.PublicInner publicInner = outer.new PublicInner(); 8 publicInner.printI(); 9 System.out.println("测试局部内部类"); 10 // 局部内部类 11 final int i = 0; 12 class A{ 13 public void print(){ 14 System.out.println("AAA,i = "+i); 15 } 16 } 17 A a = new A(); 18 a.print(); 19 System.out.println("测试匿名内部类"); 20 // 测试匿名内部类 21 Runnable runnable = new Runnable() { 22 @Override 23 public void run() { 24 // TODO Auto-generated method stub 25 26 } 27 }; 28 Thread[] threads = new Thread[10]; 29 for(int j = 0;j<10;j++){ 30 threads[j] = new Thread(runnable); 31 } 32 System.out.println("测试静态内部类"); 33 // 测试静态内部类 34 Outer.staticInner os = new Outer.staticInner(); 35 os.notStaticPrint(); 36 Outer.staticInner.staticPrint(); 37 38 } 39 40 41 } 42 // 成员内部类 43 class Outer{ 44 private int i; 45 public Outer(int id){ 46 this.i = i; 47 } 48 public void privateInnerGetI(){ 49 new PrivateInner().printI(); 50 } 51 52 class PublicInner{ 53 private int i = 2; 54 public void printI(){ 55 System.out.println(i); 56 } 57 } 58 class PrivateInner{ 59 public void printI(){ 60 System.out.println(i); 61 } 62 } 63 64 // 测试静态内部类 65 private static final int m = 1; 66 static class staticInner{ 67 public void notStaticPrint(){ 68 System.out.println("Outer.staticInner.notStaticPrint(),m = "+m); 69 } 70 public static void staticPrint(){ 71 System.out.println("Outer.staticInner.staticPrint()"); 72 } 73 } 74 75 }
代码参考地址:http://www.cnblogs.com/xrq730/p/4875907.html#undefined