写出下列程序的运行结果

class MyThread extends Thread {
  public void run() {
    System.out.println("MyThread: run()");
  }

  public void start() {
    System.out.println("MyThread: start()");
  }
}

class MyRunnable implements Runnable {
  public void run() {
    System.out.println("MyRunnable: run()");
  }

}

public class Main {
  public static void main(String args[]) {
    MyThread t1 = new MyThread();
    Thread t2 = new Thread(new MyRunnable());
    t1.start();
    t2.start();
  }
}

答案

MyThread: start()
MyRunnable: run()

分析

MyThread 类覆盖了父类的start() 方法,t1.start() 调用的是子类方法,不能真正启动一个线程,因此并不会执行MyThread中的run() 方法

t2.start() 是正常的启动线程的方式,会执行MyRunnable中的run() 方法


results matching ""

    No results matching ""