About grammar class helper for ...

https://www.cnblogs.com/del/archive/2009/10/13/1582789.html

class helper may be from Delphi 2007 added new syntax, because the feeling is not practical, until today only test it.

After tried to know:! Very interesting basic function is to modify the existing class.

Txxx = class helper for T ... { T ... } represents the existing classes
  {existing methods may alternatively}
  {new method may also be members}
End;

When re-using a T ... // class and its descendant classes after this, will give priority to the use of modified Txxx.


Example One:
Unit Unit1;

interface

uses
  Windows, Messages, SysUtils, 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;

  Helper class for TMyClass = TMyClassHelper
    function func1: String; {will replace the same method on TMyClass swap}
    function func3: String; {will increase its subclasses the methods TMyClass}
  End;

was
  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;

//测试
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;

end.


Example Two: This Example TControl increased to a class method, after TControl and all of its descendants will have this method.
Unit Unit1;

interface

uses
  Windows, Messages, SysUtils, 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;

was
  Form1 TForm1;

implementation

{$R *.dfm}

{ TMyClassHelper }

TMyClassHelper.MyMsg Procedure;
the begin
  ShowMessageFmt ( 'class name is% s% s', [the Name, ClassName]);
End;

@ Test: This test current button and the current form, they are inherited from the TControl
Procedure TForm1.Button1Click (Sender: TObject);
the begin
  Self.MyMsg; the Form1 {} class name TForm1
  the TButton (Sender). MyMsg; {Button1 class name} TButton1
End;

end.

Published 444 original articles · won praise 25 · views 180 000 +

Guess you like

Origin blog.csdn.net/ozhy111/article/details/103344768