[問題] 求助inner class

看板java作者 (carl)時間13年前 (2012/03/12 17:54), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/2 (看更多)
剛剛碰到個關於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

03/12 18:01, , 1F
test.new person();
03/12 18:01, 1F

03/12 18:02, , 2F
如果沒必要跟test class綁定的話,可以移到外面去
03/12 18:02, 2F

03/12 18:03, , 3F
說明寫得很清楚 new test & test.new person()
03/12 18:03, 3F

03/12 18:09, , 4F
()
03/12 18:09, 4F

03/12 18:15, , 5F
or public static class person{}
03/12 18:15, 5F

03/12 18:17, , 6F
03/12 18:17, 6F

03/12 18:30, , 7F
謝謝^^ 我把person改成static就可以了 我之前也有
03/12 18:30, 7F

03/12 18:31, , 8F
改成test.new person() 但是不行~"~
03/12 18:31, 8F

03/12 19:07, , 9F
test t = new test(); test.person tp = t.new person();
03/12 19:07, 9F

03/13 23:02, , 10F
謝謝^^
03/13 23:02, 10F
文章代碼(AID): #1FNSV9OW (java)
討論串 (同標題文章)
文章代碼(AID): #1FNSV9OW (java)