delphi 泛型使用很是折腾,和c++的差得太远。特别是类模板的使用,创建很是麻烦,还好可以变通的写出来。
以下举个例子,如何使用类模板。
unit test1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Vcl.Dialogs, Generics.Collections, Vcl.StdCtrls, Vcl.Buttons;
type
TForm4 = class(TForm)
btn1: TBitBtn;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TValue = class //基类
public
function Name: string; virtual; //虚方法
end;
TValue1= class(TValue)
public
function Name: string; override; //覆盖基类虚方法
end;
TTest = class
public
procedure Method<T: TValue>; //测试方法1,用基类做模板类型,可以传入任意子类
procedure Method2<T: class>; //测试方法2,用class关键字,可以传入任意类
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
{ TV