Elementary Exploration of Object Pascal class (4)

Source: Internet
Author: User

Class destructor

DestructorIt is also a special method that is automatically called before the object disappears.

DestructorIt can be seen as the opposite of the constructor, which is usually used to release the memory occupied by the class, or to clear some work. A class does not have to have a destructor, because the basic class destructor can be replaced. Like constructors, destructor do not return values.

Although a class can have multiple destructor, It is not general. If there is only one destructor, it should be namedDestroyThis is not just a habit. You can callFreeMethod,FreeThe method isTobjectClass method, before the class is deleted from the memory,FreeThe method will callDestroyMethod, which is a general method for releasing the memory occupied by classes.

For example:

VaR rect1, rect2: tmyrect; begin rect1: = tmyrect. create; {0*0 rectangle} rect2: = tmyrect. createval (0, 0,100,100); {100*100 size rectangle} {do some thing ...} rect1.free; rect2.free; end.

Next, continueTmyrectClass, added the Destructor (for the sake of conciseness, some code is omitted, please download the detailed code)

Type tmyrect = class private left: integer; top: integer; Right: integer; bottom: integer; text: pchar; {New Field} public function getwidth: integer; function getheight: integer; procedure setrect (Aleft, atop, aright, abottom: integer); {two constructors} constructor create; {This constructor is used to initialize all fields as 0} constructor createval (Aleft, atop, aright, abottom: integer); {This function is used to set a field as a special value. The parameters starting with a are all local for constructors and start with, used to differentiate Bureaus Partial Variables and fields. It is a habit to start with. } Destructor destroy; override; {override} end; {tmyrect} constructor tmyrect must be added. create; begin inherited create; {allocate memory for pchar fields} text: = allocmem (1024); end; destructor tmyrect. destroy; begin {memory used to release text} freemem (text); inherited; end;

ModifiedTmyrectClass in its constructor isPcharType stringTextBuckets are allocated and released in the destructor.

InTmyrectClass declaration, let's look at the Declaration of the destructor, as follows:

Destructor destroy; override;

Note:, There is a keyword at the endOverride, This keyword indicates that the compiler overwrites the methods in the base class. It is a little advanced here and will explain "inheritance" later ".

Note

GenerallyInheritedAs the first sentence of the constructor and the last sentence of the destructor.

The above code passes the test in Delphi7. Download the sample code:Analysis of classes. rar

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.