标签:java.匿名内部类
java匿名内部类一般是没有变量指向的对象,只能使用一次
如下代码,为了使用抽象类People的eat方法还要去继承然后复写eat方式,非常麻烦。对于接口也是如此。
package com.lubby.nosynchronizedtest; public abstract class People { public abstract void eat() ; }<pre name="code" class="java">package com.lubby.nosynchronizedtest; public class Teacher extends People { @Override public void eat() { System.out.println("老师正在吃饭"); } public static void main(String[] args) { People people = new Teacher(); people.eat(); } }
package com.lubby.nosynchronizedtest; public class Teacher { public static void main(String[] args) { new People() { @Override public void eat() { System.out.println("我正在吃饭"); } }.eat(); } }
package com.lubby.nosynchronizedtest; public class Teacher { public static void main(String[] args) { Thread thread1 = new Thread() { public void run() { System.out.println("线程1正在跑"); } }; thread1.start(); } }
package com.lubby.nosynchronizedtest; public class Teacher { public static void main(String[] args) { Runnable runable = new Runnable() { @Override public void run() { System.out.println("run......."); } }; Thread thread = new Thread(runable); thread.start(); } }
标签:java.匿名内部类
原文地址:http://blog.csdn.net/liu00614/article/details/34882745