A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 艾提儿 初级黑马   /  2014-8-18 23:11  /  1248 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


class A {
int x = 1;
class B {
int x = 2;
void func() {
int x = 3;
System.out.println( ? );
        }
    }
}

2 个回复

倒序浏览
  1. class  InnerTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 //创建内部类B的对象
  6.                 A.B ab = new A().new B();
  7.                 //调用内部类B的方法输出结果
  8.                 ab.func();
  9.                
  10.         }
  11. }
  12. class A {
  13.         int x = 1;
  14.         class B {
  15.                 int x = 2;
  16.                 void func() {
  17.                         int x = 3;
  18.                         System.out.println("内部类B的局部变量x="+x+"\n内部类B的成员变量x="+this.x+"\n外部类A的成员变量x="+A.this.x);
  19.         }
  20.     }
  21. }
复制代码


回复 使用道具 举报
将程序改为如下,可以打印这3个变量:
class A {
        int x = 1;

        class B {
                int x = 2;

                void func() {
        int x = 3;
        System.out.println("A的成员变量:"+A.this.x+"--B的成员变量:"+this.x+"--B的局部变量:"+x);
        }
        }
       
        public void test(){
                B b = new B();
                b.func();
        }
       
        public static void main(String[] args){
                A a = new A();
                a.test();
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马