In a class hierarchy, both superclasses and subclasses can have their own constructors.
The superclass constructor is responsible for constructing the part of the object that corresponds to the superclass.
The subclass constructor is responsible for constructing the part of the object that corresponds to the subclass.
In practice, object construction is done in two steps: first, the superclass is constructed and then the subclass.
If only the subclass defines a constructor, the superclass constructor is automatically called to construct the superclass part of the object, using the superclass's default constructor.
Example: Here is a version of the Triangle class that defines a constructor and makes style private, being set by the constructor.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3