delphi泛型模板编程
unit TxInfo;
interface
uses
System.Types,
System.Classes,
System.SysUtils,
Generics.Collections;
type
TPeople = record
Name: string;
Age: string;
end;
type
TTxInfo<T> = class
public
PeopleList: TList<T>;
public
constructor Create;
destructor Destroy; override;
private
function ProcessInfo(aAPeopleStr: string): T; virtual; abstract;
public
procedure GetInfo(AStr: string); overload;
end;
type
TMyNetInfo<T> = class(TTxInfo<T>)//泛型类可以继承
public
function ProcessInfo(APeopleStr: string): T; override;
end;
implementation
{ TTxHttp }
constructor TTxInfo<T>.Create;
begin
inherited Create();
PeopleList := TList<T>.Create;
end;
destructor TTxInfo<T>.Destroy;
begin
PeopleList.Free;
inherited Destroy;
end;
procedure TTxInfo<T>.GetInfo(AStr: string);
var
m_Info: T;
begin
PeopleList.Clear;
m_Info := ProcessInfo(AStr);
PeopleList.Add(m_Info);
end;
{ TMyNetInfo<TFilmInfoT> }
function TMyNetInfo<T>.ProcessInfo(APeopleStr: string): T;
begin
var pepole: TPeople;
pepole.Name:= APeopleStr;
Result:=T(Pointer(@pepole)^);//实转虚,泛型就是可以在虚实之间相互转换
end;
end.