package one;
public class SimpleThread extends Thread{
private int countDown=5;
private static int threadCount=0;
public SimpleThread(){
super(""+ ++threadCount);
start();
}
public String toString(){
return "#"+getName()+":"+countDown;
}
public void run(){
while(true){
System.out.println(this);
if(--countDown==0)return;
}
}
public static void main(String[] args) {
for(int i=0;i<5;i++)
new SimpleThread();
}}这段程序输出入下:
#1:5
#1:4
#1:3
#1:2
#1:1
#2:5
#2:4
#2:3
#2:2
#2:1
#3:5
#3:4
#3:3
#3:2
#3:1
#4:5
#4:4
#4:3
#4:2
#4:1
#5:5
#5:4
#5:3
#5:2
#5:1请问为什么程序进行到这行代码System.out.println(this);的时候,调用toString()这个方法;

解决方案 »

  1.   

    系统自动调用啊,就是这样的比如你写
    int a = 1;
    System.out.println(a);
    实际上相当于
    System.out.println(a.toString());
      

  2.   

    super(""+ ++threadCount);
    请问这句是调用Thread类的那个方法。参数之间为什么没有,隔开。
      

  3.   

    super(""+ ++threadCount);
    请问这句是调用Thread类的那个构造方法。参数之间为什么没有,隔开。
      

  4.   

    super(""+ ++threadCount);
    请问这句是调用Thread类的那个构造方法。参数之间为什么没有,隔开。
    --------------
    调用Thread(String name)构造方法
    super(""+ ++threadCount); -> super(""+ (++threadCount));
    ""+ (++threadCount)得到一个字符串,将它作为现称的名字