4

我创建了一个类

  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'

在不丢失应用程序功能的情况下消除此警告需要做什么?

4

2 回答 2

8

Use the reintroduce reserved word to indicate the compiler you want to intentionally hide the base class constructor in your class:

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

That way, no warning is shown.

That said, you have to re-think hiding the TComponent.Create constructor. It is a bad idea since the default TComponent.Constructor is called by Delphi to create your component instances at run-time when added to forms/data modules at design time.

TComponent makes the constructor virtual to allow you execute custom code during that process, but you have to stick with the Create firm passing you only the owner, and let the streaming mechanism to deal with the stored values for properties after the creation is complete.

Your component have to support being "unconfigured" if that's the case, or setup default values for it's properties in this universal constructor.

You can provide more constructors, with different names, to allow you create instances at run-time from code passing values for the different properties for your convenience.

于 2012-12-19T14:39:34.003 回答
2

如果您为构造函数使用不同的名称,它可能会更好 - 并且更具可读性,例如

constructor FormInfo.CreateWithSize(AOwner: TComponent; leftvalue, topvalue, heightvalue, widthvalue: integer);
于 2015-02-02T22:42:16.040 回答