[蟲?] 一個簡單的Thread有問題

看板java作者 (丁丁)時間15年前 (2009/06/25 00:50), 編輯推噓3(303)
留言6則, 4人參與, 最新討論串1/1
照書上把程式碼打上去,卻有問題。(沒有完全照打,形式大約相同) 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
如果你照書打, 那書寫錯了... 提示:要改行6和行8的c1,c2
06/25 01:13, 1F

06/25 01:28, , 2F
!! 我找到了 是我打錯了 應是thread1.start();才對
06/25 01:28, 2F

06/25 01:30, , 3F
謝謝一樓..
06/25 01:30, 3F

06/25 12:43, , 4F
所以就說要做好命名, type大草開首的話立刻就看出問題
06/25 12:43, 4F

06/25 15:04, , 5F
謝謝樓上的提醒 有建議的命名方法嗎?
06/25 15:04, 5F

06/25 16:19, , 6F
文章代碼(AID): #1AGbbT9B (java)