看板 java 關於我們 聯絡資訊
我把程式很精簡化了 就下面這樣 卻還是不能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)
darkk6:Pet usersPet = new PetDemo().new Pet(); 05/13 03:14
darkk6:class Pet 不是類別成員,他是實體成員 05/13 03:16
locaa:謝謝你 好厲害,可是我不懂耶? 那怎樣打才是類別成員? 05/13 03:30
locaa:那我是不是誤解了範例上的什麼 還是範例印錯了 05/13 03:33
LPH66:http://tinyurl.com/26twfk8 可以看看 05/13 06:05
PsMonkey:以後請不要用這種完全無意義的標題 05/13 08:08
locaa:不好意思 因為當時一直不知道為什麼 腦羞了標題亂下= = 05/13 08:35
locaa:總之很抱歉 謝謝大家解決我這個新手的問題 05/13 08:53
conanist:一f 正解 05/13 09:39
adrianshum:一樓是誤用不是正解.這題目正確是把 Pet改static class 05/13 12:10
adrianshum:或者搬出來做 top-level class 05/13 12:11
germun:我也覺得正常是把pet搬出來比較好 05/13 14:20
tonytonyjan:compiler不是動詞呦=3= 05/17 02:40