本文整理自网络,侵删。
A是基类、B继承A、C继承B、若C函数中有inherited方法,则C中所调用的以及实现的就是全为B中的,同样,B中若有inherited,则B中调用的全为A中的。
inherited Create(AOwner); 和直接写inherited有区别吗
有区别,inherited Create是指定调用父类的Create方法,当然你也可以inherited Destory等等,
如果直接写inherited则默认以本方法名在父类中调用
inherited就是调用祖先类的函数,如果不带参数就是默认调用同名函数
如果带参数则表明子类中的函数个数可能比祖先类要多取其中的几个参数传过去
例如
祖先类有个函数 Create(AName:string);
子类有个函数 Create(AName:string;AComponent:TObject);override;
那么子类的Create函数内就可以这样调用祖先类:
procedure TAClass.Create(AName:string;AComponent:TObject);
begin
Inherited Create(AName);
end;
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
function show:Integer;virtual;
function NumCount:Integer;virtual;
end;
TForm2 = class(TForm1)
public
function NumCount:Integer;override;
end;
TestA = class
public
function TestXX:string;virtual;
end;
TestB = Class(TestA)
public
function TestXX:String;override;
End;
TestC = class
public
function Test:string;
end;
var
Form1: TForm1;
implementation
var
X,Y : Integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(TestC.Create.Test);
end;
{$R *.dfm}
{ TestA }
function TestA.TestXX: string;
begin
Result := 'TestA';
end;
{ TestB }
function TestB.TestXX: String;
begin
Result := inherited TestXX + ','+'TestB'
end;
{ TestC }
function TestC.Test: string;
var
ATest : TestA;
begin
ATest := TestB.Create;
Result := ATest.TestXX;
end;
end.
相关阅读 >>
Delphi 将string 转为 array of ansichar
Delphi idhttp访问datasnap有密码验证的中间件
更多相关阅读请进入《Delphi》频道 >>