[問題] 為甚麼 class 無法 new?

看板java作者 (MJ專業帳號)時間16年前 (2010/05/13 02:55), 編輯推噓5(508)
留言13則, 8人參與, 最新討論串1/1
我把程式很精簡化了 就下面這樣 卻還是不能COMPILER 但是我把●●●這行刪掉 就可以 奇怪 這樣為什麼不對? public class PetDemo { public static void main(String[] args) { Pet usersPet = new Pet();●●●●●●●●●●●●●●● } public class Pet { private int age; } } 錯誤訊息 Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type PetDemo is accessible. Must qualify the allocation with an enclosing instance of type PetDemo (e.g. x.new A() where x is an instance of PetDemo). at PetDemo.main(PetDemo.java:5) 我是照著課本範例然後取我不能編譯的地方PO上來 都照課本還不能編譯好奇怪..... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 112.104.179.182 ※ 編輯: locaa 來自: 112.104.179.182 (05/13 02:55)

05/13 03:14, , 1F
Pet usersPet = new PetDemo().new Pet();
05/13 03:14, 1F

05/13 03:16, , 2F
class Pet 不是類別成員,他是實體成員
05/13 03:16, 2F

05/13 03:30, , 3F
謝謝你 好厲害,可是我不懂耶? 那怎樣打才是類別成員?
05/13 03:30, 3F

05/13 03:33, , 4F
那我是不是誤解了範例上的什麼 還是範例印錯了
05/13 03:33, 4F

05/13 06:05, , 5F
05/13 06:05, 5F

05/13 08:08, , 6F
以後請不要用這種完全無意義的標題
05/13 08:08, 6F

05/13 08:35, , 7F
不好意思 因為當時一直不知道為什麼 腦羞了標題亂下= =
05/13 08:35, 7F

05/13 08:53, , 8F
總之很抱歉 謝謝大家解決我這個新手的問題
05/13 08:53, 8F

05/13 09:39, , 9F
一f 正解
05/13 09:39, 9F

05/13 12:10, , 10F
一樓是誤用不是正解.這題目正確是把 Pet改static class
05/13 12:10, 10F

05/13 12:11, , 11F
或者搬出來做 top-level class
05/13 12:11, 11F

05/13 14:20, , 12F
我也覺得正常是把pet搬出來比較好
05/13 14:20, 12F

05/17 02:40, , 13F
compiler不是動詞呦=3=
05/17 02:40, 13F
文章代碼(AID): #1BwlcS5s (java)
文章代碼(AID): #1BwlcS5s (java)