我有一个继承自TCollection的类(让我们称它为"TMyCollection"),我必须从它继承一个新类(我们称它为"TMyItems")。
通常我们在TCollection的构造函数中传递ItemClass类型,但在我的例子中,TMyCollection的构造函数被新的构造函数覆盖,该构造函数不接受ItemClass,它只接受所有者。
如果继承的构造函数不接受ItemClass参数,我需要知道如何更改"TMyItems“中的ItemClass。
致以问候。
发布于 2012-07-11 17:19:17
您仍然可以从子类中调用继承的TCollection.Create,即使它没有相同的签名:
TMyCollectionItem = class(TCollectionItem)
private
FIntProp: Integer;
procedure SetIntProp(const Value: Integer);
public
property IntProp: Integer read FIntProp write SetIntProp;
end;
TMyCollection = class(TCollection)
public
constructor Create(AOwner: TComponent);virtual;
end;
{ TMyCollection }
constructor TMyCollection.Create(AOwner: TComponent);
begin
inherited Create(TMyCollectionItem); // call inherited constructor
end;编辑:
根据原始发帖者的评论,一个“技巧”是将新构造函数标记为重载。因为它是一个重载,所以它不会隐藏对TCollection构造函数的访问。
TMyCollection = class(TCollection)
public
constructor Create(AOwner: TComponent);overload; virtual;
end;
TMyItem = class(TCollectionItem)
private
FInt: Integer;
public
property Int: Integer read FInt;
end;
TMyItems = class(TMyCollection)
public
constructor Create(AOwner: TComponent);override;
end;
implementation
{ TMyCollection }
constructor TMyCollection.Create(AOwner: TComponent);
begin
inherited Create(TCollectionItem);
end;
{ TMyItems }
constructor TMyItems.Create(AOwner: TComponent);
begin
inherited Create(TMyItem);
inherited Create(AOwner); // odd, but valid
end;
end.https://stackoverflow.com/questions/11428972
复制相似问题