// The "Tester" class.
import java.applet.*;
import java.awt.*;
public class Tester
{
public static void main (String[] args)
{
ThreadTest ex1 = new ThreadTest ();
ThreadTest2 ex2 = new ThreadTest2 ();
ex1.start ();
ex2.start ();
}
} //Tester class
public class ThreadTest extends Thread
{
public ThreadTest ()
{
}
public void run ()
{
while (true)
{
System.out.println ("Hello");
delay (1000);
}
}
public void delay (int num)
{
Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
try
{
Thread.sleep (num);
}
catch (InterruptedException ex)
{
ex.printStackTrace ();
}
Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
}
} // ThreadTest class
public class ThreadTest2 extends Thread //This is where the error is
{
public ThreadTest2 ()
{
}
public void run ()
{
while (true)
{
System.out.println ("Hello2");
delay (1000);
}
}
public void delay (int num)
{
Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
try
{
Thread.sleep (num);
}
catch (InterruptedException ex)
{
ex.printStackTrace ();
}`enter code here
Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
}
} // ThreadTest2 class
我不理解每次尝试运行代码时弹出的错误。弹出的错误是
"类型" ThreadTest2"在compliation unit" G:/Java/newstuff/Tester.java"其中还包含公共类型," Tester""
答案 0 :(得分:2)
Java规则是文件中只能有一个顶级公共类,其名称必须与文件的名称相匹配。为什么会这样,我不知道,但我确实知道这是必须遵守的规则。
有关更多详细信息,请参阅Java 1.7 JLS 7.6。
答案 1 :(得分:2)
Java中每个文件只能有一个公共类。文件名应与类名加上.java扩展名相同。
在您的情况下,您应该有2个文件:
Tester.java与Tester
class
具有ThreadTest
类
答案 2 :(得分:0)