一个内部类问题?
class Outer{
public static class Inner{
int i = 10;
}
}
public class Test{
public static void main(String[] args){
…… ……
}
}
//请问各位,在Test类中,有哪几种方法能访问Inner类,将i打印出来?
------解决方案--------------------Java code
class Outer{
public static class Inner{
int i = 10;
}
}
public class Test{
public static void main(String[] args){
Outer.Inner in=new Outer.Inner();
System.out.println(in.i);
}
}
------解决方案--------------------这是我的方法,有些笨拙
public class Test {
public static void main(String[] args) {
Outer o = new Outer();
System.out.println(o.getInner_I());
}
}
class Outer {
public int getInner_I() {
Inner i = new Inner();
return i.getI();
}
public static class Inner {
int i = 10;
public int getI() {
return i;
}
}
}