//外部类 public class Outer { private String name = "qiudajiang"; private int age = 10; //外部类方法 public void showOuter(){ //局部变量 String address = "xxx"; class Inner{ //局部内部类属性 private String phone = "123"; private String email = "qdj168@qq.com"; public void showInner(){ //访问外部类属性 System.out.println(Outer.this.name); System.out.println(Outer.this.age); //如果showOuter方法是一个静态方法,则需要实例化一个外部类对象,才能访问外部类属性 //访问内部类属性 System.out.println(this.email); System.out.println(this.phone); //访问局部变量,jdk1.7要求,变量必须是常量 final ,jdk1.8会自动添加final System.out.println(address); } } //想要调用showInner方法,需要创建一个局部类对象 Inner inner = new Inner(); inner.showInner(); } }
测试代码
public class OuterTest { public static void main(String[] args) { Outer outer = new Outer(); outer.showOuter(); } }