type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
F, G: function: Integer;
function SomeFunction: Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}function TForm1.SomeFunction: Integer;
begin
result:=23;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
I: Integer;
begin
F := SomeFunction;//-----------error
I := F ;
ShowMessage(IntToStr(I));
end;
end.
但是[Error] Unit1.pas(38): Incompatible types: regular procedure and method pointer
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
F, G: function: Integer;
function SomeFunction: Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}function TForm1.SomeFunction: Integer;
begin
result:=23;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
I: Integer;
begin
F := SomeFunction;//-----------error
I := F ;
ShowMessage(IntToStr(I));
end;
end.
但是[Error] Unit1.pas(38): Incompatible types: regular procedure and method pointer
eg:
var ExeProc: procedure
m_Query :TAdoQuery;
....
ExeProc:= m_Query.Open; //error :类型不配套
...
ExeProc:=m_Query.ExecSQL; //error :类型不配套这样我在线程对象里直接用:
procedure TMyThread.Execute;
begin
ExeProc; //据当前函数变量调用实际引用的过程
end;
type
TMyFunction=function(iId:integer):integer;
TMyClass=class
public
TstFunc:TMyFunction ;
end ;
......
Function z(iId:integer):integer ;
begin
...
result:=... ;
end ;
....
begin
.....
TstFunc:=z ;
.....
end ;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyFunction=function(iId:integer):integer;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
TstFunc: TMyFunction ;
Function z(iId:integer):integer ;
public
{ Public declarations }
end;
var
Form1: TForm1;implementation{$R *.DFM}
Function TForm1.z(iId:integer):integer ;
begin
result:=iId+10;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
TstFunc:=z; ////////////////////错误
end;end.
[Error] Unit1.pas(35): Incompatible types: regular procedure and method pointer
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
上面代码中,把TForm1定义中的“Function z”那行去掉,“Function TForm1.z(iId:integer):integer ;"中的“TForm1.”也去掉就行。
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyFunction=function(iId:integer):integer;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
TstFunc: TMyFunction ;
public
{ Public declarations }
end;
var
Form1: TForm1;implementation{$R *.DFM}
function z(iId:integer):integer ;
begin
result:=iId+10;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
TstFunc:=z;
showMessage(TstFunc(6));
end;end.
type
TMyFunction=function(iId:integer):integer of object;