A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 碎流 高级黑马   /  2014-8-17 20:40  /  1122 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.it_05;
  2. //创建继承线程.

  3. class Test extends Thread   //创建线程继承Thread
  4. {
  5.         //private String name;
  6.         Test(String name)       //构造方法,一个参数
  7.         {
  8.                 //this.name = name;
  9.                 [color=Red]super(name);  [/color]  //[color=Blue]为什么要是super呢?[/color]
  10.         }
  11.         public void run()       //定义行为
  12.         {
  13.                 for(int x = 0;x < 60;x++)
  14.                 {
  15.                         System.out.println((Thread.currentThread()==this)+"..."+this.getName()+"run..."+x);
  16.                 }
  17.         }
  18.         }



  19. public class ThreadTest {
  20.         public static void main(String[] args) {
  21.                 Test t1 = new Test("11-----");  //创建一个线程
  22.                 Test t2 = new Test("22222+++");  //创建一个线程.
  23.                 t1.start();   //运行线程1
  24.                 t2.start();    //运行线程2
  25.                
  26.                 for(int x =0;x <60;x++)
  27.                 {
  28.                         System.out.println("main......"+x);  //主线程,,,
  29.                 }
  30.         }
  31. }
复制代码

5 个回复

倒序浏览
package cn.it_05;
//创建继承线程.

class Test extends Thread   //创建线程继承Thread
{
        //private String name;
        Test(String name)       //构造方法,一个参数
        {
                //this.name = name;
                super(name);    //为什么要是super呢?
        }
        public void run()       //定义行为
        {
                for(int x = 0;x < 60;x++)
                {
                        System.out.println((Thread.currentThread()==this)+"..."+this.getName()+"run..."+x);
                }
        }
        }



public class ThreadTest {
        public static void main(String[] args) {
                Test t1 = new Test("11-----");  //创建一个线程
                Test t2 = new Test("22222+++");  //创建一个线程.
                t1.start();   //运行线程1
                t2.start();    //运行线程2
               
                for(int x =0;x <60;x++)
                {
                        System.out.println("main......"+x);  //主线程,,,
                }
        }
}
回复 使用道具 举报
Thread类中有已经定义了构造方法  Thread(String str){};  这里直接引用父类构造函数!
回复 使用道具 举报
super指的是引用父类中的name,这里也就是引用Thread类里面的name.
回复 使用道具 举报
rolling-stone 发表于 2014-8-17 21:00
super指的是引用父类中的name,这里也就是引用Thread类里面的name.

嗯,谢谢

可是
Test t1 = new Test("11-----");  //创建一个线程
                Test t2 = new Test("22222+++");  //创建一个线程.
                t1.start();   //运行线程1
                t2.start();    //运行线程2

这里面创建的是子类的对象呀,而且还带了属性.按理说这个属性是子类的呀,怎么不用this而用super呢?
回复 使用道具 举报
学习了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马