简体   繁体   English

Inner Class是继承到OuterClass的subClass吗?

[英]Is Inner Class inherited to subClass of OuterClass?

I tried below code and according to it I have the understanding that inner class is inherited to OuterClass's subclass.Is it correct? 我尝试下面的代码,据我所知,内部类继承到OuterClass的子类。它是否正确?

    class Outter{
    int i=1;
    class Inner{
        int i=2;
        public void hello(){
            System.out.println("hello from outter.inner");
        }
    }
}

    class ChildClass extends Outter{
        class ChildInner{

        }
    }
    public class Classes {
        public static void main(String[] args) {
            Inner inner = (new ChildClass()).new Inner();
            System.out.println(inner.i);
            inner.hello();
        }
    }

Output as excepted: 输出为例外:

2 2

hello from outter.inner 你好。来自outter.inner

Inner inner = (new ChildClass()).new Inner();

As this line of code worked it should mean that Inner class is inherited to ChildClass 由于这行代码工作,它应该意味着Inner类继承到ChildClass

I am getting confused because of the below statement I found on Link 由于我在Link上找到的以下声明,我感到困惑

When an outer class is extended by it's sub class, Member inner classes will not be inherited to sub class. 当外部类通过它的子类扩展时,成员内部类将不会继承到子类。 To use inner class properties inside the sub class of outer class, sub class must also have an inner class and that inner class must extend inner class of the outer class. 要在外部类的子类中使用内部类属性,子类还必须具有内部类,并且该内部类必须扩展外部类的内部类。

So I will illustrate that statement with an example: 所以我将用一个例子说明该陈述:

When an outer class is extended by it's sub class, Member inner classes will not be inherited to sub class. 当外部类通过它的子类扩展时,成员内部类将不会继承到子类。 To use inner class properties inside the sub class of outer class, sub class must also have an inner class and that inner class must extend inner class of the outer class. 要在外部类的子类中使用内部类属性,子类还必须具有内部类,并且该内部类必须扩展外部类的内部类。

class Outter {      
    void method(){
       Inner test=new Inner();
       test.i=5; //No problem to do that even if i is private because it is inner class
    } 
    class Inner {
        private int i = 2; 
    }
}

class ChildClass extends Outter{
     void method2(){
        Inner test=new Inner();
        test.i=5;  //Does not compile
        }
}

You cannot access "i" in the child class. 您无法访问子类中的“i”。 And if you also extend the inner there you can: 如果你也扩展内部,你可以:

class ChildClass extends Outter{
 void method2(){
    Inner2 test=new Inner2();
    test.i=5;  //Compiles fine because we have also extended the inner class (like written in the quoted text)
    }

 class Inner2 extends Inner{     }
}

The statement: 该声明:

Inner inner = (new ChildClass()).new Inner();

Is not really inherited to ChildClass 是不是真的继承了ChildClass

If you break it you are basically doing this: 如果你打破它,你基本上是这样做的:

ChildClass child = new ChildClass();
Inner inner = child.new Inner();

Now you can call new Inner() on the ChildClass because it extends Outter 现在你可以在ChildClass上调用new Inner() ,因为它扩展了Outter

This doesn't mean that ChildClass can call any of the methods/properties inside Inner just because Inner is part of Outter 这并不意味着ChildClass可以调用Inner内部的任何方法/属性,因为InnerOutter一部分

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

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