覆盖一个类的Create的方法(扩展了Create方法的参数),编译是有警告是否正常?如何避免?例如有如下申明,我的Create的方法实现是否正确?type
TMyLabel = class(TLabel)
private
{ Private declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent;const ICaption:String);
//destructor Destroy; override;
end;implementationconstructor TWord.Create(AOwner: TComponent; const ICaption: String);
begin
//inherited TLabel.Create(AOwner);
TLabel.Create(AOwner);
end;end.
TMyLabel = class(TLabel)
private
{ Private declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent;const ICaption:String);
//destructor Destroy; override;
end;implementationconstructor TWord.Create(AOwner: TComponent; const ICaption: String);
begin
//inherited TLabel.Create(AOwner);
TLabel.Create(AOwner);
end;end.
好像就没有警告了//这个好像不对,改为
constructor TWord.Create(AOwner: TComponent; const ICaption: String);
begin
inherited Create(AOwner);
...
end;
你只要重新定义就可以了呀
比如说下面这个例子
Type
TMyFamily=Class
Private
FMyFatherName:String;
FMyMotherName:String;
...
Public
Constructor Create(StrFatherName,StrMontherName:String);
...
End;创建对象
MyFamilyObject:=TMyFamily.Create('zhang','Li');
procedure Test(I: Integer); overload; virtual;
end;
T2 = class(T1)
procedure Test(S: string); reintroduce; overload;
end;
你试试
reintroduce可覆盖继承的虚方法--Use reintroduce when you want to hide an inherited virtual method with a new one.
mastering delphi 6上有一个这方面的例子的,
unit ReintrF;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TMyClass = class
procedure One; overload; virtual;
procedure One (I: Integer); overload;
end; TMySubClass = class (TMyClass)
procedure One; overload; override;
procedure One (S: string); reintroduce; overload;
end; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}{ MyClass }procedure TMyClass.One;
begin
ShowMessage ('MyClass.One');
end;procedure TMyClass.One(I: Integer);
begin
ShowMessage ('Integer: ' + IntToStr (I));
end;{ MySubClass }procedure TMySubClass.One;
begin
ShowMessage ('MySubClass.One');
end;procedure TMySubClass.One(S: string);
begin
ShowMessage ('String: ' + S);
end;procedure TForm1.Button1Click(Sender: TObject);
var
Obj: TMySubClass;
begin
Obj := TMySubClass.Create;
Obj.One;
Obj.One (10);
Obj.One ('Hello');
Obj.Free;
end;end.