首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改从TCollection继承的类的ItemClass

如何更改从TCollection继承的类的ItemClass
EN

Stack Overflow用户
提问于 2012-07-11 16:55:55
回答 1查看 358关注 0票数 1

我有一个继承自TCollection的类(让我们称它为"TMyCollection"),我必须从它继承一个新类(我们称它为"TMyItems")。

通常我们在TCollection的构造函数中传递ItemClass类型,但在我的例子中,TMyCollection的构造函数被新的构造函数覆盖,该构造函数不接受ItemClass,它只接受所有者。

如果继承的构造函数不接受ItemClass参数,我需要知道如何更改"TMyItems“中的ItemClass。

致以问候。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-11 17:19:17

您仍然可以从子类中调用继承的TCollection.Create,即使它没有相同的签名:

代码语言:javascript
复制
  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构造函数的访问。

代码语言:javascript
复制
  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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11428972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档