tgeneric1.pp 793 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. {$mode objfpc}
  2. type
  3. generic TList<_T>=class(TObject)
  4. data : _T;
  5. procedure Add(item: _T);
  6. end;
  7. procedure TList.Add(item: _T);
  8. begin
  9. data:=item;
  10. end;
  11. type
  12. TMyIntList = specialize TList<integer>;
  13. TMyIntList2 = specialize TList<integer>;
  14. TMyStringList = specialize TList<string>;
  15. var
  16. ilist : TMyIntList;
  17. ilist2 : TMyIntList2;
  18. slist : TMyStringList;
  19. someInt : integer;
  20. begin
  21. someInt:=10;
  22. ilist := TMyIntList.Create;
  23. ilist.Add(someInt);
  24. writeln(ilist.data);
  25. if ilist.data<>10 then
  26. halt(1);
  27. someInt:=20;
  28. ilist2 := TMyIntList2.Create;
  29. ilist2.Add(someInt);
  30. writeln(ilist2.data);
  31. if ilist2.data<>20 then
  32. halt(1);
  33. slist := TMyStringList.Create;
  34. slist.Add('Test');
  35. writeln(slist.data);
  36. if slist.data<>'Test' then
  37. halt(1);
  38. end.