[Delphi]过程Procedure动态调用
过程也可以像通用变量一样声明、调用
procedure HelloWorld; begin ShowMessage('HI'); end; procedure HelloWorld2(a:String); begin ShowMessage('HI'); end; procedure TFormSplash.FormCreate(Sender: TObject); var myproc:Procedure; myproc2:Procedure(a:String); begin myProc:=HelloWorld; if Assigned(myProc) then myProc; myproc2:= HelloWorld2; if Assigned(myproc2) then myproc2('');
也可以这样定义
var
myproc:Procedure=HelloWorld;
myproc2:Procedure(a:String)=HelloWorld2;
参数类型不同或顺序不同会提示
E2009 Incompatible types: ‘Parameter lists differ’
直接赋值 对象中的过程或函数将出错
E2009 Incompatible types: ‘regular procedure and method pointer’
需要在Type里声明类型
type
TMethod =
procedure
of
object
;
TFunc =
function
:
integer
of
object
;
TNotifyEvent =
procedure
(Sender: TObject)
of
object
;