Class helper may be a new syntax added from Delphi 2007, because it is not very practical and has not been tested until today.
After you try it, you will find it interesting! The basic function is to modify an existing class.
TXXX = Class helper for t... {T... indicates that the existing class} {can replace the existing method} {can also have a new method, member} end; // then use t... TXXX is preferred for classes and their descendants.
Example 1:
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; tmyclass = Class function func1: string; function func2: string; end; tmyclasshelper = Class helper for tmyclass function func1: string; {Replace the tmyclass method with the same name} function func3: string; {Add this method to tmyclass and its subclass} end; var form1: tform1; implementation {$ R *. DFM} {tmyclass} function tmyclass. func1: string; begin result: = 'tmyclass. func1 '; end; function tmyclass. func2: string; begin result: = 'tmyclass. func2 '; end; {tmyclasshelper} function tmyclasshelper. func1: string; begin result: = 'tmyclasshelper. func1 '; end; function tmyclasshelper. func3: string; begin result: = 'tmyclasshelper. func3'; end; // Test Procedure tform1.button1click (Sender: tobject); var OBJ: tmyclass; begin OBJ: = tmyclass. create; showmessage (obj. func1); {tmyclasshelper. func1} showmessage (obj. func2); {tmyclass. func2} showmessage (obj. func3); {tmyclasshelper. func3} obj. free; end.
Example 2: In this example, a method is added to the tcontrol class, And tcontrol and all its child classes will have this method.
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; tmyclasshelper = Class helper for tcontrol procedure mymsg; end; var form1: tform1; implementation {$ R *. DFM} {tmyclasshelper} procedure tmyclasshelper. mymsg; begin showmessagefmt ('% S Class Name Is % s', [name, classname]); end; // test: the current form and button are tested here, they are all procedure tform1.button1click (Sender: tobject) inherited from tcontrol; Begin self. mymsg; {form1 class name is tform1} tbutton (sender ). mymsg; {button1 class name is tbutton1} end; end.