使用泛型, 写一个为任意类型的动态数组添加元素的方法

2014/09/26 15:33
阅读数 330

一、使用泛型类:

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<T> = class
    class procedure ArrayAdd(var Arr: TArray<T>; const item: T);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TArr<T> }

class procedure TArr<T>.ArrayAdd(var Arr: TArray<T>; const item: T);
begin
  SetLength(Arr, Length(Arr)+1);
  Arr[High(Arr)] := item;
end;

//测试
procedure TForm1.FormCreate(Sender: TObject);
var
  arr1: TArray<string>;
  arr2: TArray<Integer>;
begin
  TArr<string>.ArrayAdd(arr1, 'abc');
  TArr<Integer>.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<T> = record //record
    class procedure ArrayAdd(var Arr: TArray<T>; const item: T); static; //结构中的 class 方法必须是 static 的
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TArr<T> }

class procedure TArr<T>.ArrayAdd(var Arr: TArray<T>; const item: T);
begin
  SetLength(Arr, Length(Arr)+1);
  Arr[High(Arr)] := item;
end;

//测试
procedure TForm1.FormCreate(Sender: TObject);
var
  arr1: TArray<string>;
  arr2: TArray<Integer>;
begin
  TArr<string>.ArrayAdd(arr1, 'abc');
  TArr<Integer>.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<T>(var Arr: TArray<T>; const item: T); static;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TArr }

class procedure TArr.ArrayAdd<T>(var Arr: TArray<T>; const item: T);
begin
  SetLength(Arr, Length(Arr)+1);
  Arr[High(Arr)] := item;
end;

//测试
procedure TForm1.FormCreate(Sender: TObject);
var
  arr1: TArray<string>;
  arr2: TArray<Integer>;
begin
  TArr.ArrayAdd<string>(arr1, 'abc');
  TArr.ArrayAdd<Integer>(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<T>(var Arr: TArray<T>; const item: T); static;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ Txxx }

class procedure Txxx.ArrayAdd<T>(var Arr: TArray<T>; const item: T);
begin
  SetLength(Arr, Length(Arr)+1);
  Arr[High(Arr)] := item;
end;

//测试
procedure TForm1.FormCreate(Sender: TObject);
var
  arr1: TArray<string>;
  arr2: TArray<Integer>;
begin
  TArray.ArrayAdd<string>(arr1, 'abc');
  TArray.ArrayAdd<Integer>(arr2, 123);
  ShowMessageFmt('%s,%d', [arr1[0], arr2[0]]); //abc,123
end;

end.


总结:

1、Delphi 的泛型方法只能属于一个类或结构, 这是好事, 也应该是 Delphi 所提倡的; 这便于管理、也便于快速输入.

2、稍稍扩充一下就可让动态数组和其它强大的列表类比拼一下了.

3、这也像是 C++ 中的算法了, 按这个思路应该可以把许多 C++ 中的算法移植过来.

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部