【发布时间】:2014-02-09 19:43:20
【问题描述】:
我有一个接口的 TList
IImage = interface
// another methods...
procedure Draw;
end;
在画布中绘制不同的东西。
和一个父类
TCustomImage = class(TInterfacedObject, IImage)
procedure Draw; virtual; abstract;
end;
还有几个子类
TSomeShape = class(TCustomImage, IImage)
procedure Draw(aCanvas:TCanvas); reintroduce; overload;
end;
TSomeAnotherShape = class(TCustomImage, IImage)
procedure Draw(aCanvas:TCanvas); reintroduce; overload;
end;
我想编码如下:
var
list : TList<IImage>;
shape : IImage;
begin
try
list := TList<IImage>.Create;
list.Add(TSomeShape.Create(atSomePosition));
list.Add(TSomeAnotherShape.Create(atSomePosition));
// or better :)
for shape in list do
shape.Draw(Canvas); // TSomeShape and TSomeAnotherShape respectively.
finally
FreeAndNil(list);
end;
end;
UPD
我想将list.Items[I].draw() 与正确的类一起使用(TSomeShape 或TSomeAnotherShape)。但是现在,我发现这个IImage 接口是不可能的。我在 IImage 中有一个方法 Draw(Canvas:TCanvas),但我认为在类中重新引入方法是可以接受的。
谢谢,我会处理我的代码。 :)
【问题讨论】:
-
您遇到的实际问题是什么?您所做的只是提供一个代码示例,但没有任何解释。
-
如果通过下面的解决方案解决了这个问题,请通过打勾将其标记为已接受的答案。
标签: delphi interface delphi-xe generic-list