我在Delphi中创建了一个组件,并且试图覆盖create方法,如下所示:
unit DefButton;
interface
uses
Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls,
System.SysUtils, System.Classes, Vcl.Controls, Vcl.Graphics, Vcl.Forms;
type
TDefButton = class(TPanel)
private
{ Private declarations }
protected
constructor Create(AOwner: TComponent); override;
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
constructor TDefButton.Create(AOwner: TComponent);
begin
ShowMessage('test!');
end;
procedure Register;
begin
RegisterComponents('Default Controls', [TDefButton]);
end;
end.
但是当我运行它时,它会显示消息“ test”,但随后会抛出此消息:
然后再次显示该消息,然后抛出该消息:
有什么想法吗?我尝试使用Google搜索,但找不到针对此特定情况的答案...
答案 0 :(得分:4)
您需要在构造函数中调用inherited;
,因此它会调用从祖先类到TObject构造函数的一系列构造函数。
尝试一下:
constructor TDefButton.Create(AOwner: TComponent);
begin
inherited;
ShowMessage('test!');
end;