- package qbb;
- class PrimeThread extends Thread{
- PrimeThread(String name){
- super(name);
- }
- public void run(){
- for(int x =0; x < 3; x++){
- System.out.println(this.getName() + " run---" + x);
- }
- }
- }
- class ThreadDemo extends Thread{
- ThreadDemo(String name){
- super(name);
- }
- public void run(){
- for(int x =0; x < 3; x++){
- System.out.println(this.getName() + " run---" + x);
- }
- }
-
- }
- public class ThreadTest {
- public static void main(String[] args) {
- PrimeThread t1 = new PrimeThread("PrimeThread one");
- PrimeThread t2 = new PrimeThread("PrimeThread two");
- ThreadDemo d1 = new ThreadDemo("ThreadDemo one");
- t1.start();
- t2.start();
- d1.start();
- for(int x =0; x < 3; x++){
- System.out.println("main---" + x);
- }
- }
- }
复制代码 运行结果:
main---0
main---1
main---2
PrimeThread two run---0
PrimeThread two run---1
PrimeThread two run---2
PrimeThread one run---0
PrimeThread one run---1
PrimeThread one run---2
ThreadDemo one run---0
ThreadDemo one run---1
ThreadDemo one run---2
下面的这段代码有点不明白
PrimeThread(String name){
super(name);
}
构造器是用来初始化对象属性的
那么super(name);是给Thread类的对象命名
运行结果显示该对象把名称传给了PrimeThread类的对象
可构造体没有传递name的代码啊,why?
|
|