// 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 .}