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.

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2021-11-25
  • 2021-07-05
  • 2021-04-06
  • 2021-07-11
  • 2021-11-30
猜你喜欢
  • 2021-08-23
  • 2021-12-11
  • 2021-10-01
  • 2021-08-29
  • 2021-09-22
  • 2021-11-26
  • 2022-12-23
相关资源
相似解决方案