Using Generic containers in Delphi XE - always?

后端 未结 7 1214
星月不相逢
星月不相逢 2021-02-03 10:45

Generic containers can be a time saver when having a item, and a strongly typed list of those items. It saves the repetitive coding of creating a new class with perhaps a TList

7条回答
  •  迷失自我
    2021-02-03 11:22

    In the spirit of Cosmin's answer, essentially a response to Deltic's answer, here is how to fix Deltic's code:

    type
      TAnimal = class
      end;
    
      TDog = class(TAnimal)
      end;
    
      TAnimalList = class(TList)
        procedure Feed;
      end;
      TDogList = TAnimalList;
    

    Now you can write:

    var
      Dogs: TDogList;
    ...
      Dogs.Feed;
    

提交回复
热议问题