简体   繁体   中英

Calling no argument constructor of super class

If a class does not provide any constructor,the compiler will provide a default constructor,which in turn call's the no-argument of the super class. Why is this call to the no-argument constructor of the super class needed?

初始化该类的超级(继承)部分

这样您也可以初始化超类属性。

对于对象的构造函数仅部分构造该对象有意义吗?

It's part of the "is-a" relationship of subclass instances to their superclass. If a SpottedFoo is a subclass of Foo (eg each SpottedFoo "is-a" Foo ), then the Foo constructor should be called.

Otherwise, if there are initializations (private or not) performed by the Foo constructor that are skipped, there's no way a SpottedFoo can guarantee it can act like a Foo .

That's part of the contract for implementation inheritance: each subclass instance must be able to act like any other members of their superclass. This is the Liskov substitution principle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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