标签:oid 应该 接口 color size blog bst runnable system
继承抽象类的匿名内部类
1 abstract class Person { 2 public abstract void eat(); 3 } 4 public class Demo { 5 public static void main(String[] args) { 6 Person p = new Person() { 7 public void eat() { 8 System.out.println("eat something"); 9 } 10 }; 11 p.eat(); 12 } 13 }
------------------------------------------------------------------------------------------------------------------------------------------------------------
实现接口的匿名内部类
1 interface Person { 2 public void eat(); 3 } 4 public class Demo { 5 public static void main(String[] args) { 6 Person p = new Person() { 7 public void eat() { 8 System.out.println("eat something"); 9 } 10 }; 11 p.eat(); 12 } 13 }
Thread类的匿名内部类实现
1 public class ThreadDemo { 2 public static void main(String[] args) { 3 Thread thread = new Thread() { 4 public void run() { 5 for (int i = 1; i <= 5; i++) { 6 System.out.print(i + " "); 7 } 8 } 9 }; 10 thread.start(); 11 } 12 }
等价的非匿名内部类实现
1 class MyThread extends Thread { 2 public void run() { 3 for (int i = 1; i <= 5; i++) { 4 System.out.print(i + " "); 5 } 6 } 7 } 8 public class ThreadDemo { 9 public static void main(String[] args) { 10 MyThread thread = new MyThread(); 11 thread.start(); 12 } 13 }
Runnable接口的匿名内部类实现
1 public class RunnableDemo { 2 public static void main(String[] args) { 3 Runnable r = new Runnable() { 4 public void run() { 5 for (int i = 1; i <= 5; i++) { 6 System.out.print(i + " "); 7 } 8 } 9 }; 10 Thread thread = new Thread(r); 11 thread.start(); 12 } 13 }
等价的非匿名内部类实现
1 class MyThread implements Runnable { 2 public void run() { 3 for (int i = 1; i <= 5; i++) { 4 System.out.print(i + " "); 5 } 6 } 7 } 8 public class RunnableDemo { 9 public static void main(String[] args) { 10 MyThread r = new MyThread(); 11 Thread thread = new Thread(r); 12 thread.start(); 13 } 14 }
标签:oid 应该 接口 color size blog bst runnable system
原文地址:http://www.cnblogs.com/-maji/p/7281499.html