我已经被介绍给TObjectList,我想利用它,除了我似乎连sample code from Embarcadero's web site都无法为我工作。这是我的代码:unit Test03Unit1;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;type { Declare a new object type. } TNewObject = class private FName: String; public constructor Create(const AName: String); destructor Destroy(); override; end;{ TNewObject }var Form1: TForm1;implementation{$R *.dfm}constructor TNewObject.Create(const AName: String);begin FName := AName;end;destructor TNewObject.Destroy;begin { Show a message whenever an object is destroyed. } MessageDlg('Object "' + FName + '" was destroyed!', mtInformation, [mbOK], 0); inherited;end;procedure TForm1.Button1Click(Sender: TObject);var List: TObjectList<TNewObject>; Obj: TNewObject;begin { Create a new List. } { The OwnsObjects property is set by default to true -- the list will free the owned objects automatically. } List := TObjectList<TNewObject>.Create(); { Add some items to the List. } List.Add(TNewObject.Create('One')); List.Add(TNewObject.Create('Two')); { Add a new item, but keep the reference. } Obj := TNewObject.Create('Three'); List.Add(Obj); { Remove an instance of the TNewObject class. Destructor is called for the owned objects, because you have set the OwnsObjects to true. } List.Delete(0); List.Extract(Obj); { Destroy the List completely--more message boxes will be shown. } List.Free;end;end.尝试编译此错误时,出现错误[DCC错误] Test03Unit1.pas(51):E2003未声明的标识符:'TObjectList 。第51行是这样写的:List: TObjectList<TNewObject>;我以前从未见过在Pascal语言中使用过,因此对我来说这是全新的领域。用谷歌搜索“ Delphi and ”似乎并不能告诉我我需要知道些什么。从other examples I can find on the internet看来,这是使用它的正确方法。使用Delphi XE2。我究竟做错了什么? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您必须在子句中添加System.Generics.Collections。那就是声明TObjectList<T>的单位。我已经为答案添加了文档链接。当找不到类时,请在文档中查找。这将告诉您您需要使用哪个单位。和TObjectList<T>一样,还有TList<T>。当您希望列表拥有其成员时,使用TObjectList<T>是有意义的。否则,使用TObjectList<T>没有任何好处,您也可以使用TList<T>。除了内置的Delphi通用容器之外,还有许多出色的第三方容器,它们通常是上乘的。例如,Delphi Spring Framework具有很好的容器集合。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-05 22:15