黑马程序员技术交流社区

标题: 内部类的问题 [打印本页]

作者: 付左军    时间: 2012-5-2 15:26
标题: 内部类的问题
  1. class Outer
  2. {
  3.         private static int x=3;
  4.         static class Inner1
  5.         {
  6.                 static void function()
  7.                 {
  8.                         System.out.println("inner1..."+x);
  9.                 }
  10.         }
  11.         class Inner2
  12.         {
  13.                 void show()
  14.                 {
  15.                         System.out.println("inner2----");
  16.                 }
  17.         }
  18.         public static void method()
  19.         {
  20.                 new Inner2().show(); //此处编译错误,我要调用非静态的方法,创建了对象。为什么不可以访问
  21.         }
  22. }

  23. class  InnerDemo2
  24. {
  25.         public static void main(String[] args)
  26.         {
  27.                 Outer.method();
  28.         }
  29. }
复制代码

作者: 何阳    时间: 2012-5-2 15:40
本帖最后由 天道酬勤 于 2012-5-2 15:42 编辑

01.class Outer

02.{

03.        private static int x=3;

04.        static class Inner1

05.        {

06.                static void function()

07.                {

08.                        System.out.println("inner1..."+x);

09.                }

10.        }

11.        class Inner2

12.        {

13.                void show()

14.                {

15.                        System.out.println("inner2----");

16.                }

17.        }

18.        public static void method()

19.        {

20.                new Inner2().show(); //此处编译错误,我要调用非静态的方法,创建了对象。为什么不可以访问
                                                     //静态只能访问静态,你创建了一个对象,就相当于有一个this,
                                                   //你认为静态中存在this吗?你在静态的方法中访问了非静态的数据,你觉的可以吗?
                                                  //因为静态随着类的加载而加载,优先于对象所以在编译时会报错。   


21.        }

22.}

23.

24.class  InnerDemo2

25.{

26.        public static void main(String[] args)

27.        {

28.                Outer.method();

29.        }

30.}


作者: 贾联国    时间: 2012-5-2 15:43
静态内部类只能够引用外部类中的静态成员方法或者静态成员变量。对于那些非静态的成员变量与成员方法,在静态内部类中是无法访问的。这就是静态内部类的最大使用限制。在普通的非静态内部类中是没有这个限制的。也正是这个原因,决定了静态内部类只应用在一些特定的场合。其应用范围远远没有像非静态的内部类那样广泛。

作者: 刘基军    时间: 2012-5-2 15:45
本帖最后由 刘基军 于 2012-5-2 15:48 编辑

public static void method()
        {
                new Outer().new Inner2().show(); //Inner2是Outer的一个非静态成员变量,要想在Outer的静态方法中访问它,肯定得先创建出Outer对象,然后才会有Inner2,继而通过Outer对象创建Inner2对象
        }
作者: 刘_浩    时间: 2012-5-2 15:54
是这样的,public static void method()是静态的,它是优于对象存在的,那么它在内存中存在了,但是它加载的时候发现里面有个对象,但是不知道是哪个类的,因这不是静态的,还没有加载进来,以报错的。


作者: 隋营营    时间: 2012-5-2 16:06
这是因为:
内部类的方法可以访问外部包装类的成员;
而外部包装类的方法中不能访问内部类的成员。
作者: 崔仁军    时间: 2012-5-2 18:21
1.  不能在静态方法中访问非静态成员变量;可以在静态方法中访问静态的成员变量。可以在非静态方法中访问静态的成员变量, 不能在静态方法中使用this 关键字。  
2 . 总结:静态的只能访问静态的;非静态的可以访问一切。
如果要 Inner2().show()方法   修改代码如下
01.class Outer

02.{

03.        private static int x=3;

04.        static class Inner1

05.        {

06.                static void function()

07.                {

08.                        System.out.println("inner1..."+x);

09.                }

10.        }

11.        class Inner2

12.        {

13.                void show()

14.                {

15.                        System.out.println("inner2----");

16.                }

17.        }



22.}

23.

24.class  InnerDemo2

25.{

26.        public static void main(String[] args)

27.        {

28.              Outer.Inner2    inner2   = new   Outer().new Inner2();
                    inner2.show();
29.        }

30.}






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