SerialForms.pas(17):W1010方法'Create'隐藏基类型'TComponent'的虚方法

use*_*132 4 delphi constructor warnings build delphi-xe2

我创建了一个类

  FormInfo = class (TComponent)
  private
    FLeftValue : Integer;
    FTopValue : Integer;
    FHeightValue : Integer;
    FWidthValue : Integer;
  public
    constructor Create(
      AOwner : TComponent;
      leftvalue : integer;
      topvalue : integer;
      heightvalue : integer;
      widthvalue : integer);
  protected
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
    function  GetChildOwner: TComponent; override;
    //procedure SetParentComponent(Value : TComponent); override;
  published
    property LeftValue : Integer read FLeftValue write FLeftValue;
    property TopValue : Integer read FTopValue write FTopValue;
    property HeightValue : Integer read FHeightValue write FHeightValue;
    property WidthValue : Integer read FWidthValue write FWidthValue;
  end;
Run Code Online (Sandbox Code Playgroud)

它进一步用于表单序列化.Create方法具有以下实现

constructor FormInfo.Create(AOwner: TComponent; leftvalue, topvalue, heightvalue,
  widthvalue: integer);
begin
  inherited Create(AOwner);

  FLeftValue := leftvalue;
  FTopValue := topvalue;
  FHeightValue := heightvalue;
  FWidthValue := widthvalue;
end;
Run Code Online (Sandbox Code Playgroud)

由于组装,我收到警告

[dcc32 Warning] SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'
Run Code Online (Sandbox Code Playgroud)

在不丢失应用程序功能的情况下,有必要做出什么来摆脱这个警告?

jac*_*ate 8

使用reintroduce保留字来指示要在类中有意隐藏基类构造函数的编译器:

TMyClass = class (TComponent)
public
  constructor Create(AOwner: TComponent; MyParam: Integer; Other: Boolean); reintroduce;
Run Code Online (Sandbox Code Playgroud)

这样,没有显示警告.

也就是说,你必须重新考虑隐藏TComponent.Create构造函数.这是一个坏主意,因为在设计时添加到表单/数据模块时,Delphi调用默认的TComponent.Constructor来在运行时创建组件实例.

TComponent使构造函数虚拟化以允许您在该过程中执行自定义代码,但您必须坚持使用Create公司仅传递所有者,并让流机制在创建完成后处理属性的存储值.

如果是这种情况,您的组件必须支持"取消配置",或者在此通用构造函数中为其属性设置默认值.

您可以提供更多具有不同名称的构造函数,以允许您在运行时通过代码传递不同属性的值来创建实例,以方便您使用.