Java – why do I need to provide closed class objects instead of closed class objects

class OuterA {
class OuterA {
    class InnerA {
    }
}
class SubclassC extends OuterA.InnerA { 
    SubclassC(OuterA outerRef) {
        outerRef.super(); 
    }
}
class XYZ {
    public static void main(String[] args) {
        new SubclassC(new OuterA());
    }
}

In the code above

>Why is it necessary to The outera object reference of the subclasc constructor of the java file is passed to? > Why doesn't innera object reference need to be passed to the subclass C constructor?

Solution

Because subclasc extends the definition of innera class At the same time, innera class is bound to outera (that is, all instances of innera will be bound to the corresponding outera instance) Therefore, to get an instance of subclasc, you need an instance of outera

Because when creating a subclass C, under the hood, first create an instance of the superclass, that is, there will be an instance of innera You can actually think of subclasc as a special innera

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>