分享一个java内部类的小例子:
public class A {
int arg=1;
publicstaticvoid main(String[] args) {
new A().new B().fun();
}
class B{
int arg=2;
publicvoid fun(){
int arg=3;
System.out.println(A.this.arg+"\t"+this.arg+"\t"+arg);
}
}
}
注意:内部类中持有外部类的引用,该引用为 外部类名.this 如上面例子中为A.this。
同时,如果一个内部类没有被私有化的话是可以在外部类以外被实例化的。如上例可以这样实例化
A a = new A();
A.B b = a.new B();