java 內部類

來源:互聯網
上載者:User
/** *  * @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}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.