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

© dsh 中级黑马   /  2014-10-2 22:41  /  1133 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;
/*
* 7、 在打印语句中如何打印这3个x变量?

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

        public static void main(String[] args) {
                // TODO Auto-generated method stub
               
                //创建内部类对象
                A.B oi = new A().new B();
                oi.func();
        }

}
class A {
        int x = 1;
        class B {
                int x = 2;
                void func() {
                        int x = 3;
                        System.out.println(x+"---"+this.x +"---"+new A().x  );
            }
        }
}

1 个回复

倒序浏览
public class Test7 {
        public static void main(String args[]){
                A a=new A();
                A.B b=a.new B();
                b.func();

        }

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

你看看这样,方法差不多的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马