一、使用泛型类:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure
FormCreate(Sender: TObject);
end;
TArr = class
class
procedure ArrayAdd(var Arr: TArray; const item: T);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TArr }
class procedure TArr.ArrayAdd(var Arr: TArray; const item:
T);
begin
SetLength(Arr, Length(Arr)+1);
Arr[High(Arr)] := item;
end;
//测试
procedure TForm1.FormCreate(Sender: TObject);
var
arr1: TArray;
arr2: TArray;
begin
TArr.ArrayAdd(arr1, 'abc');
TArr.ArrayAdd(arr2, 123);
ShowMessageFmt('%s,%d', [arr1[0], arr2[0]]);
//abc,123
end;
end.
二、使用泛型结构:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure
FormCreate(Sender: TObject);
end;
TArr = record //record
class
procedure ArrayAdd(var Arr: TArray; const item: T); static; //结构中的
class 方法必须是 static 的
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TArr }
class procedure TArr.ArrayAdd(var Arr: TArray; const item:
T);
begin
SetLength(Arr, Length(Arr)+1);
Arr[High(Arr)] := item;
end;
//测试
procedure TForm1.FormCreate(Sender: TObject);
var
arr1: TArray;
arr2: TArray;
begin
TArr.ArrayAdd(arr1, 'abc');
TArr.ArrayAdd(arr2, 123);
ShowMessageFmt('%s,%d', [arr1[0], arr2[0]]);
//abc,123
end;
end.
三、在类或结构中建立泛型方法:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure
FormCreate(Sender: TObject);
end;
TArr = record
class
procedure ArrayAdd(var Arr: TArray; const item: T); static;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TArr }
class procedure TArr.ArrayAdd(var Arr: TArray; const item:
T);
begin
SetLength(Arr, Length(Arr)+1);
Arr[High(Arr)] := item;
end;
//测试
procedure TForm1.FormCreate(Sender: TObject);
var
arr1: TArray;
arr2: TArray;
begin
TArr.ArrayAdd(arr1, 'abc');
TArr.ArrayAdd(arr2, 123);
ShowMessageFmt('%s,%d', [arr1[0], arr2[0]]);
//abc,123
end;
end.
四、扩充 System.Generics.Collections 单元中的 TArray 类:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
System.Generics.Collections;
type
TForm1 = class(TForm)
procedure
FormCreate(Sender: TObject);
end;
Txxx = class helper for TArray
class
procedure ArrayAdd(var Arr: TArray; const item: T); static;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ Txxx }
class procedure Txxx.ArrayAdd(var Arr: TArray; const item:
T);
begin
SetLength(Arr, Length(Arr)+1);
Arr[High(Arr)] := item;
end;
//测试
procedure TForm1.FormCreate(Sender: TObject);
var
arr1: TArray;
arr2: TArray;
begin
TArray.ArrayAdd(arr1, 'abc');
TArray.ArrayAdd(arr2, 123);
ShowMessageFmt('%s,%d', [arr1[0], arr2[0]]);
//abc,123
end;
end.
总结:
1、Delphi 的泛型方法只能属于一个类或结构, 这是好事, 也应该是 Delphi 所提倡的;
这便于管理、也便于快速输入.
2、稍稍扩充一下就可让动态数组和其它强大的列表类比拼一下了.
3、这也像是 C++ 中的算法了, 按这个思路应该可以把许多 C++ 中的算法移植过来.
posted @
2013-05-07 12:40
Wishmeluck
阅读(
199)
评论()
编辑
收藏
举报