VCL dynamically creates an object-creates an object based on the class name

Source: Internet
Author: User

 

VCL Dynamic Object Creation

During the development process, we sometimes need to create an object for a class or its sub-classes. During the design, we do not know which concrete sub-class instance to create, this requires the dynamic creation method.

Delphi can create a pair based on the class name. We say this method is a dynamic creation pair, which benefits Delphi's very powerful rtti support, the types of the objects we can get and the information of the class itself are very rich. In addition to frequently used is and as operations, Delphi also provides functions that directly operate the class itself, such as registerclass, unregisterclass, getclass, and findclass. These function operation objects are subclasses of the persistence class tpersistent, that is, the variable of the tpersistentclass class. Here, the variable of the tpersistentclass class represents a class, which is actually a class pointer, because the class in Delphi itself is a pointer, which saves the VMT address of the class in the memory.

Let's look at the following code:

Procedure tform1.btn1click (Sender:
Tobject );

VaR Aclass, bclass: tpersistentclass;

Begin

 
Aclass: = tform1;

 
Bclass: = tpersistentclass (classtype );

 
Mmo1.clear;

 
Mmo1.lines. Add (inttohex (longword (Aclass), 8 ));

 
Mmo1.lines. Add (inttohex (longword (bclass), 8 ))

End;

Here we assign the tform1 class to the Aclass variable of the tpersistentclass type, that is, the value of Aclass is tform1, the value of bclass is reference of the class of the object form1, and the description of clasatype in the Help File of Delphi

Classtype dynamically determines the type
Of an object and returns its class reference, or metaclass. The results below show that the values of Aclass and bclass are the same, and they are the addresses of tform1 in the memory.

Here, if
Self is Aclass for testing, you have to go to true.

 

If
Is Aclass. The test result is true.


 


The following code shows how to use a class name to create an instance object of a class.

 

 
Tbaseclass = Class of tbase;

 
Tbase = Class (tpersistent)

 

 
End;

 

 
Tderivea = Class (tbase)

 
End;

 
Tderiveb = Class (tbase)

 
End;

Procedure tform1.btn2click (Sender:
Tobject );

VaR aobj, bobj: tbase;


Aclassname, bclassname: string;


Baseclass: tbaseclass;

Begin

 
Registerclasses ([tderive1, tderive2]);

 
Aclassname: = 'tderive1 ';

 
Bclassname: = 'tderive2 ';

 
Baseclass: = tbaseclass (findclass (aclassname ));

 
Aobj: = baseclass. Create;

 
Mmo1.lines. Add (aobj. classname );

 

 
Baseclass: = tbaseclass (findclass (bclassname ));

 
Bobj: = baseclass. Create;

 
Mmo1.lines. Add (bobj. classname );

 
Aobj. Free;

 
Bobj. Free;

End;

Registerclass and registerclasses are used to register our custom classes with the system. These classes must be the findclass inherited from tpersistent or its derived classes to obtain a class that has been registered with the system, it returns a value of the tpersistentclass type, which is a class of referenced variables.

The following results show that the aobj and bobj objects of the tderive1 class are successfully created by using the class names 'tderive1' and 'tderive12' respectively.


 

 

We usually define our own classes in the new unit. We only need to call registerclass in the initial code segment of the Unit to register our own classes, you can create objects of these classes based on the class name in other units. Other units only need to reference the units of the custom base class.
Instead of referencing the units that we define the child classes, we do not have to change the reference relationships of other units when new child classes are added!

 

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.