unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBGrids, ComObj, StdCtrls, Grids;type  // 声明一个函数指针类型
  TUpAniInfoProc = procedure(const sInfo: string) of object;  TForm1 = class(TForm)
    btn1: TButton;
    DBGrid1: TDBGrid;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpAniInforpoc(const sInfo:string);
  end;function DBGridToExcel(dgrSource: TDBGrid;
  UpAniInfo: TUpAniInfoProc = nil): Integer;var
  Form1: TForm1;implementation{$R *.dfm}function DBGridToExcel(dgrSource: TDBGrid; UpAniInfo: TUpAniInfoProc): Integer;
begin
  // do something...
end;{ TForm1 }//   TUpAniInfoProc = procedure(const sInfo: string) of object;
//  of object 表示这个函数是某一个对象的成员函数,这个对象没有交代,未可知,暂用TForm
// 如此,参数 const sInfo: string 也是无意义的
procedure TForm1.UpAniInforpoc(const sInfo: string);
begin
  // do something...
end;// 调用
procedure TForm1.btn1Click(Sender: TObject);
begin
   DBGridToExcel(DBGrid1, Self{Form1}.UpAniInforpoc);
end;end.

解决方案 »

  1.   

    sololie 大哥,Self{Form1}.可以这样用么?
      

  2.   

    不写self也可以,这里只是要强调 of object 的这个 object 是 self(form1)。
    不过看LZ的代码,设计好像有问题,而且貌似也基本没有OOP的概念。
      

  3.   

    2楼正解,补充一点
    TUpAniInfoProc = procedure (const sInfo: string) of object;
    这是一个过程类型,有“of object”的是方法指针,具体实现是要参考该对象的实现过程,意思是该对象中找一个名称相同(UpAniInfoProc)参数相同的方法。如果不加“of object”的话,就在全局找。
    你在调用DBGridToExcel函数的时候,对TUpAniInfoProc类型参数的指定要看你是用的那个类了,比如A类实现了这个过程,B类也实现了这个过程,用哪个类就掉用哪个类的过程。
    2楼说的参数无意义不对吧
    个人认为是这样的,这种方法很少用到,只是在看delphi的源码的时候经常见到