1 package anonymous; 2 3 public class InnerClass { 4 public static void main(String[] args) { 5 new Test().run(); 6 } 7 } 8 9 10 class Test { 11 12 public void run() { 13 Animal dog = new Animal() { 14 @Override 15 public void Say() { 16 System.out.println("小狗汪汪"); 17 System.out.println(this.getClass()); 18 } 19 }; 20 dog.Say(); 21 } 22 } 23 24 25 interface Animal { 26 void Say(); 27 }
----------
输出结果。
小狗汪汪
class anonymous.Test$1
1 package anonymous; 2 3 public class MemberInnerClass { 4 public static void main(String[] args) { 5 Test01 test01 = new Test01(); 6 Test01.InnerClass innerClass = test01.new InnerClass(); 7 innerClass.Run(); 8 } 9 } 10 11 class Test01 { 12 13 class InnerClass { 14 public void Run() { 15 System.out.println("这是成员内部类"); 16 } 17 } 18 }