继续:
/*又查阅了资料:Thread类中有个Runnable类型的属性,名字叫target,而Thread类的run()方法用到了这个属性。run()方法的代码为:
public void run(){
if(target != null){
target.run();
}
}*/
public class ThreadTest {
public static void main(String[] args){
new MyThread(new MyRunnable()).start();
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("This is the method run() in MyRunnable.");
}
}
/*class MyThread extends Thread{
MyThread(Runnable r){
super(r);
}
public void run(){
System.out.println("This is the method run() in MyThread.");
}
}*/
/*Console:
This is the method run() in MyThread.*/
//还原run()方法
class MyThread extends Thread{
private Runnable target;
MyThread(Runnable r){
super(r);
this.target = r;
}
public void run(){
if(this.target != null){
target.run();
}
}
}
/*Console:
This is the method run() in MyRunnable.*/ |