Delphi TList <T>泛型

hik*_*ari 4 delphi generics tlist

如果有可能,有人可以向我解释,或者我完全不了解这个Delphi功能.

假设我有一个类,我创建了一些类,然后将它们添加到ObjectList中.通常我会这样做:

Type TMyClass = class(TObject)
  stuff: string;
..
end;

Var things: TObjectList;

things := TObjectList.Create;
things.Add(TMyClass.Create);

// now I want to access stuff, so I need to typecast the class
TMyClass(things[0]).stuff..
Run Code Online (Sandbox Code Playgroud)

所以现在我的问题是,是否有可能以我可以做的方式声明列表.. thing [0] .stuff并且仍然可以访问常见的TObjectList功能,如.sort .indexof等.?(没有为此创建一个特殊的类来模拟objectlist)

Bru*_*Gee 6

您正在使用System.Contnrs中TObjectList,它管理指针列表.

您想从System.Generics.Collections中获取TObjectList.我知道,使用相同的名称可能会有点混乱.

Type TMyClass = class(TObject)
  stuff: string;
..
end;

Var things: TObjectList<TMyCLass>;

things := TObjectList<TMyCLass>.Create;
things.Add(TMyClass.Create);

things[0].stuff..
Run Code Online (Sandbox Code Playgroud)