type
myprocedure1=procedure (i:integer;a:string);
myprocedure2=procedure (i:integer;a:string) of object;有什么不同?
请分别阐述之...
myprocedure1=procedure (i:integer;a:string);
myprocedure2=procedure (i:integer;a:string) of object;有什么不同?
请分别阐述之...
第二個是定義了一個實例對象的方法,myprocedure2實際上包含了一對地址,第一部分是這個過程或函數的地址,第二部分是引用的對象的地址。
第一種情況:
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;
class procedure myprocname(i:integer;a:string);
类的方法与myprocedure2=procedure (i:integer;a:string) of object是不同的,可是你们又说不出他们的区别啊,我没法给分.
myprocedure2=procedure (i:integer;a:string) of object是定义一个属于类的方法类型,其实就是C++的函数指针类型。
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.
试一下两个注释的地方,就明白了
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;