合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[success]百度网盘中可下载源码 源码文件名: 附加信息TList泛型扩展.zip ``` unit uSimpleListEx; interface uses System.Classes, Generics.Collections; type TSimpleListEx<T> = class(TList<T>) protected procedure FreeItem(AItem: T); virtual; public procedure ClearAndFreeAllItems; destructor Destroy; override; end; TClassSimpleListEx<T: Class, constructor> = class(TSimpleListEx<T>) protected procedure FreeItem(AItem: T); override; public function AddNewOne: T; // 创建一个新的 Item end; implementation { TSimpleListEx<T> } procedure TSimpleListEx<T>.ClearAndFreeAllItems; var Q: T; begin for Q in self do begin FreeItem(Q); end; Clear; end; destructor TSimpleListEx<T>.Destroy; begin ClearAndFreeAllItems; inherited; end; procedure TSimpleListEx<T>.FreeItem(AItem: T); begin end; { TClassSimleListEx<T> } function TClassSimpleListEx<T>.AddNewOne: T; begin Result := T.Create; add(Result); end; procedure TClassSimpleListEx<T>.FreeItem(AItem: T); begin inherited; AItem.free; // 已经知道他是 TObject ,就这样释放 end; end. ``` ***** ``` unit uMyWorkList; interface uses SysUtils, uSimpleListEx; type TMyWorkItemBase = class public function ToDetail: string; virtual; public ID: integer; end; TMyWorkItemOne = class(TMyWorkItemBase) public function ToDetail: string; override; public Age: integer; Info: string; end; TMyWorkItemList<T: TMyWorkItemBase, constructor> = class(TClassSimpleListEx<T>) public function GetItemById(AId: integer): T; end; TMyWorkItemOneList = class(TMyWorkItemList<TMyWorkItemOne>); implementation { TMyWorkItemList<T> } function TMyWorkItemList<T>.GetItemById(AId: integer): T; var Q: T; begin Result := nil; for Q in self do begin if Q.ID = AId then begin Result := Q; break; end; end; end; { TMyWorkItemBase } function TMyWorkItemBase.ToDetail: string; begin Result := '-------------'#13#10'id:' + inttostr(ID); end; { TMyWorkItemOne } function TMyWorkItemOne.ToDetail: string; var r: string; begin r := inherited; Result := r + #13#10; Result := Result + 'age:' + inttostr(Age) + #13#10; Result := Result + 'Info:' + Info; end; end. ```