type
myprocedure1=procedure (i:integer;a:string);
myprocedure2=procedure (i:integer;a:string) of object;有什么不同?
请分别阐述之...

解决方案 »

  1.   

    第一個是定義了一個過程的指針,myprocedure1是一個過程的地址。
    第二個是定義了一個實例對象的方法,myprocedure2實際上包含了一對地址,第一部分是這個過程或函數的地址,第二部分是引用的對象的地址。
      

  2.   

    例子是Delphi的說明:
    第一種情況:
    type
      TIntegerFunction = function: Integer;
      TProcedure = procedure;
      TStrProc = procedure(const S: string);
      TMathFunc = function(X: Double): Double;
    var
      F: TIntegerFunction;             { F is a parameterless function that returns an integer }
      Proc: TProcedure;                { Proc is a parameterless procedure }
      SP: TStrProc;                    { SP is a procedure that takes a string parameter }  M: TMathFunc;                    { M is a function that takes a Double (real) parameter 
                                           and returns a Double }
    第二種情況:
    type
      TNotifyEvent = procedure(Sender: TObject) of object;
      TMainForm = class(TForm)
        procedure ButtonClick(Sender: TObject);
        ...
      end;
    var
      MainForm: TMainForm;
      OnClick: TNotifyEventwe could make the following assignment.OnClick := MainForm.ButtonClick;
      

  3.   

    http://topic.csdn.net/t/20060912/09/5015133.html这里有大牛的比较详细的说明。
      

  4.   

    不行的,上面的说法均不能让我信服,因为有的说type myprocedure2=procedure (i:integer;a:string) of object是类的方法,我感觉不是,因为类的方法是这样定义的:
    class procedure myprocname(i:integer;a:string);
    类的方法与myprocedure2=procedure (i:integer;a:string) of object是不同的,可是你们又说不出他们的区别啊,我没法给分.
      

  5.   

    class procedure myprocname(i:integer;a:string);一般叫类方法,就是C++静态方法。其实这种叫法不准确,可能是因为方法前有个class的缘故。
    myprocedure2=procedure (i:integer;a:string) of object是定义一个属于类的方法类型,其实就是C++的函数指针类型。
      

  6.   

    前者是定义一个方法类型;后者是定义一个属于类的方法类型。二者的不同点在于后者有一个隐含的Self参数。
      

  7.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure form1Test(a : string);
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      procedure pubTest(a : string);implementationprocedure pubTest(a : string);
    begin
        showmessage('pubTest:' + a);
    end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    type
      Taaa = procedure (a: string) of object;
      //Taaa = procedure (a: string)
    var
        ap: Taaa;
    begin
        ap := self.form1Test;
        //ap := pubTest;
        ap('aaa');
    end;procedure TForm1.form1Test(a: string);
    begin
        showmessage('form1Test:' + a);
    end;end.
    试一下两个注释的地方,就明白了
      

  8.   

    补充一下注释说明
    procedure TForm1.Button1Click(Sender: TObject);
    type
      Taaa = procedure (a: string) of object; //只能用当前类的方法,如:self.方法名
      //Taaa = procedure (a: string)    //只能用公共方法
    var
        ap: Taaa;
    begin
        ap := self.form1Test; //只能应用在 procedure (a: string) of object
        //ap := pubTest;    //只能应用在procedure (a: string)
        ap('aaa');
    end;