/** * * @ClassName: Demo * @Description: 靜態內部類中可以有靜態或非靜態成員,而非靜態內部類中只能有非靜態成員; * 靜態內部類中的方法只能訪問外部類中的靜態屬性,而非靜態內部類中的方法可以訪問外部類中的靜態或非靜態屬性; * 內部類訪問外部類的屬性格式: 外部類名.this.非靜態屬性名 外部類名.this.靜態成員名(非靜態內部類中才可以使用) * 外部類名.靜態屬性名 外部類訪問靜態內部類格式: 內部類名.靜態成員名 外部類訪問內部類格式: new * 外部類名.內部類名().內部類成員名 或new 內部類名().內部類成員名 * @author wangzhu * @date 2012-10-9 上午10:45:14 * */public class Demo { protected static int numa = 1; private int numb = 2; private static class A { int numA = 11; static int numAA = 22; static void showA() { System.out.println(numa + "" + Demo.numa + " " + numAA); } void show() { System.out .println(numa + " " + Demo.numa + "" + numA + " " + numAA); } } private class B { int numB = 111; void showB() { System.out.println(numa + " " + numb + "" + Demo.numa + "" + Demo.this.numa + " " + Demo.this.numb + " " + " " + numB); } } public void showDemo() { System.out.println(numa + " " + numb); System.out.println(Demo.A.numAA + " " + A.numAA); A.showA(); Demo.A.showA(); Demo.A demoA = new Demo.A(); Demo.B demoB = new Demo.B(); A a = new A(); B b = new B(); System.out.println(demoA.numA + " " + demoA.numAA); demoA.show(); demoA.showA(); System.out.println(demoB.numB); demoB.showB(); System.out.println(a.numA + " " + a.numAA); a.show(); a.showA(); b.showB(); System.out.println(b.numB); a = new Demo.A(); demoA = new A(); System.out.println(a.numA + " " + a.numAA); a.show(); a.showA(); System.out.println(demoA.numA + " " + demoA.numAA); demoA.show(); demoA.showA(); b = new Demo.B(); demoB = new B(); System.out.println(b.numB + " " + demoB.numB); b.showB(); demoB.showB(); } public static void main(String[] args) { new Demo().showDemo(); } // 1 2 // 22 22 // 11 22 // 11 22 // 11 22 // 1 111 22 // 11 22 // 111 // 1 211 2 111 // 11 22 // 1 111 22 // 11 22 // 1 211 2 111 // 111 // 11 22 // 1 111 22 // 11 22 // 11 22 // 1 111 22 // 11 22 // 111 111 // 1 211 2 111 // 1 211 2 111}