objs.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:2k
- unit objs;
- interface
- uses classes,strutils,sysutils,Variants,Contnrs;
- type
- //去掉了TOrderedList的Create和Destroy方法。不先创建FList。
- //从而,可以在子类中子类化FList的类型。
- TCustomOrderList=class(TObject)
- private
- FList: TList;
- protected
- procedure PushItem(AItem: Pointer); virtual; abstract;
- function PopItem: Pointer; virtual;
- function PeekItem: Pointer; virtual;
- property List: TList read FList Write FList;
- public
- function Count: Integer;
- function AtLeast(ACount: Integer): Boolean;
- function Push(AItem: Pointer): Pointer;
- function Pop: Pointer;
- function Peek: Pointer;
- end;
- //TCustomObjectList=class
- TCustomQueue=class(TCustomOrderList)
- protected
- procedure PushItem(AItem: Pointer); override;
- end;
- implementation
- { TCustomOrderList }
- function TCustomOrderList.AtLeast(ACount: integer): boolean;
- begin
- Result := List.Count >= ACount;
- end;
- function TCustomOrderList.Peek: Pointer;
- begin
- Result := PeekItem;
- end;
- function TCustomOrderList.Pop: Pointer;
- begin
- Result := PopItem;
- end;
- function TCustomOrderList.Push(AItem: Pointer): Pointer;
- begin
- PushItem(AItem);
- Result := AItem;
- end;
- function TCustomOrderList.Count: Integer;
- begin
- Result := List.Count;
- end;
- function TCustomOrderList.PeekItem: Pointer;
- begin
- Result := List[List.Count-1];
- end;
- function TCustomOrderList.PopItem: Pointer;
- begin
- Result := PeekItem;
- List.Delete(List.Count-1);
- end;
- { TCustomQueue }
- procedure TCustomQueue.PushItem(AItem: Pointer);
- begin
- List.Insert(0, AItem);
- end;
- end.