标签:
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();
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4828856.html