为什么这个程序会有“需要标识符”的错误?
public class Test
{
A aa1 = new A();
aa1.start();
A aa2 = new A();
aa2.start();
}
class A extends Thread
{
private static int tickets = 50;
public void run()
{
while(true)
{
if(tickets > 0)
{
System.out.printf("%s 正在卖第%d张票。", Thread.currentThread().getName(), tickets);
tickets--;
}
else
{
break;
}
}
}
}
E:\java练习程序\practice>javac Test.java
Test.java:6: 错误: 需要<标识符>
aa1.start();
^
Test.java:9: 错误: 需要<标识符>
aa2.start();
^
2 个错误
为什么?
------解决方案--------------------public class Test
{
public static void main(String[] args) {
A aa1 = new A();
aa1.start();
A aa2 = new A();
aa2.start();
}
}
class A extends Thread
{
private static int tickets = 50;
public void run()
{
while(true)
{
if(tickets > 0)
{
System.out.printf("%s 正在卖第%d张票。", Thread.currentThread().getName(), tickets);
tickets--;
}
else
{
break;
}
}
}
}
语句必须放在方法里执行