I have a TList of an interface
IImage = interface
// another methods...
procedure Draw;
end;
that draw different stuff in a Canvas.
and a parent class
TCustomImage = class(TInterfacedObject, IImage)
procedure Draw; virtual; abstract;
end;
and a couple of child classes
TSomeShape = class(TCustomImage, IImage)
procedure Draw(aCanvas:TCanvas); reintroduce; overload;
end;
TSomeAnotherShape = class(TCustomImage, IImage)
procedure Draw(aCanvas:TCanvas); reintroduce; overload;
end;
I would like to code as below:
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
I want to use the list.Items[I].draw()
with the correct classes (TSomeShape
or TSomeAnotherShape
). But now, I see that it is impossible with this IImage
interface. I had a method Draw(Canvas:TCanvas)
in the IImage but thought that it is acceptably to reintroduce method in classes.
Thanks, I`ll work on my code. :)