第28题 阅读下面程序
1   public class Try extends Thread{
2      public static void main(String args[ ]){
3         Try t = new Try( );
4         t.start( );
5      }
6
7      public void run( int j){
8         int i = 0;
9         while(i<5){
10           System.out.println("祝你成功!");
11           i++;
12        }
13     }
14  }
该程序要求打印5行“祝你成功!”,必须改正程序中的某行代码,程序才能完成。选择正确的修改是
A)将第1行的extends Thread改为implements Runnable
B)将第3行的new Try()改为new Thread()\TAB 
C)将第4行t.start()改为start(t)
D)将第7行的public void run(int j)改为public void run()
     正确答案:D

解决方案 »

  1.   

    有了参数。就成了重载了没有覆盖父类的run()方法。。
    public void run( int j)也就成了一个普通的方法了。
      

  2.   

    这个,我知道是继承自thread啊,请指点一下
      

  3.   

    可以带参数,但是带了参数就是另一个方法了。也就说不是run()了,而是run(参数)了
      

  4.   

    其实,我觉得线程更像一个命令令模式,线程本身不做什么,它所执行的就是run()方法。就是Runnable接口的run方法。正如Thread t = new Thread(runnable);一样,将一个任务附着在一个线程上,线程所要做的就是执行那个run()方法
      

  5.   

    java doc中这样写道: Subclasses of Thread should override this method 
    而override 就是重写,重写是方法名和参数列表不能改变
      

  6.   

    在Thread类的start方法只会调用不带参数的run方法而产生新线程,run方法没有必要带参数,完全可以将run方法所要使用的参数改成线程类的一个属性,添加相应的get、set方法,并在线程类对象的run方法被调用前set到对象中,这样run方法就可以直接使用对象的属性,而不需要传参数了。
      

  7.   


    public class Try extends Thread{ 
          public static void main(String args[ ]){ 
             Try t = new Try( ); 
             t.start( ); 
          } 
     
          public void run(){ 
             int i = 0; 
             while(i <5){ 
               System.out.println("祝你成功!"); 
               i++; 
            } 
         } 
      } 
    这样修改就好了
      

  8.   

    if you want to extends Thread,you must override the run method.
    override means you can't change the method's name and arguments list(name and type). 
      

  9.   

    我们是调用start()方法启动线程,而start()执行的任务是调用无参run方法,重载的run方法不会被调用,你可以去看看Thread类的源码。一个线程的执行的任务只能是无参run方法里的。
      

  10.   

    要想传数据可以通过构造方法,或是set方法 定义类的成员进行传参。