【问题标题】:access outer class from inner nested enum从内部嵌套枚举访问外部类
【发布时间】:2012-01-17 11:39:31
【问题描述】:

有办法进入外面吗?

public class OuterClass  {
    String data;

    public void outerMethod(String data) {
         this.data = data;
    }

    public enum InnerEnum {
        OPTION1("someData"),
        OPTION2("otherData");

        InnerEnum(String data) {
              // Does not work:             
              OuterClass.this.outerMethod(data);
        }
    }
}

【问题讨论】:

  • 您需要OuterClass 的实例才能调用实例方法。

标签: java android scope closures inner-classes


【解决方案1】:

正如 Eric 所说,枚举是隐式静态的。做你想做的事,添加一个方法,callOuterMethod(OuterClass oc) 调用oc.outerMethod(data) 做你想做的事:

public enum InnerEnum {
    OPTION1("someData"),
    OPTION2("otherData");

    final String data;

    InnerEnum(String data) {
       this.data = data;             
    }

    void callOuterMethod(OuterClass oc) {
        oc.outerMethod(data);
    }
}

【讨论】:

    【解决方案2】:

    不能这样做。枚举是隐式静态的,即使您没有声明它是。请参阅类似的问题/答案:

    "嵌套枚举类型是隐式静态的。允许显式声明嵌套的枚举类型 枚举类型为静态。”

    In Java, are enum types inside a class static?

    【讨论】:

      【解决方案3】:

      我相信您将对象实例与类型混淆了。您声明的是两种嵌套类型。这与两个嵌套的对象实例不同。

      关键字this 对类型进行操作时没有意义。它仅在处理对象实例时才有意义。因此,如果您尝试从内部类型调用外部类型的实例方法,那么您需要对外部类型的实例的引用。

      但是,如果您将外部类型的方法设为静态,那么您可以从嵌套类型调用静态方法,而无需引用外部类型的实例。请记住,如果您这样做,则该方法“对所有实例都相同”——这意味着它与 OuterClass 的所有实例共享任何状态——因此它只能访问该类型的静态成员。

      在下面的示例中,outerMethod 被声明为静态的,因此可以从嵌套类型调用它,而无需引用 OuterClass 的实例。但是,通过这样做,它不能再访问私有实例成员data(当然没有对实例的引用)。您可以声明一个静态成员 staticData 并改为访问它,但请记住,该成员将由 OuterClass 的所有实例和所有 outerMethod 调用共享。

      public class OuterClass  {
      
              String data;                 // instance member - can not be accessed from static methods
                                           //   without a reference to an instance of OuterClass
      
              static String staticData;    // shared by all instances of OuterClass, and subsequently
                                           //   by all invocations of outerMethod
      
              // By making this method static you can invoke it from the nested type 
              //  without needing a reference to an instance of OuterClass. However, you can
              //  no longer use `this` inside the method now because it's a static method of
              //  the type OuterClass
          public static void outerMethod(String data) {
      
                  //this.data = data;  --- will not work anymore
      
                  // could use a static field instead (shared by all instances)
                  staticData = data;
          }
      
          public enum InnerEnum {
              OPTION1("someData"),
              OPTION2("otherData");
      
              InnerEnum(String data) {
                          // Calling the static method on the outer type
                          OuterClass.outerMethod(data);
              }
          }
      }
      

      【讨论】:

      • 实际上,该语法用于访问内部类对封闭类型实例的隐式引用,即非静态嵌套、匿名或本地类。由于嵌套枚举是一个静态嵌套类,它的实例没有这样的引用。
      猜你喜欢
      • 2014-10-30
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      相关资源
      最近更新 更多