作者bil193 (丁丁)
看板java
标题[虫?] 一个简单的Thread有问题
时间Thu Jun 25 00:50:36 2009
照书上把程式码打上去,却有问题。(没有完全照打,形式大约相同)
public class bb
{
public static void main(String ss[])
{
c1 thread1=new c1();
c1.start();
c2 thread2=new c2();
c2.start();
}
}
class c1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
System.out.println("in c1 i="+i);
}
}
class c2 extends Thread
{
public void run()
{
while(true)
{
try{
System.out.println(" In c2");
sleep(2000);
}
catch(InterruptedException e){}
}
}
}
可是编译时出现错误讯息:
bb.java:6: non-static method start() cannot be referenced from a static
context
c1.start();
^
bb.java:8: non-static method start() cannot be referenced from a static
context
c2.start();
^
2 errors
可是我把c1和c2两个类别里的函数都改成public static void run()
也是会出问题耶
可是书上明明没有static,而是public void run()而已啊@@
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.99.193
1F:推 AI3767:如果你照书打, 那书写错了... 提示:要改行6和行8的c1,c2 06/25 01:13
2F:→ bil193:!! 我找到了 是我打错了 应是thread1.start();才对 06/25 01:28
3F:→ bil193:谢谢一楼.. 06/25 01:30
4F:推 adrianshum:所以就说要做好命名, type大草开首的话立刻就看出问题 06/25 12:43
5F:→ bil193:谢谢楼上的提醒 有建议的命名方法吗? 06/25 15:04