黑马程序员技术交流社区

标题: 集中讨论几java基础个问题,很典型的 [打印本页]

作者: 王维波    时间: 2011-10-29 22:34
标题: 集中讨论几java基础个问题,很典型的
  class Outer
{
   int x=3;
  void method()
   {
   class Inner
    {
    void function()
      {
         System.out.println(Outer.this.x);
       }
    }
    new Inner().function();
   }

}


class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method();
  }
  }
  
  
  上面的程序可以运行,结果为 3
  但是我把上面的改一下,注意,改的地方我会提醒。
  1  class Outer
{
   int x=3;
  void method()
   {
    new Inner().function();\\我把它提上来后为什么不能运行。
   class Inner
    {
    void function()
      {
         System.out.println(Outer.this.x);
       }
    }
   
   }

}


class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method();
  }
  }
  我继续改一下,下面的为什么不能运行了?
  
    class Outer
{
   int x=3;
  void method()
   {
   class Inner
    {
   static void function()//我在此处改成了 static ,为什么不能加static。
                          //不要说在类class前加static,一样的不行。
      {
         System.out.println(Outer.this.x);
       }
    }
    new Inner().function();
   }

}


class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method();
  }
  }
  
  
  我继续改一下。
   class Outer
{
   int x=3;
  void method()
   {
   int y=4  //此处改了
   class Inner
    {
    void function()
      {
         System.out.println(y);//此处改了,为什么它没有访问成功了?为什么不不能直接调用Y?
       }
    }
    new Inner().function();
   }

}
继续改

class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method();
  }
  }
  
  
  我继续改一下
   class Outer
{
   int x=3;
  void method(final int a    )//此处a是final,为什么可以把它当变量继续使用。7如何传它。
   {
     final int y=5
   class Inner
    {
    void function()
      {
         System.out.println(Outer.this.x);
       }
    }
    new Inner().function();
   }

}


class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method(7);
  }
  }
  
   我继续改一下
   class Outer
{
   int x=3;
  void method(final int a    )
   {
     final int y=5
   class Inner
    {
    void function()
      {
         System.out.println(Outer.this.x);
       }
    }
    new Inner().function();
   }

}


class InnerClassDemo
  {
  public static void main(String[]  args)
  {   
    new Outer().method(7);//此处7已经被锁住了,为什么还是可以传8进去?
    new Outer().method(8);
  }
  }
  
  
  上面的一些问题都是很有针对性,如果大家能够好好的想明白,我想对你的java基础还是很有帮值得。
  期待你的讨论。

该贴已经同步到 保持常态0616的微博
作者: o火o把o    时间: 2011-10-31 12:38
1. new Inner().function();//把此行代码提上来之后就变成类似于, 在定义变量之前调用变量.肯定报错.
2. static void function();//static 只能用于修饰成员变量,成员方法.而class inner 之前是不能加 static 的,只能加 abstract 或者 final修饰.
3. y;// 当你在 Inner 的外面定义了一个 y 而Inner内部要调用y的时候需要在  int y=4;之前加finnal.来保证y一直存在于内存中.要不然内部类没法访问方法中的局部变量.
4. void method(final int a);//如果一个变量或方法参数被final修饰,就表示它只能被赋值一次.
5. new Outer().method(7);new Outer().method(8);//分别调用了两次方法,分别传递的7,8.他们互不影响啊.如果在method内部修改被final 修饰了的a的值,那是不允许的.
作者: 白倩    时间: 2011-10-31 20:22
"   new Inner().function();\\我把它提上来后为什么不能运行。"——当运行到这行代码时Inner类还没有读到,它还不存在所以会报错。

“void method(final int a    )//此处a是final,为什么可以把它当变量继续使用。7如何传它。”——当传值7以后运行完代码这个方法就被锁上了,当再传新的值时它就会再一次的进栈跟上次的调用没有关系。

“ static void function()//我在此处改成了 static ,为什么不能加static。
                          //不要说在类class前加static,一样的不行。”——因为Inner此时是一个局部内部类,当内部类定义在局部时不能被成员方法修饰。





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