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.

posted on 2011-01-24 21:04  巅枫  阅读(251)  评论(0编辑  收藏  举报