Understanding Delphi classes (11)-in-depth methods in classes [10]-constructor and constructor

Source: Internet
Author: User
// The constructor is the method called when the object is created. The constructor is the method called when the object is destroyed. For example:
 Tmyclass = Class (tobject) Public constructor create; {constructor destroy; override; {destructor} end;
 {Key points: both methods can be traced back to tobject, the ancestor class of all classes; they all belong to class methods, although there is no Class Identifier; but they are also different from class methods: normal class methods cannot use non-static data in the class, but they can. it must be defined by constructor and destructor, but the names may not be: create and destroy. If you do not use the create and destroy names, it will cause a lot of trouble and there is no need to try. our own code in create is executed after the create code of the parent class; before the execution in destroy; therefore, the statement format is generally used :}
 Constructor tmyclass. Create; begin inherited; //... end; destructor tmyclass. Destroy; begin //... inherited; end;
 {Create is the method we use the most, but destroy is the method we use the least, because calling destroy through free is safer. destroy is a virtual method. Although create is a static method in the ancestor class, it also becomes a virtual method in many practical classes. Therefore, it should be treated differently when overwriting or reloading .}
 // Create should be a method specially treated by the compiler, for example, tmyclass = Class (tobject) Public constructor create; end; {In principle, this will hide or replace the create statement of the parent class; but it does not actually exist. The compiler must do a background work !}
 // Another example is tmyclass = Class (tobject) Public constructor create (X, Y: integer); end; var form1: tform1; implementation {$ R *. DFM} {tmyclass} constructor tmyclass. create (X, Y: integer); begin // inherited create ;//... end;
 {The compiler has allowed the overload without overload, and has allowed to remove this sentence: inherited create; if there is no tobject. Create method, how can the class initialize and allocate memory? So this is a superficial phenomenon. I think the compiler will automatically overload and automatically inherited create. Other methods won't have these special treatments. It seems that there are no backdoors .}
 

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.