[蟲?] 一個簡單的Thread有問題
照書上把程式碼打上去,卻有問題。(沒有完全照打,形式大約相同)
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
推
06/25 01:13, , 1F
06/25 01:13, 1F
→
06/25 01:28, , 2F
06/25 01:28, 2F
→
06/25 01:30, , 3F
06/25 01:30, 3F
推
06/25 12:43, , 4F
06/25 12:43, 4F
→
06/25 15:04, , 5F
06/25 15:04, 5F
推
06/25 16:19, , 6F
06/25 16:19, 6F