动态调用需要的方法

  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls;
  6. type
  7.   TForm1 = class(TForm)
  8.     Edit1: TEdit;
  9.     Button1: TButton;
  10.     procedure Button1Click(Sender: TObject);
  11.     procedure xx1;
  12.     procedure xx2;
  13.     procedure xx3;
  14.   private
  15.     { Private declarations }
  16.     FExampleMethod : TNotifyEvent;
  17.     //TNotifyEvent是一个Delphi最基本的事件类型,多用于触发事件,也可以自己定义一个函数类型作为事件触发
  18.   public
  19.     { Public declarations }
  20.     procedure GetFcun(MethodName:string);
  21.   end;
  22. var
  23.   Form1: TForm1;
  24. implementation
  25. {$R *.dfm}
  26. procedure TForm1.GetFcun(MethodName: string);
  27. begin
  28.   @FExampleMethod := MethodAddress(MethodName);
  29.                      //MethodAddress(MethodName)返回 MethodName的地址
  30.                      //MethodAddress只支持published声明的方法
  31.   if Assigned(@FExampleMethod) then //用Assigned测试函数或过程变量是否为空
  32.   FExampleMethod(nil);
  33. end;
  34. procedure TForm1.xx1;
  35. begin
  36.   showmessage('xx1');
  37. end;
  38. procedure TForm1.xx2;
  39. begin
  40.   showmessage('xx2');
  41. end;
  42. procedure TForm1.xx3;
  43. begin
  44.   showmessage('xx3');
  45. end;
  46. procedure TForm1.Button1Click(Sender: TObject);
  47. begin
  48.     GetFcun(Edit1.Text);//调用GetFcun执行需要的方法
  49. end;
  50. end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值