Class of http://blog.csdn.net/blue_morning/article/details/8815609 Delphi class of usage This concept was originally made clear in a document about the introduction of Delphi RTTI. But there is no introduction to the actual use, after I understand the concept and how to use the decision to write a use note to facilitate the use of everyone.
When the class class is used:
When you know the parent class but need to create a specific subclass (you don't know what the subclass will be)
For example:
A Delphi EXE program in the project file Application.createform, tracking down the source code can understand, Delphi realized at all do not know what we will derive from the tform of what kind of case, the implementation of this class creation.
Key:
Tcomponentclass = Class of tcomponent;
Procedure Tapplication.createform (Instanceclass:tcomponentclass; var Reference);
Begin
Instance: = Tcomponent (instanceclass.newinstance);
Instance.create (self);
...
End
The key code is the bold two-sentence and class-declaration
Essence:
Class when declaring, the corresponding classes and subclasses are appended with additional information (RTTI) to the compiler to allow the system to find the create and newinstance addresses of the specific subclasses. That's what it should be.
Price:
The additional rtti information will make our class occupy extra memory, which is the cost of convenience.
Simple question Complex description
The problem has already been explained, but there is still a problem: where in our code do we need to use class of? I found the problem to be very complicated, and I would give an example of how I developed it. When doing database program development: I first define a Ttableset object, which functions like datamodule. It functions like Tdataset for placing the Texporttable,texporttable class. I have defined the basic operation of its increment, delete, change, check and so on. The Ttableset object has an Add method, presumably with the following code:
Procedure Ttableset.add (const Aexoprtobjectinfo:record)
Var
exprottable:texporttable;
Begin
Exprottable: = Texporttable.create (nil)
Exporttable objects are materialized according to Aexoprtobjectinfo data content for easy reuse of code
End
Then, in the specific business functions (such as inbound order management) need to inherit from the texporttable of an inbound single class
Tinstoragebill = Class (Texporttable)
Some specific class properties and methods
Overwrite the creation method of the texporttable to create the appropriate resource
End
After so much nonsense, the question finally came up: "How can I create a Tinstoragebill object in the Ttableset.add () method?" "Or in other words:" How do I create its indeterminate subclasses in the case of knowing the parent class? ”。 And you all know the answer.
Welcome to use class of
Class of Delphi Class of usage