控制台程序代碼:
1program TestTObjectList;
2
3{$APPTYPE CONSOLE}
4
5uses
6 SysUtils,
7 Contnrs,
8 Generics.Collections,
9 Felix in 'Felix.pas';
10
11var
12 objList: TObjectList<TFelix>;
13 oldObjList: TObjectList;
14 n: Integer;
15 felix: TFelix;
16 pFelix: Pointer;
17begin
18 // 以下代碼測試舊對象集合
19 Writeln('TObjectList start');
20
21 oldObjList := TObjectList.Create; // 1*
22 for n := 0 to 2 do
23 begin
24 oldObjList.Add(TFelix.Create);
25 end;
26
27 for pFelix in oldObjList do
28 begin
29 Writeln(TFelix(pFelix).Id);
30 end;
31
32 FreeAndNil(oldObjList);
33
34 // 以下代碼測試泛型對象集合
35 Writeln(#13 + #10 + 'TObjectList<T> start');
36
37 objList := TObjectList<TFelix>.Create; // 2*
38 for n := 0 to 2 do
39 begin
40 objList.Add(TFelix.Create);
41 end;
42
43 for felix in objList do
44 begin
45 Writeln(felix.Id);
46 end;
47
48 FreeAndNil(objList);
49
50 // ----------------------------------------------------------
51 Writeln('press any key');
52 Readln;
53end.
圖3