使用其它类可以通过,线程类却找不到,是不是线程中的字段有讲研?还是extends继承了的原因?class ThreadTest
{
public static void main(String [] args)
{
Thread tt = new MyThread();
tt.className = "tt";//找不到符号
tt.setDaemon(true);
tt.start();

Thread dd = new MyThread();
dd.className = "dd";//找不到符号
dd.setDaemon(false);
dd.start();

//new MyThread().run();
}
}class MyThread extends Thread
{
String className = new String("");
public void run()
{
while(true)
{
System.out.println(className + ".run()");
}
}
}

解决方案 »

  1.   

    你把tt和dd声明为MyThread即可。public class ThreadTest {
    public static void main(String[] args) {
    MyThread tt = new MyThread();
    tt.className = "tt";//找不到符号
    tt.setDaemon(true);
    tt.start(); MyThread dd = new MyThread();
    dd.className = "dd";//找不到符号
    dd.setDaemon(false);
    dd.start(); //new MyThread().run();
    }
    }class MyThread  extends Thread{
    String className = new String(""); public void run() {
    while (true) {
    System.out.println(className + ".run()");
    }
    }
    }对于编译而言,按照你的写法,认为tt和dd是Thread类的引用,
    而Thread类没有className 这个变量。所以编译不通过。
      

  2.   

    class ThreadTest
    {
        public static void main(String [] args)
        {
            MyThread tt = new MyThread();
            tt.className = "tt
             tt.setDaemon(true);
            tt.start();
            
            MyThread dd = new MyThread();
            dd.className = "dd";//找不到符号
             dd.setDaemon(false);
            dd.start();
            
            //new MyThread().run();
        }
    }class MyThread extends Thread
    {
        String className = new String("");
        public void run()
        {
            while(true)
            {
                System.out.println(className + ".run()");
            }    
        }
    }