1 package test; 2 3 7 public class AnonClassTest { 8 public static void main(String [] args) { 9 AnonClassTest ac = new AnonClassTest(); 10 ac.invokeInner(); 11 ac.invokeAnonymous(); 12 } 13 14 InnerClass ic; 15 InnerInterface inf; 16 17 AnonClassTest() { 18 System.out.println("0"); 19 ic = new InnerClass(); 20 System.out.println("1"); 21 inf = new InnerInterface() { 22 public void foo() { 23 System.out.println("Anonymous_class.foo()"); 24 } 25 }; 26 } 27 28 void invokeInner() { 29 ic.innerMethod(); 30 } 31 32 void invokeAnonymous() { 33 inf.foo(); 34 } 35 36 class InnerClass { 37 public InnerClass() { 38 System.out.println("InnerClass.<init>"); 39 } 40 41 void innerMethod() { 42 System.out.println("innerMethod()"); 43 } 44 } 45 46 interface InnerInterface { 47 void foo(); 48 } 49 } 50 | Popular Tags |