program Project3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Classes;
type
TMyCollectionItem =
class(TCollectionItem)
private
// hier kannst du auch eigene Felder und Methoden definieren
protected
function GetDisplayName:
string;
override;
end;
TMyCollection =
class(TCollection)
protected
procedure Update(Item: TCollectionItem);
override;
procedure SetItemName(Item: TCollectionItem);
override;
procedure Notify(Item: TCollectionItem; Action: TCollectionNotification);
override;
public
procedure BeginUpdate;
override;
procedure EndUpdate;
override;
end;
procedure TMyCollection.BeginUpdate;
begin
inherited;
Writeln('
BeginUpdate');
end;
procedure TMyCollection.EndUpdate;
begin
inherited;
Writeln('
EndUpdate');
end;
procedure TMyCollection.Notify(Item: TCollectionItem; Action: TCollectionNotification);
begin
inherited;
case Action
of
cnAdded: Writeln(Item.DisplayName + '
added');
cnExtracting:
Writeln(Item.DisplayName + '
extracing');
cnDeleting:
Writeln(Item.DisplayName + '
deleting');
end;
end;
procedure TMyCollection.SetItemName(Item: TCollectionItem);
begin
inherited;
Writeln(Item.DisplayName + '
ItemName');
end;
procedure TMyCollection.Update(Item: TCollectionItem);
begin
inherited;
if Assigned(Item)
then
begin
// Ein einzelnes Item wurde befummelt
Writeln(Item.DisplayName + '
updated');
end else
// wenn Item = nil, dann ist die ganze Collection betroffen
end;
function TMyCollectionItem.GetDisplayName:
string;
begin
Result := '
HuiBui: ' + IntToStr(Self.ID);
end;
procedure Main;
var
MyCollection: TMyCollection;
MyItem: TCollectionItem;
I: Integer;
Something:
string;
begin
MyCollection := TMyCollection.Create(TMyCollectionItem);
try
MyCollection.BeginUpdate;
try
for I := 0
to 10 - 1
do
begin
MyItem := MyCollection.Add;
end;
finally
MyCollection.EndUpdate;
end;
Something := '
';
for I := 0
to MyCollection.Count - 1
do
begin
MyItem := MyCollection.Items[I];
Something := Something + MyItem.DisplayName + '
';
end;
Writeln(Something);
MyCollection.Items[5].DisplayName := '
asdf';
MyCollection.Delete(5);
finally
MyCollection.Free;
end;
end;
begin
try
Main;
Readln;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
end.
end.