内部类获取外部类的成员变量,使用 外部类类名.this.变量名 的形式获取
public class Outer { public String name = "outer"; class Inner { String name = "inner"; public void show() { String name = "show"; System.out.println(name); System.out.println(this.name); System.out.println(Outer.this.name); } } public static void main(String[] args) { Inner inner = new Outer().new Inner(); inner.show(); } }
运行结果
show
inner
outer