请问一个派生类重载父类的函数时是否需要写override?(50分)

  • 主题发起人 主题发起人 nibble
  • 开始时间 开始时间
N

nibble

Unregistered / Unconfirmed
GUEST, unregistred user!
我看到有些函数通常都有override,如Destroy,而有些函数则没有,请问写与不写有什么
区别?
 
type
TFirstComponent = class(TCustomControl)
procedure Move
{ static method }
procedure Flash
virtual
{ virtual method }
procedure Beep
dynamic
{ dynamic virtual method }
end;
TSecondComponent = class(TFirstComponent)
procedure Move
{ declares new method }
procedure Flash
override
{ overrides inherited method }
procedure Beep
override
{ overrides inherited method }
end;

看看Delphi的帮助就知道了如果要重载父类的函数,就必须写
如果不写,相当于重新申明(定义)一个函数
 
OVERRIDE是覆盖。
OVERLOAD是重载!!!
不一样的。。。。

要重载父类的涵数用OVERLOAD。。
DESTROY 是一个特殊的涵数,通常再OVERRIDE后还要INHERITED。。
嗯,,理解不够深刻,可能讲的不清楚。。
 
但是很多函数在函数体中都是这样写的:
function myfunction;
begin
inherited myfunction;
...
end;

那这样的情况下,写与不写override标记有什么差别呢?
 
刚刚写的,仅供参考:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TFirstComponent = class(TCustomControl)
procedure Move
{ static method }
procedure Flash
virtual
{ virtual method }
procedure Beep
dynamic
{ dynamic virtual method }
end;

TSecondComponent = class(TFirstComponent)
procedure Move
{ declares new method }
procedure Flash
override
{ overrides inherited method }
procedure Beep
override
{ overrides inherited method }
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TFirstComponent }

procedure TFirstComponent.Beep;
begin
ShowMessage('1Beep');
end;

procedure TFirstComponent.Flash;
begin
ShowMessage('1Flash');
end;

procedure TFirstComponent.Move;
begin
ShowMessage('1Move');
end;

{ TSecondComponent }

procedure TSecondComponent.Beep;
begin
inherited;
ShowMessage('2Beep');
end;

procedure TSecondComponent.Flash;
begin
inherited;
ShowMessage('2Beep');
end;

procedure TSecondComponent.Move;
begin
ShowMessage('2Beep');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
a: TSecondComponent;
begin
a := TSecondComponent.Create(Self);
a.Beep;
a.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
a: TSecondComponent;
begin
a := TSecondComponent.Create(Self);
a.Move;
a.Free;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
a: TSecondComponent;
begin
a := TSecondComponent.Create(Self);
a.Flash;
a.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := 'Call Beep';
Button1.Caption := 'Call Move';
Button1.Caption := 'Call Flash';
end;

end.
 
>>nibble
你的提法我从没见过哦。。:)。。从作用来上看是继承原先的涵数体(并且可以传参数)
再加上interited后面的涵数部分。
还往有人可以点明。。:)
 
to another_benlei:
如果你的代码中TSecondComponent.Move改成下面的形式
procedure TSecondComponent.Move;
begin
inherited;
ShowMessage('2Beep');
end;
而其它都不变,那么当Component = TSecondComponent.Create时,Move与Beep的效果不是
相同了吗?
 
“但是很多函数在函数体中都是这样写的:
function myfunction;
begin
inherited myfunction;//先执行父类的同名函数定义的操作;
...//再定义子类的附加操作;
end;
 
后退
顶部