【发布时间】:2014-02-14 03:23:54
【问题描述】:
我是接口的新手,并且一直在我的最新项目中尝试它们。我有这个(简化的)界面:
IBoardShape = interface(IInterface)
function GetColor: integer;
procedure SetColor(const aColor: integer);
property Color: integer read GetColor write SetColor;
end;
几个类是这样继承的:
TGameRectangle = class(TRectangle, IBoardShape)
private
FColor: integer;
function GetColor: integer;
procedure SetColor(const aColor: integer);
property Color: integer read GetColor write SetColor;
end;
我有一个工厂,用于在自己的数据模块中创建形状。
function TdmShapeManager.CreateRect(aParent: TLayout): IBoardShape;
var
lRect: TGameRectangle;
begin
lRect := TGameRectangle.Create(self);
lRect.Parent := aParent;
lRect.Align := TAlignLayout.alClient;
result := lRect as IBoardShape;
end;
结果被添加到TList<IBoardShape>。
所有这些都运行良好,直到我开始尝试在运行时删除形状。我发现TList[I] := nil 没有释放该项目,控件只会停留在屏幕上。所以,从这里我不确定。我发现我可以将对象转换为 TShape 并在其上调用 .Free,但这听起来不对。 (我试过了,它可以工作,但它会导致其他问题 - 尝试释放接口时 Delphi 代码中的错误。)
我不确定的一件事:由于我的TGameRectangle 不是来自TInterfacedObject,我应该自己做引用计数吗?还是我误解了TInterfacedObject 的用途?
【问题讨论】: