democollection.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. program democollection;
  2. uses browserconsole, sysutils, classes;
  3. Type
  4. { TMyCollection }
  5. TMyCollectionItem = class(TCollectionItem)
  6. private
  7. FMyName: String;
  8. Published
  9. Property MyName : String Read FMyName Write FMyName;
  10. end;
  11. { TMyCollectionItems }
  12. TMyCollectionItems = Class(TCollection)
  13. private
  14. function GetMI(AIndex : Integer): TMyCollectionItem;
  15. procedure SetMI(AIndex : Integer; AValue: TMyCollectionItem);
  16. Public
  17. Function AddItem : TMyCollectionItem;
  18. Property MyItems[AIndex : Integer] : TMyCollectionItem Read GetMI Write SetMI;default;
  19. end;
  20. { TMyCollectionItems }
  21. function TMyCollectionItems.GetMI(AIndex : Integer): TMyCollectionItem;
  22. begin
  23. Result:=Items[AIndex] as TMyCollectionItem;
  24. end;
  25. procedure TMyCollectionItems.SetMI(AIndex : Integer; AValue: TMyCollectionItem);
  26. begin
  27. Items[AIndex]:=AValue;
  28. end;
  29. function TMyCollectionItems.AddItem: TMyCollectionItem;
  30. begin
  31. Result:=Add as TMyCollectionItem;
  32. end;
  33. Procedure DumpCollection(C : TMyCollectionItems);
  34. Var
  35. S : String;
  36. I : integer;
  37. begin
  38. S:='';
  39. For I:=0 to C.Count-1 do
  40. begin
  41. If S<>'' then
  42. S:=S+', ';
  43. S:=S+'['+IntToStr(I)+'] : '+C[i].MyName;
  44. end;
  45. Writeln('Fruit collection: ',s);
  46. end;
  47. Procedure DumpCollection2(C : TMyCollectionItems);
  48. Var
  49. S : String;
  50. Itm : TCollectionItem;
  51. begin
  52. S:='';
  53. For Itm in C do
  54. begin
  55. If S<>'' then
  56. S:=S+', ';
  57. S:=S+'['+IntToStr(Itm.Index)+'] : '+TMyCollectionItem(Itm).MyName;
  58. end;
  59. Writeln('Fruit collection: ',s);
  60. end;
  61. {
  62. Const
  63. MyNames : Array [1..10] of string
  64. = ('apple','pear','banana','orange','lemon','prune',
  65. 'pineapple','strawberry','raspberry','cherry');
  66. }
  67. Function GetName(I : integer) : String;
  68. begin
  69. Case I of
  70. 1 : Result:='apple';
  71. 2 : Result:='pear';
  72. 3 : Result:='banana';
  73. 4 : Result:='orange';
  74. 5 : Result:='lemon';
  75. 6 : Result:='prune';
  76. 7 : Result:='pineapple';
  77. 8 : Result:='strawberry';
  78. 9 : Result:='raspberry';
  79. 10 : Result:='cherry';
  80. end;
  81. end;
  82. Var
  83. MyC : TMyCollectionItems;
  84. C : TMyCollectionItem;
  85. I : Integer;
  86. begin
  87. MyC:=TMyCollectionItems.Create(TMyCollectionItem);
  88. For I:=1 to 10 do
  89. begin
  90. C:=MyC.AddItem;
  91. C.MyName:=GetName(i);
  92. end;
  93. DumpCollection(MyC);
  94. Writeln('pruning prune');
  95. MyC.Delete(5);
  96. DumpCollection(MyC);
  97. Writeln('Prefer banana over pear');
  98. MyC.Exchange(1,2);
  99. DumpCollection2(MyC);
  100. Writeln('Indigestion, no more fruit');
  101. MyC.Clear;
  102. DumpCollection2(MyC);
  103. end.