


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; virtual; abstract;
//抽象方法(纯虚方法),只有定义没有实现,一个类包含一个即成抽象类,抽象类不能直接创建对象。
end;
{子类}
TChild = class(TParent)
protected
function MyFun(i:Integer):Integer; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TChild }
function TChild.MyFun(i: Integer): Integer; //功能,对函数参数+1
begin
inc(i);
Result:=i;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
p:TParent;
c:TChild;
begin
p:=TChild.Create; //抽象类只能通过其子类创建对象
c:=TChild.Create;
ShowMessage(IntToStr(p.MyFun(2))); //3
ShowMessage(IntToStr(c.MyFun(2))); //3
p.Free;
c.Free;
end;
end.
浙公网安备 33010602011771号