"Mastering Delphi 6" study notes
In C , we can use Static to declare a function that belongs to a particular instance of a class is not a class. Many people think that Object Pascal does not have a similar feature, in fact, as long as the function declares is the class procedure or class function. E.g:
TFORM1 = Class (TFORM)
public
Class Procedure T;
END;
IMPLEMentation
Class Procedure TFOMR1.T;
Begin
END;
You can verify that it is really static in the project file:
Begin
TFORM1.T;
Application.INITIALZIE;
Application.createform (TFORM1, FORM1);
Application.run;
END;
Class Procedure / Class Function is also a very important type of special way in VCL. TOBJECT ClassName, ClassNameis, ClassPoint, ClassInfo These four methods are all Class function. Although you don't use them in actual programming 99.999999999%, these four functions are the cornerstone of the entire VCL.
It is worth mentioning that the Static Member Function in C is different, and the Class Method in Object Pascal can have a polymorphic nature. For example, classname is a class method of TOBJECT, but you call TForm.ClassName is 'tform', call TButton.className is 'TButton', and so on. The static method can do polymorphism, isn't it very magical? All this is done through RTTI. You can imagine that if classname declares in a general Virtual Method, you must overload it in each derived class to realize the correct mapping of the class name, which will be a painful thing.
Class Reference is a concept that is not known in the object pascal. Its syntax is simple, for example,
TCLASS = Class of TOBJECT;
What is the use of Class Reference? Use a small example to explain. Place a RadioGroup on the Form, including Radio Button, Check Box, button. code show as below:
Type
TFORM1 = Class (TFORM)
...
Private
ControlRef: tcontrolclass;
Counter: integer;
...
END;
IMPLEMentation
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
COUNTER: = 0;
END;
Procedure TFORM1.RADIOGROUP1CLICK (Sender: TOBJECT);
Begin
Case Radiogroup1.ItemIndex of
0: ControlRef: = tradiobutton;
1: ControlRef: = tCheckbox;
2: ControlRef: = Tbutton;
END;
END;
Procedure TForm1.FormMousedown (Sender: Tobject; Button: TMouseButton; Shift: TshiftState; x, y: integer; var
Control: tcontrol;
ControlName: strin;
Begin
if Button = Mblef Then Begin
Control: = tControlRef.create (Self);
WITH Control Do Begin
Visible: = false;
Parent: = Self;
LEFT: = X;
Top: = Y;
ControlName: = Control.className INTSTOSTR (Counter);
DELETE (ControlName, 1, 1);
Name: = controlname;
Visible: = true;
END;
END;
END;
Class Reference reveals an amazing ability to Object Pascal. In the general concept of the oo language, the constructor cannot be Virtual. We call TButton.create to build button, TForm.create is TForm, and so on. However, from the Class Reference, VCL has the ability of Virtual Construction, in the above code, through a separate controlRef.create, we build a derived object of any TControl. It is because of this capability, Form Designer and Object Inspector have a way to operate a variety of different types of components, regardless of these components are Delphi built-in, or we get from elsewhere, or we have written themselves. The concept of Class Reference is also known elsewhere as Meta-Class, which is a special type used to describe information. In fact, the real skelist hero is RTTI, through RTTI, not only makes the class with the Virtual Construction function, but also the Class Method in the class also has the nature of Virtual. For example, the classname method in TOBJEC is declared as a class function classname: String, and the static method is impossible to do polymorphism, but in fact, we call TButton.classname to get TButton, call tform.classname to get it is TFORM, through RTTI, impossible things becomes possible, and avoid troubleload Classname in each derived class.
In the MFC, if an object satisfies the following conditions: 1. It is derived from COBJECT; 2. It contains declare_dynamic, and implements Implement_Dynamic in some place, which can also achieve similar features via getRuntimeClass () -> CreateObject. The difference is that the implementation method of the MFC is to establish a complete CRUNTIMECLASS table through macro in memory, while Object Pascal, all objects have these functions due to individual inheritance features. The MFC's memory form is established by the declaration of the program, and the RTTI table in the Object Pascal is automatically generated by the compiler, and the method is different from each other, but the thought is basically consistent.
Through RTTI, not only the object can be generated, but the properties of the object can also be dynamically set. The following code is not from "Mastering Delphi 6", but from Mr. Chen Xida's "Delphi Deep Adventures", it also reveals powerful features and flexibility with Object Pascal through RTTI: VAR
i: integer;
PROPINFO: PPROPINFO;
Begin
For i: = 0 to Componentcount-1 Do Begin
PROPINFO: = GetPropinfo (Components [i] .classinfo, 'color');
IF propinfo <> nil dam
SetorProp (Components [i], propinfo, clred;
END;
END;