程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi中的容器類(5)

Delphi中的容器類(5)

編輯:Delphi

擴展TList,限制類型的對象列表 

Begin Listing Two - TMyObjectList
TMyObject = class(TObject)
public
  procedure DOSomething;
end;
TMyObjectList = class(TObjectList)
protected
  function GetItems(Index: Integer): TMyObject;
  procedure SetItems(Index: Integer; AMyObject: TMyObject);
public
  function Add(aMyObject: TMyObject): Integer;
  procedure DOSomething;
  function Remove(aMyObject: TMyObject): Integer;
  function IndexOf(aMyObject: TMyObject): Integer;
  procedure Insert(Index: Integer; aMyObject: TMyObject);
  property Items[Index: Integer]: TMyObject
   read GetItems write SetItems; default;
end;
...
{ TMyObjectList }
function TMyObjectList.Add(AMyObject: TMyObject): Integer;
begin
 Result := inherited Add(AMyObject);
end;
procedure TMyObjectList.DOSomething;
var
 i: Integer;
begin
  for i := 0 to Count-1 do
  Items[i].DOSomething;
end;
function TMyObjectList.GetItems(Index: Integer): TMyObject;
begin
 Result := TMyObject(inherited Items[Index]);
end;
function TMyObjectList.IndexOf(AMyObject: TMyObject):
 Integer;
begin
 Result := inherited IndexOf(AMyObject);
end;
procedure TMyObjectList.Insert(Index: Integer;
 AMyObject: TMyObject);
begin
  inherited Insert(Index, AMyObject);
end;
function TMyObjectList.Remove(AMyObject: TMyObject):
 Integer;
begin
 Result := inherited Remove(AMyObject);
end;
procedure TMyObjectList.SetItems(Index: Integer;
 AMyObject: TMyObject);
begin
  inherited Items[Index] := AMyObject;
end;
End Listing Two

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved