L
liwei
Unregistered / Unconfirmed
GUEST, unregistred user!
谁能够帮我解释一下OVERLOAD,OVERRIDE,VIRTUAL,REINTRODUCE
这些关键字的差别? 各是干嘛的?
看下面的程序:
units unit1;
interface
uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
ExtCtrls;
type
TMyObject = class(TObject)
public
procedure DoSomething(I: Integer)
VIRTUAL;
// 上面这个VIRTUAL, 改成OVERLOAD也行。
// 那么它们有什么不同?
end;
TAnimal = class(TMyObject)
public
procedure DoSomething(S: string)
reintroduce
overload;
// 上面这个REINTRODUCE如果删掉,程序也不会出错。
// 它究竟有什么用?
end;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Animal: TAnimal;
begin
Animal := TAnimal.Create;
Animal.DoSomething('Cat')
// calls TAnimal.DoSomething
Animal.DoSomething(7)
// calls TMyObject.DoSomething
end;
{ TMyObject }
procedure TMyObject.DoSomething(I: Integer);
begin
Form1.Edit1.Text := IntToStr(i);
end;
{ TAnimal }
procedure TAnimal.DoSomething(S: string);
begin
Form1.Edit2.Text := S;
end;
end.
这些关键字的差别? 各是干嘛的?
看下面的程序:
units unit1;
interface
uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
ExtCtrls;
type
TMyObject = class(TObject)
public
procedure DoSomething(I: Integer)
VIRTUAL;
// 上面这个VIRTUAL, 改成OVERLOAD也行。
// 那么它们有什么不同?
end;
TAnimal = class(TMyObject)
public
procedure DoSomething(S: string)
reintroduce
overload;
// 上面这个REINTRODUCE如果删掉,程序也不会出错。
// 它究竟有什么用?
end;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Animal: TAnimal;
begin
Animal := TAnimal.Create;
Animal.DoSomething('Cat')
// calls TAnimal.DoSomething
Animal.DoSomething(7)
// calls TMyObject.DoSomething
end;
{ TMyObject }
procedure TMyObject.DoSomething(I: Integer);
begin
Form1.Edit1.Text := IntToStr(i);
end;
{ TAnimal }
procedure TAnimal.DoSomething(S: string);
begin
Form1.Edit2.Text := S;
end;
end.