黑马程序员技术交流社区

标题: 关于内部类的一道问题,有谁有更好的方法 [打印本页]

作者: dsh    时间: 2014-10-2 22:41
标题: 关于内部类的一道问题,有谁有更好的方法
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  );
            }
        }
}
作者: 千里马    时间: 2014-10-2 23:06
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);
                }
        }
}

你看看这样,方法差不多的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2