默默地 发表于 2022-3-27 23:35:42

请问如何访问外部类中的类

我是这样写的:
public class Father {
    String name = "zhangjun";

    class Child {
      public void introFather() {
            System.out.println(Father.this.name);
      }
    }
}

class Test {
    public static void main(String[] args) {
      Child outting = new Father().new Child();
      outting.introFather();
    }
}
但提示:
Father.java:13: 错误: 找不到符号
      Child outting = new Father().new Child();
      ^
符号:   类 Child
位置: 类 Test

isdkz 发表于 2022-3-27 23:50:37

public class Father {
    String name = "zhangjun";

    class Child {
      public void introFather() {
            System.out.println(Father.this.name);
      }
    }
}

class Test {
    public static void main(String[] args) {
      Father.Child outting = new Father().new Child();         // 这里的类型应该是 Father.Child
      outting.introFather();
    }
}
页: [1]
查看完整版本: 请问如何访问外部类中的类