Using Delphi objects

Source: Internet
Author: User

I. Declaration and instantiation

Declare an object with the class keyword before using an object. You can declare an object type in the Type section of a program or cell:

Type    tfooobject = class;

In addition to declaring an object type, you typically need a variable of an object, that is, an instance. instance defined in the var section

var    fooobject; tfooobject;

In Delphi, an instance of an object is constructed by invoking its constructor, which is used primarily to create an instance of an object and allocate memory for the domain in the object to initialize it so that the object is in a working state .

 Delphi objects have at least one constructor called create (), but an object can have more than one constructor. Create () can have different parameters depending on the object type .

Unlike in C + +, where the constructor cannot be called automatically in Delphi, the programmer must call the constructor itself, and the syntax for invoking the constructor is as follows

Fooobject:= tfooobject.create;

Note that the syntax for calling the constructor here is a bit special, and is to refer to the Create () method of an object through a type, rather than referencing it through an instance, as other methods do . It looks a little strange, but it's very meaningful. The variable fooobject is not defined at the time of invocation, and Tfooobject is already statically present in memory, and the Create () method that calls it statically is legal .

By calling the constructor to create an instance of the object, this is called instantiation

Note: When an object instance is created with a constructor, the compiler will initialize each field of the object by line, and you can safely assume that all the numbers are assigned a value of 0, that all pointers are assigned nil, and that all strings are empty

Second, the destruction

When the object is exhausted, the free () method of the instance should be called to release it. The free () preference checks to ensure that the object instance is not nil, and then calls the object's Destructor method Destroy (). Naturally, the destructor performs the opposite of the construction, freeing up all allocated space and performing some other action to ensure that the object is able to remove the memory appropriately. Grammar

Fooobject.free;

Do not want to call create (), this is the free () method that invokes the object instance, remember not to call destroy () directly, but to call the more secure Free () method, because the first check to ensure that the object instance is not nil, Then call the object's Destructor method Destroy ().

Warning: in C + +, a statically declared object automatically calls its destructor when it leaves its scope, but calls the destructor method manually on the dynamically generated object. This rule also applies in Delphi, and all objects created using the Create () dynamic declaration are not automatically freed even when they leave the scope of the creation, and must use the free () method for dynamic destruction , except for objects that are dynamically created in Delphi , so be sure to keep this rule in mind: whatever is created, it needs to be released. There are two important exceptions to this rule.

1) When an object is owned by another object, it releases the object for you.

2) The object referencing the technology, when the last reference is released, it will be refactored.

  

You might want to ask how these methods go into the object and don't need to write code for these methods, right? Yes, the method just discussed comes from the base class TObject object of Object Pascal. All objects in Object Pascal are descendants of the TObject object, regardless of whether they are declared as such. Therefore, the following declaration:

Type    Tfoo = class;

Equivalent to declaring it as

Type    Tfoo = Class (TObject);

  

Using Delphi objects

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.