简体   繁体   English

在Java中扩展内部类

[英]Extending an inner class in java

I'm having trouble trying to implement this statement I read in Oracle's Docs about Inheritance when it comes to inner classes. 当涉及到内部类时,我很难实现我在Oracle Docs中阅读的有关继承的语句。

The statement : 该声明 :

A nested class has access to all the private members of its enclosing class—both fields and methods. 嵌套类可以访问其封闭类的所有私有成员,包括字段和方法。 Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. 因此,子类继承的公共或受保护的嵌套类可以间接访问超类的所有私有成员。

In order to test this out ie to see if I can achieve the above I created a top level class OC1 which had an inner class IC1 ,then I created another top level class OC2 which extended IC1. 为了测试这一点,即查看是否可以实现上述目标,我创建了一个具有内部类IC1的顶级类OC1,然后创建了另一个扩展了IC1的顶级类OC2。

Before I could even start writing a single method , the IDE stopped me at the OC2 class body itself saying 在我什至无法开始编写单个方法之前,IDE就在OC2类正文中停止了我的发言

"No enclosing instance of type DataStructure is available due to some intermediate constructor invocation" “由于一些中间构造函数调用,因此没有可用的DataStructure类型的封闭实例”

I read some other answers and most of them point to either a) Changing the inner class to static Nested Class -- it resolves the error b) The whole scenario is unnecessary and convoluted. 我阅读了其他一些答案,其中大多数都指向以下一种情况:a)将内部类更改为静态的嵌套类-它可以解决错误b)整个情况都是不必要且复杂的。

Here is the code: 这是代码:

 public class DataStructure {
    // Create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        // fill the array with ascending integer values
        super();
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    //other methods
    //IC1
    protected  class instanceArr{

        private int a = 8;
        private static final int B = 4;
        protected instanceArr(){
        }

        protected void doSomething(){
            System.out.println("arrayOfInts[] is accessible " + arrayOfInts[6]);
        }

    }

    //main method
}

OC2 OC2

public class DataStructureChild extends DataStructure.instanceArr{


    public DataStructureChild(){

    }
}

I know that the scenario is not an ideal one but I don't want to change inner class to static nested class - it would defeat my purpose of basically trying to see whether arrayOfInts is accessible without OC1's instance in hand. 我知道该方案不是理想的方案,但我不想将内部类更改为静态嵌套类-这将破坏我的基本目的,即尝试不使用OC1实例就可以访问arrayOfInts。

Am I misinterpreting this statement ? 我是否误解了这个说法? if not then kindly point me in the correct direction. 如果没有,请指出正确的方向。

PS - this is my first question here - apologies in advance if some guidelines were flouted. PS-这是我的第一个问题-如果不遵守某些准则,请事先道歉。

Yes, this is a Trap caused by Java's synthetic sugar. 是的,这是由Java的合成糖引起的陷阱。 You think the inner-non-static-class have the default-no-arguments-constructor but that is wrong. 您认为内部非静态类具有默认无参数构造函数,但这是错误的。 Internally the constructor of IC1 have the OC1 as first argument in the constructor - even if you can not see it. 在内部,IC1的构造函数将OC1作为构造函数中的第一个参数-即使您看不到它。

Thats why the OC2 constructor must use the OC1 as constructor-argument: 这就是为什么OC2构造函数必须使用OC1作为构造函数参数的原因:

public DataStructureChild(DataStructure argument) {
}

Unfortunaltely this is not enougth, you need to get sure the argument is not-null: 不幸的是,这不是绝对的,您需要确保该参数不为空:

public DataStructureChild(DataStructure argument) {
    argument.super();
}

It looks very wierd but it works. 看起来很奇怪,但是有效。

You can do this since you inherit access to the inner class of the parent. 您可以执行此操作,因为您继承了对父级内部类的访问。

class DataStructureChild extends DataStructure {
    public DataStructureChild() {
    }

    public void foo() {
         InstanceArr ins = new InstanceArr();
         ins.doSomething();
         System.out.println(ins.a);
    }
}

But could you please give a link or explain where you read the following? 但是,您能否提供一个链接或解释您在以下地方读到的内容? A nested class has access to all the private members of its enclosing class—both fields and methods. 嵌套类可以访问其封闭类的所有私有成员,包括字段和方法。 Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. 因此,子类继承的公共或受保护的嵌套类可以间接访问超类的所有私有成员。

The first part I knew about. 我知道的第一部分。 But I never considered a separate class extending another classes inner class. 但是我从来没有考虑过一个单独的类来扩展另一个类的内部类。 Especially since there is usually an implicit relationship between classes and their enclosed inner classes. 尤其是因为类及其封闭的内部类之间通常存在隐式关系。

Edit: 编辑:

I believe you misunderstood the statement. 我相信您误解了该声明。

  1. It says that your subclass inherits the inner class. 它说您的子类继承了内部类。 That is true. 那是真实的。
  2. It also says that once done you have access to the private values of the inherited inner class. 它还说,一旦完成,您就可以访问继承的内部类的私有值。 That is also true as demonstrated above: 如上所示,这也是正确的:

So it was just talking about access the inner class via inheritance, not extending it directly. 因此,它只是在谈论通过继承访问内部类,而不是直接扩展它。

However, if you really want to do have that kind of inheritance relationship without passing references around, you can go this route. 但是,如果您确实想在不传递引用的情况下具有这种继承关系,则可以采用这种方法。


    public class Inheritance extends Outer.Inner {

       public Inheritance() {
          new Outer().super();
       }
       public static void main(String[] args) {
          new Inheritance().start();
       }
       public void start() {
          System.out.println(a);
          method();

       }

    }

    class Outer {
       public Outer() {
       }

       protected class Inner {
          protected int a = 10;

          protected Inner() {
          }
          protected void method() {
             System.out.println("This is a private message");
          }
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM