Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93023
  • 博文数量: 32
  • 博客积分: 2144
  • 博客等级: 大尉
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-25 09:28
文章分类

全部博文(32)

文章存档

2011年(6)

2010年(2)

2009年(10)

2008年(14)

我的朋友

分类: Java

2009-09-20 11:36:27

 

内部类的变量引用

----inner&outer

class test
{
    public static void main(String[] args)
    {        
        A a = new A();
        A.B b = a.new B();
        A.B.C c = b.new C();
        A.B.C.D d = c.new D();
        d.print();
    }
}

class A
{
    private int index = 10;
    class B
    {
        private int index = 9;
            class C
            {
                private int index = 8;
                class D
                {
                    private int index = 7;
                    void print()
                    {
                        System.out.println(this.index);
                        System.out.println(D.this.index);
                        System.out.println(C.this.index);
                        System.out.println(B.this.index);
                        System.out.println(A.this.index);                        
                        System.out.println(A.B.this.index);

//这里总是打印this左边最近的类的变量,而this就是该类的引用。
                    }
                }
            }

    }
}

阅读(658) | 评论(0) | 转发(0) |
0

上一篇:快速关机代码

下一篇:javax包中的Servlet包

给主人留下些什么吧!~~