接口可以作为变量类型
例如:Runnable
理解为实现了Runnable 接口的某个类,然后该类必定有run方法,可以进行调用
class MyThread extends Thread {
Runnable runnable;
@Override
public void run() {
runnable.run();
try {
Runnable todoRunnable = todoWorks.take();
while (todoRunnable != null) {
runnable.run();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MyThread(Runnable runnable) {
this.runnable = runnable;
}
}