class Outer
{
private int x = 3;
class Inner//内部类
{
void function()
{
System.out.println("innner :"+Outer.this.x);//输出3,可以省略Outer.this,这就是内部类持有一个外部类的引用
}
}
void method()
{
Inner in = new Inner();
in.function();
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method();
}
}作者: 沉默de羔羊 时间: 2013-3-19 17:59
外部类名.this可以理解成:内部类引用当前外部类的对象!作者: 王龙涛 时间: 2013-3-19 18:20