this指的是当前正在访问这段代码的对象,当在内部类中使用this指的就是内部类的对象,
为了访问外层类对象,就可使用外层类名.this来访问,通常也只在这种状况下使用这种
形式.例以下例
this
public class Outer { public int num = 10; class Inner { public int num = 20; public void show() { int num = 30; System.out.println(num); System.out.println(this.num);// 当在内部类中使用this指的就是内部类的对象 System.out.println(Outer.this.num);// 为了访问外层类对象,使用外层类名.this来访问 } } } class InnerClassTest { public static void main(String[] args) { Outer.Inner oi = new Outer().new Inner(); oi.show(); } }
输出:code
30对象
20class
10static