Delphi匿名方法(二):使用本地变量
2013-02-21 16:24
369 查看
在匿名函数中,可以修改本地变量的值
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs; type TIntSum = reference to procedure (x, y: Integer); TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private procedure plusXandY(x, y: Integer; intSum: TIntSum); { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var lResult: Integer; begin plusXandY(40, 30, procedure (x, y: Integer) begin lResult := lResult + x + y; end); ShowMessageFmt('x + y = %d', [lResult]); // 调用三次,结果是210 end; procedure TForm1.plusXandY(x, y: Integer; intSum: TIntSum); begin intSum(x, y); intSum(x, y); intSum(x, y); end; end.
相关文章推荐
- 面向对象——OOP(一)(落地法则、局部变量与全局变量、方法及重载、构造器及重载、代码块、内部类及匿名内部类、对象的创建与使用)
- C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
- DELPHI中EXCEL组件的使用方法
- 不用重装DELPHI重新使用的方法
- Asp.net中static变量和viewstate的使用方法(谨慎)
- 使用strpos函数时,在本地测试没有问题,但是上传到服务器没有反应(变量类型)
- DELPHI使用TClientDataSet时不携带MIDAS.DLL的方法
- twig里使用js变量的方法
- Eclipse恢复已删除的文件和代码、svn使用了还原,但本地的没有提交找回没提交代码的方法
- 有关全局变量的正确使用方法
- C#实现在匿名方法中捕获外部变量的方法
- 【新版】VC中全局变量的使用总结(N种方法,看完就不再疑惑啦!)
- 对Java方法内部使用this访问成员变量的深入理解
- 构造方法和匿名对象的使用
- 使用匿名方法 委托
- 解析在PHP中使用全局变量的几种方法
- JS字符串替换函数全部替换方法 JS 正则表达式如何使用变量
- python中使用shell命令的输出作为其变量的方法
- 使用Docker Toolbox 创建v1.12.6版的Docker虚拟机的方法-注意正确使用本地文件 file:参数的路径名
- DELPHI使用TClientDataSet时不携带MIDAS.DLL的方法