"Delphi master breaks through"

zhaozj2021-02-08  227

Last post of the first chapter of the new book, I was very expensive, and the response was warm and got supported by many netizens. Today, I have completed the writing of the draft of the second chapter. In this section, I will choose a small text, I hope to continue to get everyone's support, correct. Of course, the amount of the election is very small (otherwise the publishing house does not agree, huh, huh), but a spots can be seen a full Leopard, and I will have a small number of texts here after each chapter.

Mailbox nicrosoft@sunistudio.com personal home http://www.sunistudio.com/nicrosoft/ East day production room http://www.sunistudio.com

============================================================================================================================================================================================================= =========

In Object Pascal, all objects are built on the memory of the memory, instead of stack, so the constructor is not automatically called as C . Constructors and sectors are the duties of the programmer. The constructor first assigns memory to objects, this step is completed by the compiler - the so-called compile magic "in Object Pascal, which is not necessary to participate; then to initialize the data of the object Members, the compiler will be "cleared", but if there is a special assignment, you can do it in the constructor; the object needs to release the resources required in the destructed time (the memory itself occupies the memory), these work It is the duties of the descent function; the recycling of the object itself is also completed by the "Compiler Magic".

Assignment and recycling of object memory

The compiler is assigned memory for the object, the support provided is to insert these line assembly code before calling the constructor: TEST DL, DLJZ $ 08Add ESP, - $ 10Call @classcreate // Note the last code above the code One line of code calls the _classcreate function (subject to Delphi 6) of the _classcreate function (subject to Delphi 6), which is specifically assigned a suitable memory for each object. After the memory allocation is completed, it is called the constructor of the call class to initialize the data member. After that, the compiler will then insert the following quotation code: Test DL, DLJZ $ 0FCALL @afterconstructionpop DWORD PTR fs: [$ 00000000] Add ESP, $ 0c, the main job is an afterConstruction of each object instance, this call is in Delphi It is useless, and its existence is preserved for C Builder. Similarly, when destructure objects, first call the destructor of the class to release the resources of the object application. After that, the recycled object itself is in memory space. This work is done by the compiler after calling the designer function: Call @beforeDestructiontest DL, DLJLE $ 05CALL @classdestroy These code does this code Correspondingly, it is corresponding to the constructor to allocate memory, mainly to call the _classdestroy function on line 8997 of System.PAS. Constructor and destructor

Define constructor uses constructor keywords, press conventions, constructor names CREATE (of course, other names can also be used, but that is not good design!). Such as: type tmyfamily = class // Defined by your home Private FtemYfatherName: String; // Your father's name fmymothername: String; // Your mother's name ... // Other members in your family Public Constructor Create Strfathername, strmothername: string; ... // Other method END; Maybe you will ask, if I don't provide constructor for my class, can its object be established? The answer is: Yes. The reason has been said before, the distribution of the object itself is completed by the compiler. And because of Object Pascal, all classes (except for the TOBJECT class itself) are derived from the TOBJECT class, so the compiler calls the TOBJECT.CREATE () constructor, just this function is an empty function, it does not pay for the TMYFAMILY class FMYFATHERNAME, FMYMOTHERNAME initializes, they will be automatically cleared to empty strings (ie ''), because Tobject.create () does not know your father, mother! When you create an object, the constructor is called directly, the form is as follows: MyFamilyObject: = TMYFAMILY.CREATE ('zhang', 'li');

转载请注明原文地址:https://www.9cbs.com/read-2055.html

New Post(0)