我真的很挣扎,无法在网上找到任何关于它的信息 - 或者我可能不明白网上写的是什么。我正在尝试创建一个堆栈并使用ibut我甚至无法创建它。
我在一个过程中创建它,我希望堆栈包含整数。我试着跟随,我只是得到下面的红线(两者都在程序的var部分):
genericstack:TObjectStack; 以及 genericstack:T堆;
并且都没有设置堆栈。我刚下面有一条红线。
答案 0 :(得分:0)
您可以在官方网站上查看示例代码:
Generics Collections TObjectStack
有用的一个: How to store a String in a TObjectStack
并且代码段可以作为您的答案:
{
This example demonstrates the usage of the generic TObjectStack class.
}
type
{ Declare a new object type }
TNewObject = class
private
FName: String;
public
constructor Create(const AName: String);
destructor Destroy(); override;
end;
{ TNewObject }
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 TForm3.Button1Click(Sender: TObject);
var
Stack: TObjectStack<TNewObject>;
Button: TButton;
begin
{ Create a new stack }
Stack := TObjectStack<TNewObject>.Create();
{ Set the OwnsObjects to true - the stack will free them automatically }
Stack.OwnsObjects := true;
{ Push some items up the stack }
Stack.Push(TNewObject.Create('One'));
Stack.Push(TNewObject.Create('Two'));
Stack.Push(TNewObject.Create('Three'));
{
Pop an instance of TNewObject class. Destructor
show be called because we have set the OwnsObjects
to true!
}
Stack.Pop();
{ Destroy the stack completely - more messageboxes will be shown }
Stack.Free;
end;