看板 java 關於我們 聯絡資訊
剛剛碰到個關於inner class的問題..求解~__~" 像是這樣... public class test { public class person { private String name; public person() { this("noname", 0); } public person(String name, int money) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } } public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new FileReader("test.in")); int NP = Integer.parseInt(f.readLine()); person[] pe = new person[NP]; for (int index = 0; index < NP; index++) { pe[index] = new person(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 會有錯誤:No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing instance of type test (e.g. x.new A() where x is an instance of test). } System.exit(0); // don't omit this! } } 不知道該怎麼解決= ="..把person class放在外面就沒這個問題,弄來裡面就會這樣 .. 咕狗過有看到不能這樣宣告,想請問一下這樣該怎麼解決嗎 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.39.170.147
cyberwizard:test.new person(); 03/12 18:01
LaPass:如果沒必要跟test class綁定的話,可以移到外面去 03/12 18:02
cyberwizard:說明寫得很清楚 new test & test.new person() 03/12 18:03
cyberwizard: () 03/12 18:09
cyberwizard:or public static class person{} 03/12 18:15
cyberwizard:參考 http://ppt.cc/Px7J 03/12 18:17
soso0316:謝謝^^ 我把person改成static就可以了 我之前也有 03/12 18:30
soso0316:改成test.new person() 但是不行~"~ 03/12 18:31
LaPass:test t = new test(); test.person tp = t.new person(); 03/12 19:07
soso0316:謝謝^^ 03/13 23:02