Inherited 的功能是什么? ( 积分: 10 )

  • 主题发起人 主题发起人 sypoh
  • 开始时间 开始时间
S

sypoh

Unregistered / Unconfirmed
GUEST, unregistred user!
Inherited 的功能是什么?
 
Inherited 的功能是什么?
 

继承

给个例子先:
unit Unit1;

interface

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

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

TBaseClass=class(Tobject)
procedure DoSomething1
virtual;
procedure DoSomething2
virtual;
end;

TSubClass=class(TBaseClass)
procedure DoSomething1
override;
procedure DoSOmething2
override;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TBaseClass }

procedure TBaseClass.DoSomething1;
begin
showmessage('TBaseClass.DoSomething1');
end;

procedure TBaseClass.DoSomething2;
begin
showmessage('TBaseClass.DoSomething2');
end;

{ TSubClass }

procedure TSubClass.DoSomething1;
begin
inherited;
showmessage('TSubClass.DoSomething1');
end;

procedure TSubClass.DoSOmething2;
begin
//inherited
呵呵,在此做个比较吗 :-)
showmessage('TSubClass.DoSOmething2');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
sample:TSubclass;
begin
sample:=Tsubclass.Create;
sample.DoSomething1;
sample.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
sample:TSubclass;
begin
sample:=Tsubclass.Create;
sample.DoSomething2;
sample.Free;
end;

end.
 
1.消息 继续向底层派发消息,直到Inherited不被调用,或者Window默认的消息处理函数
2.继承 调用父类/祖父类/..中由本方法覆盖的函数。

调用形式:
1. Inherited;//
2. Inherited(para1, prara2, ...);
3. Result:= Inherited (para1, para2...);
 
[blue]Inherited指调用父类中的方法。[/blue]
 
The reserved word inherited plays a special role in implementing polymorphic behavior. It can occur in method definitions, with or without an identifier after it.

If inherited is followed by the name of a member, it represents a normal method call or reference to a property or field--except that the search for the referenced member begins with the immediate ancestor of the enclosing method's class. For example, when

inherited Create(...);

occurs in the definition of a method, it calls the inherited Create.

When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method or, if the enclosing method is a message handler, to the inherited message handler for the same message. In this case, inherited takes no explicit parameters, but passes to the inherited method the same parameters with which the enclosing method was called. For example,

inherited;

occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters that were passed to the descendant.
 
[gold]Inherited指调用父类中的方法。[/gold]
 
base.XXXXXX[:D]
 
没有参数的是调用父类中的同名方法
指定了方法和参数的是调用父类中的指定方法

construstor TClass.Create(AObject: TObject)
begin
Self.FObject:=AObject;
inherited Create;
end;
 
在继承类中初始化方法。
 
后退
顶部