作用
对现有的类进行扩展,如果把类理解为一个容器,那么这条语法的作用是对现有的类的容器的成员进行扩展或者修改。
有点像lua中的类,可以取到类的table,对table修改。
语法
TMyClass = class
public
procedure print;
end
TMyClassHelper = class helper for TMyClass
private
procedure show; //理解为:对TMyClass进行扩展
public
procedure print; //理解为:覆盖掉了TMyClass的print方法
end
上一个简单的例子
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TClassA = class
private
tvClassId : Integer;
function GetClassId: Integer;
public
procedure print();
property ClsId: Integer read GetClassId write tvClassId;
end;
TClassAHelper = class helper for TClassA
private
function GetClassName: string;
public
procedure print();
property ClsName: string read GetClassName;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TClassA }
function TClassA.GetClassId: Integer;
begin
Result := tvClassId;
end;
procedure TClassA.print;
begin
ShowMessage('TClassA.print');
end;
{ TClassAHelper }
function TClassAHelper.GetClassName: string;
begin
Result := 'ClassAHelper';
end;
procedure TForm2.btn1Click(Sender: TObject);
var
obj: TClassA;
begin
obj := TClassA.Create;
obj.ClsId := 1;
ShowMessage(IntToStr(obj.GetClassId));
ShowMessage(obj.GetClassName);
obj.print();
end;
procedure TClassAHelper.print;
begin
ShowMessage('TClassAHelper.print');
end;
end.
————————————————