Constructors and destructors

Source: Internet
Author: User

Constructor Features:

The construction method is a special method, which has the following characteristics.

(1) The method name of the construction method must be the same as the class name.

(2) The constructor method does not have a return type and cannot be defined as void, and the method type is not declared before the method name.

(3) The main function of the construction method is to accomplish the initialization of the object, which can pass the parameters that define the object to the domain of the object.

(4) A class can define multiple construction methods, and if a constructor is not defined when the class is defined, the compiled system automatically inserts a default constructor with no parameters, which does not execute any code.

(5) The construction method can be overloaded, with the number of parameters, type, order.

Inheritance of Java constructors//cannot inherit

(1) Subclasses inherit only the default (default) constructor of the parent class, the invisible parameter constructor. If the parent class does not have a default constructor, the subclass cannot inherit the default constructor from the parent class.

(2) The parent class inherits from the parent class the default constructor of the subclass, and cannot be the default constructor for the child class.

(3) When creating an object, first call the parent class default constructor to initialize the object, and then call the subclass itself to define the constructor.

(4) If a subclass wants to invoke a non-default constructor of the parent class, it must be implemented using super. The statement that uses super to call the parent class constructor must be the first statement of the subclass constructor

(5) The subclass must call the constructor of the parent class. The default constructor of the parent class can be called automatically by the system, and if the parent class does not have a default constructor, the subclass constructor must call the parent class's constructor through Super.

Introduction to Destructors

In the case of the C + + language: The destructor name should also be the same as the class name, just preceded by the function name with a bitwise inverse ~, such as ~stud (), to distinguish it from the constructor. It cannot take any parameters, and there is no return value (including void type). There can be only one destructor and cannot be overloaded. If the user does not write a destructor, the compilation system will automatically generate a default destructor (even if the destructor is customized, the compiler will always synthesize a destructor for us, and if the destructor is customized, the compiler will call a custom destructor and then call the composition's destructor when executing). It also does nothing. So many simple classes do not use the destructor that is displayed.

Constructors and destructors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.