delphi - delphi tlist对象方法调用

标签 delphi tlist

所有,

在Delphi中,我创建了一个名为T_Test的简单类(请参见下文)。

T_Test = class(TObject)

private

 F_Int : Integer;

public

  constructor Create(inInt: Integer);
  destructor Destroy; override;

  property Int: Integer read F_Int write F_Int;

  function showInt : String;


end;

constructor T_Test.Create(inInt: Integer);

 begin

  F_Int := inInt;

 end;


destructor T_Test.Destroy;

 begin

  self.Free;

 end;


function T_Test.showInt : String;

 var outputLine : String;


 begin

   result := IntToStr(Int);
   outputLine := result;
   Form1.Memo1.Lines.Add(outputLine);

 end;


然后我有一个过程,要在其中创建T_Test对象的TList并调用
showInt方法函数在它们上面。

我这样尝试过:

procedure testTlist;

 var

  a, b: T_Test;
  i : Integer;

 begin

  a := T_Test.Create(5);
  b := T_Test.Create(10);

 listTest := TList.Create;

 listTest.Add(a);
 listTest.Add(b);


 listTest[i].showInt;


end;


但是我一直收到一个提示,说我必须在记录上使用Record,Object或Class Type。
“ listTest [i] .showInt”的调用

有人知道如何调用此方法吗?

最佳答案

将listTest [i]指针转换回T_Test,然后调用其方法:

T_Test(listTest[i]).showInt;


或者,如果可用,请使用模板化的TObjectList类直接存储T_Test实例。

关于delphi - delphi tlist对象方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017188/

相关文章:

c++ - 我可以将 C++ 项目转换为 Delphi 吗?

algorithm - 在 Delphi 中解决 TSP 的蛮力算法

delphi - 是否有任何实现基本IDE结构的Delphi开源项目?

c++ - Delphi typcast tlist items C++ 方法

delphi - EListError 列表索引越界 (0) - TForm 项目

delphi - 如何销毁运行时定义的 TClientDataSet TFields?

c++ - 显示 Delphi 和 C++ 源代码

arrays - 包含动态数组的通用记录列表

delphi - 为什么 Delphi 7 不允许扩展类型的 TList

Delphi:TList 创建,内存错误