作者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
推 AI3767:如果你照書打, 那書寫錯了... 提示:要改行6和行8的c1,c2 06/25 01:13
→ bil193:!! 我找到了 是我打錯了 應是thread1.start();才對 06/25 01:28
→ bil193:謝謝一樓.. 06/25 01:30
推 adrianshum:所以就說要做好命名, type大草開首的話立刻就看出問題 06/25 12:43
→ bil193:謝謝樓上的提醒 有建議的命名方法嗎? 06/25 15:04