About class helper... Syntax

Source: Internet
Author: User
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.

  

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.