有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    外部类已经是隐式静态的

    非静态嵌套类(=内部类)表示内部类隐式引用其父类

    这就是为什么对于嵌套类,可以区分静态和非静态。这对于外部类没有意义

    下面是一个理解静态/非静态嵌套类之间区别的示例。你应该理解为什么它在外部课堂上没有意义

    public class MyClass {
    
      private String anAttributeOfMyClass;
    
      private /*static*/ class MyInnerClass {
    
        public void foo() {
          /*
           * Here, I can access the attribute of the parent class
           * because I implicitly have a reference to it.
           * Try to make the nested class static an see the difference.
           */
          anAttributeOfMyClass.trim();
        }
      }
    
    }