请问delphi中inherited是怎么用的阿?

tiankong 2002-04-24 09:30:38
代码中有的是inherited;有的是inherited后面还跟上函数或过程,有什么区别阿?
...全文
3561 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
codelover 2002-04-24
  • 打赏
  • 举报
回复
看看这个例子,对理解有帮助:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
THuman=class //基类
public
procedure showinfo1;virtual;
procedure showinfo2;virtual;
end;
TStudent=class(Thuman) //派生类
public
procedure showinfo1;override;// 虚拟继承
procedure showinfo2;override;
end;
TWorker=Class(THuman) //派生类
public
procedure showinfo1;override;
procedure showinfo2;override;
end;
var
Form1: TForm1;

implementation

{$R *.dfm}
procedure THuman.showinfo1 ;
begin
ShowMessage('THuman Showinfo1');
end;
procedure THuman.showinfo2 ;
begin
ShowMessage('THuman Showinfo2');
end;
procedure TStudent.showinfo1 ;
begin
ShowMessage('TStudent Showinfo1');
inherited showinfo2; //调用父类中不同名的成员函数
end;
procedure TStudent.showinfo2 ;
begin
ShowMessage('TStudent Showinfo2');
end;
procedure TWorker.showinfo1 ;
begin
ShowMessage('TWorker Showinfo1');
  inherited; //调用父类中的同名函数
end;
procedure TWorker.showinfo2 ;
begin
ShowMessage('TWorker Showinfo2');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
H:THuman;
begin
H:=THuman.Create ;
h.showinfo1;
h.showinfo2;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
S:TStudent;
begin
s:=TStudent.Create ;
s.showinfo1 ;
s.showinfo2;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
W:TWorker;
begin
w:=Tworker.Create ;
w.showinfo1 ;
w.showinfo2;
end;

end.
Billy_Chen28 2002-04-24
  • 打赏
  • 举报
回复
继承父类的方法,以便子类返回去调用父类的方法
codelover 2002-04-24
  • 打赏
  • 举报
回复
仔细看一下联机帮助: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梕xcept that the search for the referenced member begins with the immediate ancestor of the enclosing method抯 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. 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.
inherited的作用是调用父类的成员函数,不加函数名是调用父类中同名的方法。
CeleronII 2002-04-24
  • 打赏
  • 举报
回复
继承啊,在子类中可以执行父类中同名的方法
Snakeguo 2002-04-24
  • 打赏
  • 举报
回复 1
有inherited就是先继承父类的方法,然后再执行自己的方法
没有就是没有继承罗
forgot2000 2002-04-24
  • 打赏
  • 举报
回复
delphi封装了windows的消息循环,delphi的所有组件都继承自TObject类
inherited是使当前组件不处理的一些消息,向类继承层次中的上一级传递,并最终实现消息被父类默认处理。
tiankong 2002-04-24
  • 打赏
  • 举报
回复
多谢各位啦

5,913

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧