从内部类访问扩展类的方法 [英] Accessing methods of extended class from inner class

查看:96
本文介绍了从内部类访问扩展类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ABC extends XYZ
{
          public static class InnerClass1
          {
            ...
          }
          public static class InnerClass2
          {
            ...
          }
          public static class InnerClass3
          {
            ...
          }
          public static class InnerClass4
          {
            ...
          }
}

在上面的代码中,我无法访问内部类1,2,3和4中的XYZ类的方法.如何修改上述结构,以便内部类可以访问XYZ类中的方法? /p>

提前谢谢!

解决方案

public class ABC extends XYZ
{
      public static class InnerClass1
      {
        ...
      }

InnerClass1不是内部类.这是一个嵌套类,因为有static一词.

如果没有static,则它将是一个内部类.并且该内部类的任何实例都具有对ABC(也是XYZ)的隐藏引用.如果内部类调用ABCXYZ的任何实例方法,或引用这些类中的任何实例变量,则它将使用该隐藏引用来调用实例方法或访问实例变量.

但是,由于它是一个嵌套类,因此没有对ABC(或XYZ)的隐藏引用.因此,如果调用实例方法或引用实例变量,则将无法执行该操作,因为没有可使用的ABC对象. (但是,您仍然可以调用ABC static 方法,或引用静态变量.)

我不确定解决方案是什么-它取决于您的需求.您可能无法调用的XYZ方法实际上不需要使用XYZ对象,因此这些方法应该是静态的.嵌套类还可能应具有一些用于访问实例方法的显式ABCXYZ变量.如果您有要处理的对象,仍然可以从嵌套类中调用实例方法:

public static class NestedClass {
    XYZ x;
    void someMethod() { 
       x.instanceMethod();  // legal even if instanceMethod is non-static
    }
}

另一种解决方案是删除单词static,以便InnerClass1实际上具有对ABC的隐藏引用.这意味着在创建InnerClass1实例时,需要一些ABC对象供其引用.如果在其他类中创建此语法,则语法将类似于

ABC abcObject;
...
ABC.InnerClass1 newObject = abcObject.new InnerClass1(); 

public class ABC extends XYZ
{
          public static class InnerClass1
          {
            ...
          }
          public static class InnerClass2
          {
            ...
          }
          public static class InnerClass3
          {
            ...
          }
          public static class InnerClass4
          {
            ...
          }
}

In the above code, I cannot access the methods of the class XYZ inside the inner classes1,2,3 and 4. How can i modify the above structure so that the inner classes can access the methods within the class XYZ ?

Thanks in advance !

解决方案

public class ABC extends XYZ
{
      public static class InnerClass1
      {
        ...
      }

InnerClass1 is not an inner class. It's a nested class, because of the word static.

If there were no static, it would be an inner class. And any instance of that inner class would have a hidden reference to an ABC (which is also an XYZ). If the inner class called any instance methods of ABC or XYZ, or referred to any instance variables in those classes, it would use that hidden reference to call the instance methods or access the instance variables.

Since it's a nested class, though, there is no hidden reference to an ABC (or XYZ). Thus, if you call an instance method or refer to an instance variable, it can't do it, because there is no ABC object to work with. (However, you can still call a static method of an ABC, or refer to a static variable.)

I'm not sure what the solution is--it depends on your needs. It's possible that the XYZ methods you can't call don't actually need an XYZ object to work on, and therefore those methods should be static. It's also possible that the nested class should have some explicit ABC or XYZ variable that it uses to access the instance methods; you can still call instance methods from a nested class if you have an object to work on:

public static class NestedClass {
    XYZ x;
    void someMethod() { 
       x.instanceMethod();  // legal even if instanceMethod is non-static
    }
}

The other solution would be to remove the word static, so that InnerClass1 really has a hidden reference to an ABC. This means that when you create an InnerClass1 instance, you need some ABC object for it to refer to. If you create this in some other class, the syntax would be something like

ABC abcObject;
...
ABC.InnerClass1 newObject = abcObject.new InnerClass1(); 

这篇关于从内部类访问扩展类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆