package com.java1234.chap03.sec06;public class Outer { private int a=1; /* * 定义内部类 * */ class Inner{ public void show(){ System.out.println(a); } } public void show(){ //实例化内部类 Inner inner=new Inner(); inner.show(); } public static void main(String[] args) { //通过调用外部类的方法创建实例化内部类 Outer outer=new Outer(); outer.show(); }}执行结果:1package com.java1234.chap03.sec06;public class Outer2 { private int a=2; /* * 定义内部类 * */ class Inner{ public void show(){ System.out.println(a); } } public static void main(String[] args) { //直接new一个外部类的内部类 Outer2 outer2=new Outer2(); Outer2.Inner inner=outer2.new Inner(); inner.show(); }}
执行结果:2