黑马程序员技术交流社区

标题: 内部类规则疑惑求解? [打印本页]

作者: 谢洋    时间: 2013-2-28 23:07
标题: 内部类规则疑惑求解?
本帖最后由 谢洋 于 2013-3-1 12:46 编辑

package test1;
//内部类
public class InnerTest {

public static void main(String[] args) {
}
}
class Outer{
private String name="xxx";
private static String name2="qqqq";
//非静态内部类
class Inner1{
  String name2="xxxx";
  public void funtion(){
   Outer.this.name="xxx";
   Outer.this.name2="xxx";
  }
}
//静态内部类
static class Inner2{
  //非静态方法
  public  void funtion1(){
   Outer.this.name="xxx";//报错:No enclosing instance of the type Outer is accessible in scope
   /*报错提示信息
    Multiple markers at this line
   - The static field Outer.name2 should be accessed in a static way
   - No enclosing instance of the type Outer is accessible in scope
    */
   Outer.this.name2="xxx";//报错
  }
  //非静态方法
  public static void funtion2(){
   Outer.this.name="xxx";//报错:No enclosing instance of the type Outer is accessible in scope
   /*报错提示信息
    Multiple markers at this line
   - The static field Outer.name2 should be accessed in a static way
   - No enclosing instance of the type Outer is accessible in scope
    */
   Outer.this.name2="xxx";//报错
  }
}
}
/*
这是毕老师视频中说的:
访问规则:
a、内部类可以直接访问外部类中的成员,包括私有。
   之所以可以直接访问外部类中的成员,是因为内部类中只
   有一个外部类的引用,格式:外部类.this
b、外部类要访问内部类,必须建立内部类对象。
问题:
1、这规则仅是指非静态内部类的规则,还是指静态或非静态内部类? 为什么上面会报错?
2、看上面的测试结果好像这条规则只适合用于非静态内部类;
    那么如果这条规则不适用于静态内部类,那为什么静态内部类不能这样子做,有得解的吗?
3,静态内部类可以访问外部类的成员吗,如果能,那怎么样的方式去访问?

真心求解,顺便拿技术分,最好是用:理论+代码示例来解释

*/


作者: lzw123451    时间: 2013-3-1 00:37
本帖最后由 李志卫 于 2013-3-1 01:26 编辑
  1. public class InnerTest {

  2. public static void main(String[] args) {
  3. }
  4. }
  5. class Outer{
  6. private String name="xxx";
  7. private static String name2="qqqq";
  8. //非静态内部类
  9. class Inner1{
  10.   String name2="xxxx";
  11.   public void funtion(){
  12.    Outer.this.name="xxx";
  13.    Outer.this.name2="xxx";
  14.   }
  15. }
  16. //静态内部类
  17. static class Inner2{
  18.   //非静态方法
  19. public  void funtion1(){     
  20.   // Outer.this.name="xxx";       //毕老师说了有静态成员的内部类必须是静态内部类。
  21.   // Outer.this.name2="xxx";
  22.   }
  23.   //非静态方法
  24.   
  25.   public static void funtion2(){
  26. // Outer.this.name="xxx";   //毕老师还说了,内部类被静态修饰后,只能访问外部类的静态成员。
  27.   // Outer.this.name="xxx";
  28.    name2 = "XXX" ;              //这样才可以,也是访问外部类的静态变量,至于为什么不能用
  29.                                  // Outer.this.name2 = "xxx";这种形式,我还不知道。请高手再回答。                                   
  30.   }
  31. }

复制代码

作者: 张豪杰    时间: 2013-3-1 01:10
静态内部类的访问会受限,也就是说只能访问外部类的静态成员,而不能访问外部类的非静态成员
至于原因,我觉得这个跟静态方法只能调用静态成员是一个道理
可以把静态内部类作为外部类Outer的一个静态成员,在执行类加载过程中,静态内部类在加载Outer后会进行初始化,而非静态成员都还没在内存里产生,你又怎么去访问呢?
作者: 谢洋    时间: 2013-3-1 01:12
谢谢!
//毕老师还说了有静态成员的内部类必须是静态内部类,静态内部类只能有静态成员
你这句话有问题,我测试结果是:静态内部类中可以有非静函数,
我只记得毕老都说过这句话的前半部分,后半部分好像没说过
  

作者: 谢洋    时间: 2013-3-1 01:16
张豪杰 发表于 2013-3-1 01:10
静态内部类的访问会受限,也就是说只能访问外部类的静态成员,而不能访问外部类的非静态成员
至于原因,我 ...

有道理,不错!!
作者: lzw123451    时间: 2013-3-1 01:20
谢洋 发表于 2013-3-1 01:12
谢谢!
//毕老师还说了有静态成员的内部类必须是静态内部类,静态内部类只能有静态成员。
你这句话有问题, ...

嗯 看来是我记错了,看来是 静态内部类不能用Outer.this.静态成员的问题了。
作者: lzw123451    时间: 2013-3-1 01:47
林明华 发表于 2013-3-1 01:37
//静态方法
           public static void funtion2(){
                        Outer.this.name="xxx ...

嗯嗯,有道理。 看来是外部类名.静态成员, 外部类名.this对象).非静态成员。
作者: 谢洋    时间: 2013-3-1 09:18
谢谢上面几位可哥们了,我把你们的结论总结了一下;
a、当内部类中定义了静态成员,该内部类必须是静态的static
b、当外部类中的静态方法访问内部类时,内部类也必须是static 的
c、静态内部类中不能出现this,因为他先于对象。
d、当静态内部类定义在外部类的成员位置上,可以把它看待成外部类
的一个的个静态方法来处理

作者: 陈迎春    时间: 2013-3-1 10:09
新手,学习了,谢谢




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