======================================================
注:本文源代码点此下载
======================================================
delphi 2009在delphi程式語言方面加入了兩個主要的功能,一個是泛型程式設計(generics programming),另外一個就是匿名方法(anonymous method)。delphi 2009在win32加入了泛型程式設計之後,delphi程式語言便可以同時在win32,.net平台下使用泛型程式設計。由於delphi 2009在delphi程式語言本身加入了泛型程式設計,因此在delphi rtl中也加入了一些新的泛型容器類別(generic container class)以方便開發人員使用於日常的開發程式碼中。本文將為讀者簡單的介紹如何使用使用這些新的泛型容器類別,希望能夠幫助讀者快速學習上手。
簡單的說,泛型程式設計允許開發人員撰寫可泛用的運算法則,這些運算法則能夠使用於各種不同的資料型態,因此一旦開發人員開發完成這些泛用的運算法則,其他的開發人員就可以根據需要的處理資料型態帶入泛用的運算法則來完成運算的工作。例如delphi最早提供的tlist容器類別在實作時由於寫死是使用 tobject的資料型態(類別型態),因此開發人員只能在tlist中使用tobject樣例。但是tlist容器提供的運算法則其實是能夠應用於任何型態的,而且開發人員在許多情形中也希望能夠使用tlist來暫時處理一些程式碼中的物件,而不只是tobject樣例。
因此所謂泛型的 tlist就是讓tlist提供的運算法則能夠適用於各種不同的資料型態(類別型態),例如讓tlist可以處理字串,整數,浮點數或是任何開發人員定義的類別型態。在一般的程式語言中泛型是使用符號來代表的,其中的代表type或是template的意思,因此泛型的tlist就是:
tlist += tlist
因此要讓tlist處理字串,就使用tlist,
因此要讓tlist處理tcomponent,就使用tlist,
以此類推,所以使用泛型程式設計並不困難,把代換成你要使用的資料型態(類別型態)即可。
當然,在要使用泛型容器類別之前也是需要建立它們才能使用,在建立泛型容器類別時,記得也要代入你建立泛型容器類別之後要在其中使用的資料型態(類別型態),例如前面的例子中,要讓tlist處理字串就必須如下的建立tlist泛型容器類別物件:
var
ltstring : tlist;
begin
…
tlstring := tlist.create;
讓tlist處理tcomponent必須如下的建立tlist泛型容器類別物件:
var
tlcomponent: tlist;
begin
…
tlcomponent := tlist.create;
以此類推。
下面列出了delphi 2009中提供的泛型容器類別:
tlist,tobjectlist
tqueue,tobjectqueue
tstack,tobjectstack
tdictionary,tobjectdictionary
上面的泛型容器類別使用方法和前面介紹的tlist差不多,只是不同的泛型容器類別提供了對於其中包含的元素不同的運算法則。
其中需要稍為解釋的是為什麼每一個泛型容器類別都有一個對應的object泛型容器類別?例如tlist既然已經能夠處理任何的型態,包含了類別型態,那麼為什麼還需要tobjectlist呢? 沒錯tlist是能夠放入任何的資料型態/類別型態,而tlist和tobjectlist的差別是tobjectlist能夠自動管理在tobjectlist中包含的物件的生命週期,簡單的說 tobjectlist能夠在本身的樣例釋放時自動釋放它管理的所有物件樣例。
例如tobjectlist有如下的建構函式原型:
constructor create(aownsobjects: boolean = true); overload;
我們可以看到它的建構函式接受一個aownsobjects的參數,它的內定值為true,這代表一旦開發人員建立了tobjectlist物件之後,如果在其中放入物件樣例,那麼tobjectlist物件在被釋放時也會釋放它包含的所有物件樣例。例如假設現在我們有一個tproduct類別,如果我們使用tobjectlist來管理tproduct類別,那麼可以使用如下的程式碼:
var
aproduct: tproduct;
ptlist : tobjectlist;
begin
…
ptlist := tobjectlist.create;
try
ptlist.add(tproduct.create(‘delphi’));
ptlist.add(tproduct.create(‘bcb’));
ptlist.add(tproduct.create(‘jbuilder’));
…
finally
ptlist.free;
end;
..
那麼當上面的程式碼執行到ptlist.free時,ptlist中包含的3個tproduct物件也會自動被釋放。當然,如果您不希望 tobjectlist自動刪除其中管理的物件,那麼在建立時傳遞false給它的建構函式做為參數,或是在建立之後設定它的 ownsobjects特性值為false也可以。
有了這些基本的知識之後,讓我們使用一個簡單的範例來說明如何使用 tobjectlist和tenumerator,tdictionary以及 tcomparer等泛型容器類別,在讀者瞭解了如何使用這四個類別之後對於使用其他的泛型容器類別應該就非常簡單了。
使用tobjectlist和tenumerator
為了說明起見,讓我們定義一個tproduct類別如下:
tproduct = class
private
fname : string;
fcode : string;
fcategory : string;
fversion : double;
public
constructor create(sname, scode : string; dversion : double; scategory : string = 'delphi');
destructor destroy; override;
function getname : string;
function getcode : string;
function getcategory : string;
function getversion : double;
end;
接著我們建立一個tobjectlist:
procedure tform17.formcreate(sender: tobject);
begin
glproduct := tobjectlist.create(true);
end;
delphi 2009的泛型容器類別是定義在一個新的程式單元generics.collections之中,因此當然要記得先在uses句子中加入使用generics.collections。
接著其中建立一些範例tproduct物件:
procedure tform17.createtempproducts;
begin
glproduct.add(tproduct.create('delphi 2009', 'tiburon', 12.0));
glproduct.add(tproduct.create('delphi 2007', 'highlander', 11.5));
glproduct.add(tproduct.create('delphi 2006', 'dexter', 11.0));
glproduct.add(tproduct.create('c++builder 2009', 'tiburon', 12.0, 'bcb'));
glproduct.add(tproduct.create('c++builder 6', 'riptide', 6.0, 'bcb'));
glproduct.add(tproduct.create('c++builder 5', 'rampage', 5.0, 'bcb'));
end;
在一般使用泛型容器類別時,最常應用的情形是需要從泛型容器類別中一一取出它包含的元素來處理。在這種應用中大都是使用tenumerator物件來幫助開發人員存取其中的元素,一般來說泛型容器類別都會提供getenumerator方法讓開發人員取得泛型容器類別對應的tenumerator物件,再藉由tenumerator物件來一一存取其中的元素。
在delphi 2009中tenumerator有如下的宣告:
tenumerator = class abstract
protected
function dogetcurrent: t; virtual; abstract;
function domovenext: boolean; virtual; abstract;
public
property current: t read dogetcurrent;
function movenext: boolean;
end;
其中開發人員藉由呼叫movenext方法來判斷是否還有未存取的元素,而current特性可以讓開發人員取得目前的元素。
在tlist類別中也定義了getenumerator方法可以取得它相關的tenumerator物件:
function getenumerator: tenumerator; reintroduce;
例如假設現在我們希望把剛才加入的所有tproduct物件顯示在tlistbox中,那麼就可以使用如下的程式碼:
procedure tform17.btn使用enumeratorclick(sender: tobject);
var
aenum : tenumerator;
begin
aenum := glproduct.getenumerator;
displayproducts(aenum);
end;
首先我們呼叫tobjectlist的getenumerator取得tenumerator物件,由於這個tenumerator物件是存取 tproduct物件,因此它的型態定義是tenumerator,也就是把代換成。
接著displayproducts就藉由tenumerator物件進入while迴圈,如果 tenumerator物件的movenext持續回傳true就代表尚有未存取的tproduct。在while迴圈中開發人員可以藉由 tenumerator物件的current特性值取得目前要處理的tproduct物件再進行後續的處理工作。
procedure tform17.displayproducts(aenum: tenumerator);
begin
lb產品資料.items.clear;
while (aenum.movenext) do
begin
lb產品資料.items.add(aenum.current.getname + ' : ' + aenum.current.getcode);
end;
end;
排序泛型容器類別
另外一個經常使用的情形是需要排序泛型容器類別之中的元素,由於泛型容器類別可以包含任何型態的物件,因此如何排序其中的元素物件應該是根據不同的型態而異的,因此開發人員必須提供如何排序的程式碼給泛型容器類別來呼叫以決定元素之間的次序。
tobjectlist提供了兩個排序方法:
procedure sort; overload;
procedure sort(const acomparer: icomparer); overload;
其中第一個sort是使用內定的排序方式,開發人員如果需要定義自己的排序法則就需要使用第二個sort方法。第二個sort方法接受一個icomparer介面的參數,icomparer定義如下:
icomparer = interface
function compare(const left, right: t): integer;
end;
icomparer介面定義了compare方法,它回傳整數,-1代表小於,0代表等於而1代表大於。
因此開發人員需要實作一個實作icomparer介面的類別,再把這個類別的物件傳遞給上面的第二個sort方法來實際的排序。
實作icomparer最簡單的方法是定義一個從tcomparer類別繼承下來的子類別,因為delphi2 2009已經定義了如下的tcomparer類別:
tcomparer = class(tinterfacedobject, icomparer)
public
class function default: icomparer;
class function construct(const comparison: tcomparison): icomparer;
function compare(const left, right: t): integer; virtual; abstract;
end;
因此我們要排序glproduct之中的tproduct物件,讓我們定義tproductlinecomparer類別,它從tcomparer類別繼承下,我們只需要複載實作compare來撰寫如何排序tproduct物件。在下面的程式碼中我們先以tproduct的 category特性值來排序,如果category特性值相同,就以tproduct的version特性值來做子排序條件:
type
tproductlinecomparer = class(tcomparer)
public
function compare(const left, right: tproduct): integer; override;
end;
implementation
{ tproductlinecomparer }
function tproductlinecomparer.compare(const left, right: tproduct): integer;
begin
result := 0;
if (left.getcategoryright.getcategory) then
result := 1
else
begin
if (left.getversionright.getversion) then
result := 1;
end;
end;
有了tproductlinecomparer之後就可以使用下面的程式碼來排序glproducts之中所有tproduct物件的次序了:
procedure tform17.btn依產品線排序click(sender: tobject);
var
aplc : tproductlinecomparer;
begin
aplc := tproductlinecomparer.create;
try
glproduct.sort(aplc);
displayproducts(glproduct.getenumerator);
finally
aplc.free;
end;
end;
上面的程式碼先建立tproductlinecomparer物件,傳遞給sort做為參數即可。
下圖是先呼叫tenumerator顯示glproducts中所有的tproduct物件,讀者可以看到物件是以我們加入glproducts之中的次序顯示,而呼叫依產品線排序之後tproduct物件就以正確的產品種類+產品版本的次序來顯示。
結合匿名方法
現在讓我們稍為展示如何結合泛型和匿名方法。
delphi 2009新的程式語言功能之一就是匿名方法,匿名方法主要的功能是允許開發人員在程式碼中定義一些小而簡單的程式碼來使用,而這些小而簡單的程式碼不需要正式定義成方法,而是使用完之後就不再需要的情形之中。此外匿名方法可以和泛型程式設計一起使用。現在讓我們再對glproducts中的 tproduct進行排序,但是我們只需要對delphi的產品排序。
因此我們需要先過濾出delphi的產品物件,再根據過濾出delphi的產品物件進行排序,下面的程式碼就可以完成這個工作:
001procedure tform17.btn依delphi版本排序click(sender: tobject);
002var
003adelphifilter : tfunc;
004ltdelphi: tlist;
005aenum : tenumerator;
006aplc: tproductlinecomparer;
007begin
008ltdelphi := tlist.create;
009try
010aenum := glproduct.getenumerator;
011adelphifilter := function (aproduct : tproduct) : boolean
012begin
013result := false;
014if (aproduct.getcategory = 'delphi') then
015result := true;
016end;
017
018while aenum.movenext do
019begin
020if (adelphifilter(aenum.current)) then
021ltdelphi.add(aenum.current);
022end;
023
024aplc := tproductlinecomparer.create;
025try
026ltdelphi.sort(aplc);
027self.lb產品資料.items.add('========依delphi產品線排序==========');
028displayproducts(ltdelphi.getenumerator);
029finally
030aplc.free;
031end;
032finally
033ltdelphi.free;
034end;
035 end;
上面程式碼的邏輯很簡單,我們使用一個匿名方法從glproduct中過濾出delphi的產品,放入另外一個tlist泛型容器類別之中,最後再使用前面介紹的tproductlinecomparer來排序。
在上面的程式碼中,003行的adelphifilter宣告的型態是tfunc,那麼是tfunc呢? tfunc就是一個接受泛型的匿名方法,它定義在sysutils程式單元中,它的原型如下:
tfunc = reference to function (arg1: t): tresult;
事實上在delphi 2009的sysutils程式單元中已經定義了許多通用的泛型匿名方法可以讓開發人員使用,它們的定義如下:
// generic anonymous method declarations
type
tproc = reference to procedure;
tproc = reference to procedure (arg1: t);
tproc = reference to procedure (arg1: t1; arg2: t2);
tproc = reference to procedure (arg1: t1; arg2: t2; arg3: t3);
tproc = reference to procedure (arg1: t1; arg2: t2; arg3: t3; arg4: t4);
tfunc
= reference to function: tresult;
tfunc = reference to function (arg1: t): tresult;
tfunc = reference to function (arg1: t1; arg2: t2): tresult;
tfunc = reference to function (arg1: t1; arg2: t2; arg3: t3): tresult;
tfunc = reference to function (arg1: t1; arg2: t2; arg3: t3; arg4: t4): tresult;
tpredicate = reference to function (arg1: t): boolean;
回到前面的程式碼中,在011到016定義了匿名方法並且指定給匿名方法變數adelphifilter,接著藉由tenumerator物件一一的存取 glproduct中的元素,並且使用adelphifilter來過濾出delphi的產品,最後在026行呼叫ltdelphi的sort方法並且傳入tproductlinecomparer做為參數。
下面是執行這個方法的結果畫面:
ok,下次讓我解釋如何結合使用tdictionary以及匿名方法更為技術面的內容,have fun!。
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/