- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- procedure xx1;
- procedure xx2;
- procedure xx3;
- private
- { Private declarations }
- FExampleMethod : TNotifyEvent;
- //TNotifyEvent是一个Delphi最基本的事件类型,多用于触发事件,也可以自己定义一个函数类型作为事件触发
- public
- { Public declarations }
- procedure GetFcun(MethodName:string);
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.GetFcun(MethodName: string);
- begin
- @FExampleMethod := MethodAddress(MethodName);
- //MethodAddress(MethodName)返回 MethodName的地址
- //MethodAddress只支持published声明的方法
- if Assigned(@FExampleMethod) then //用Assigned测试函数或过程变量是否为空
- FExampleMethod(nil);
- end;
- procedure TForm1.xx1;
- begin
- showmessage('xx1');
- end;
- procedure TForm1.xx2;
- begin
- showmessage('xx2');
- end;
- procedure TForm1.xx3;
- begin
- showmessage('xx3');
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- GetFcun(Edit1.Text);//调用GetFcun执行需要的方法
- end;
- end.
动态调用需要的方法
最新推荐文章于 2018-03-04 09:42:49 发布