现在的位置: 首页 > 综合 > 正文

java 内部类

2012年09月18日 ⁄ 综合 ⁄ 共 1560字 ⁄ 字号 评论关闭
/**
 * 
 * @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

}

 

抱歉!评论已关闭.