delphi - SerialForms.pas(17) : W1010 Method 'Create' hides virtual method of base type 'TComponent'

标签 delphi build constructor warnings 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;

进一步用于表单序列化。 Create方法有如下实现

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

  FLeftValue := leftvalue;
  FTopValue := topvalue;
  FHeightValue := heightvalue;
  FWidthValue := widthvalue;
end;

由于组装,我收到警告

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

需要做什么才能消除此警告而不损失应用程序的功能?

最佳答案

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

TMyClass = class (TComponent)
public
  constructor Create(AOwner: TComponent; MyParam: Integer; Other: Boolean); reintroduce;

这样,就不会显示任何警告。

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

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

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

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

关于delphi - SerialForms.pas(17) : W1010 Method 'Create' hides virtual method of base type 'TComponent' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13954582/

相关文章:

delphi - 何时以及为何在 DirectShow 过滤器中使用 CoTaskMemAlloc()?

javascript - 在特定的 webpack 构建( Angular 2)的情况下,如何将 CSS 文件中的 Assets url() 替换为第三方域?

c++ - 介子项目 : can I have part of the source located elsewhere?

c++ - 如何将用户输入存储在与默认构造函数中的变量初始化值不同的变量中?

delphi - 无法通过Delphi替换word应用程序中的页脚文本

delphi 7 语句的 C# 语法

java - 从另一个调用构造函数 - this 关键字用法

C++ 初始化类成员构造函数

delphi - 如何在其他程序中访问和读取 Label 的标题

build - 自动docker构建运行错误: Unable to find image