如下面代码:
public class OuterInnerTest
{
public static void main(String[] args)
{
//1
Outer outer = new Outer();
outer.show();
//2
Outer.Inner inner = outer.getInner();
inner.show();
//3
Outer.Inner inner2 = outer.new Inner();
inner2.show();
}
}
class Outer
{
private int i = 3;
class Inner
{
private int i = 6;
public void show()
{
int i = 9;
System.out.println(i);
System.out.println(this.i);
System.out.println(Outer.this.i);
}
}
public void show()
{
Inner inner = new Inner();
inner.show();
}
public Inner getInner()
{
return new Inner();
}
}
输出为:
9
6
3
如下:
public class OuterInnerTest2
{
private int i = 3;
public static void main(String[] args)
{
OuterInnerTest2 oit = new OuterInnerTest2();
oit.show(12);
}
public void show(final int n)
{
class Inner
{
private int i = 6;
public void show()
{
int i = 9;
System.out.println(i);
System.out.println(this.i);
System.out.println(OuterInnerTest2.this.i);
System.out.println(n);//n必须为final类型的
}
}
Inner inner = new Inner();
inner.show();
}
}
输出为:
9
6
3
12