





unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{自定义父类}
TParent = class
protected
function MyFun(i:Integer):Integer; dynamic; //动态方法
procedure MyProc; virtual; //虚拟方法
end;
{自定义子类}
TChild = class(TParent)
protected
function MyFun(i:Integer):Integer; override; //覆盖父类的同名动态方法
procedure MyProc; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TParent }
function TParent.MyFun(i: Integer): Integer; //功能,对函数参数+1
begin
inc(i);
Result:=i;
end;
procedure TParent.MyProc; //功能,显示一个字符串'TParent'
begin
ShowMessage('TParent');
end;
{ TChild }
function TChild.MyFun(i: Integer): Integer; //功能,调用父类的MyFun方法后再+1
begin
i:=inherited MyFun(i);
inc(i);
Result:=i;
end;
procedure TChild.MyProc; //功能,调用父类的MyProc方法后再显示另一个字符串'TChild'
begin
inherited;
ShowMessage('TChild');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
p:TParent;
c:TChild;
begin
p:=TParent.Create;
c:=TChild.Create;
p.MyProc; //Parent
c.MyProc; //Parent; TChild
ShowMessage(IntToStr(p.MyFun(2))); //3
ShowMessage(IntToStr(c.MyFun(2))); //4
p.Free;
c.Free;
end;
end.
浙公网安备 33010602011771号