作者soso0316 (carl)
看板java
标题[问题] 求助inner class
时间Mon Mar 12 17:54:14 2012
刚刚碰到个关於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
1F:→ cyberwizard:test.new person(); 03/12 18:01
2F:推 LaPass:如果没必要跟test class绑定的话,可以移到外面去 03/12 18:02
3F:→ cyberwizard:说明写得很清楚 new test & test.new person() 03/12 18:03
4F:→ cyberwizard: () 03/12 18:09
5F:→ cyberwizard:or public static class person{} 03/12 18:15
7F:→ soso0316:谢谢^^ 我把person改成static就可以了 我之前也有 03/12 18:30
8F:→ soso0316:改成test.new person() 但是不行~"~ 03/12 18:31
9F:→ LaPass:test t = new test(); test.person tp = t.new person(); 03/12 19:07
10F:→ soso0316:谢谢^^ 03/13 23:02