unit Unit1;

interface

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

type
  {父类}
  TBase = class
    procedure proc; virtual; abstract; {抽象方法}
  end;

  {子类}
  TChild = class(TBase)
    procedure proc; override; {在子类中覆盖、实现方法}
  end;


  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TChild }
//方法实现
procedure TChild.proc;
begin
  ShowMessage('IsChild');
end;


//AbstractErrorProc 将要调用的过程
procedure err;
begin
  ShowMessage('Err...');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  B: TBase;
  C: TChild;
begin
  {AbstractErrorProc 是抽象错误处理的指针, 对应一个无参数的外部过程}
  AbstractErrorProc := Addr(err);

  {在子类中调用方法, Ok}
  C := TChild.Create;
  C.proc; {IsChild}
  C.Free;

  {父类通过子类实现后调用方法, Ok}
  B := TChild.Create;
  B.proc; {IsChild}
  B.Free;

  {父类自实现后调用方法, 将会调用 AbstractErrorProc 指定的错误过程; 因为父类中的方法还是抽象的}
  B := TBase.Create;
  B.proc; {Err...}
  B.Free;
end;

end.

posted on 2007-12-31 18:16  万一  阅读(4030)  评论(2编辑  收藏  举报