fork download
  1. {$MODE OBJFPC}{$LONGSTRINGS ON}
  2.  
  3. uses
  4. FGL;
  5.  
  6. type
  7. TCustomItem = class(TObject)
  8. public
  9. Name: String;
  10. end;
  11.  
  12. type
  13. TOrange = class(TCustomItem);
  14. TBanana = class(TCustomItem);
  15.  
  16. type
  17. TCustomItemClass = class of TCustomItem;
  18.  
  19. type
  20. TItems = specialize TFPGObjectList<TCustomItem>;
  21.  
  22. function CreateItem(AClass: TCustomItemClass; const AName: String): TCustomItem;
  23. begin
  24. Result := AClass.Create();
  25. Result.Name := AName;
  26. end;
  27.  
  28. var
  29. Items: TItems;
  30. Item: TCustomItem;
  31. begin
  32. Items := TItems.Create();
  33. try
  34. Items.Add(CreateItem(TOrange, 'orange'));
  35. Items.Add(CreateItem(TBanana, 'banana'));
  36. Items.Add(CreateItem(TCustomItem, 'custom'));
  37.  
  38. for Item in Items do
  39. WriteLn(Item.Name);
  40. finally
  41. Items.Free();
  42. end;
  43. end.
  44.  
Success #stdin #stdout 0s 5480KB
stdin
Standard input is empty
stdout
orange
banana
custom