Delphi-new syntax helper

Source: Internet
Author: User

Technical Exchange, DH explanation.

Haha, I haven't written this series for a long time. Today I'm watching VCLSource codeYou saw this.
I 'd like to declare that this is only available in Versions later than D7. Thank you, don't spray me!

When we want to add a method or member variable to a class, but this class is referenced by many units, it is unwise to directly access this class. What should we do?
By the way, if it is object-oriented, We can inherit it. It is OK. But what if it is a struct? In fact, this key is more like preparing for the struct.
But here I want to talk about the usage of a helper keyword.
Class assistant can extend a class without inheritance and does not exist in UML.

Simple Syntax:

 
TypeHelper class name= Class helper [(Base class list)]Extended Class NameMember Variable listEnd; we can see that it is very simple, that is, class helper. of course, here I have to talk about it. record is also acceptable. who is the nickname of a record. we can define multiple helper classes for a class, but extend the attributes of the original class.CodeThe helper class can only have 0 or 1.

Here is a simple example.
Type
Tmyclass = Class
Procedure Myproc;
Function Myfunc: integer;
End ;
...
Procedure Tmyclass. myproc;
VaR
X: integer;
Begin
X: = myfunc;
End ;
Function Tmyclass. myfunc: integer;
Begin
...
End ;
Next is its helper class.
Type
Tmyclasshelper = Class Helper For Tmyclass
Procedure Helloworld;
Function Myfunc: integer;
End ;
...
Procedure Tmyclasshelper. helloworld; // new method to be added
Begin
Writeln (self. classname );
End ;
Function Tmyclasshelper. myfunc: integer; // overwrite the original method
Begin
...
End ;
Usage:
VaR
X: tmyclass;
Begin
X: = tmyclass. Create;
X. myproc; // Call tmyclass. myproc
X. helloworld; // Call tmyclasshelper. helloworld
X. myfunc; // Call tmyclasshelper. myfunc
End .
It's that simple. I hope you can use it.

I am DH today;

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.