Delphi中的“委托”
.NET中有委托(Delegate)的概念,其声明形式如下所示:
TMyDelegateFunc = function (AIntParam: integer; AStringParam: string): Boolean;
TMyDelegateProc = procedure (AIntParam: integer; AStringParam: string);
TMyObjectDelegate = procedure (AIntParam: integer; AStringParam: string) of object; //对象的
TMyRegularDelegate = procedure (AIntParam: integer; AStringParam: string); //非对象的(一般的)函数/过程
- {type
- TMyDelegateFunc = function (AIntParam: integer; AStringParam: string): Boolean;
- TMyDelegateProc = procedure (AIntParam: integer; AStringParam: string);
- //在以上的声明中,还可以用of object关键字来规定所定义的“委托”是应用于对象的函数/过程,还是应用于非对象的函数/过程,例:
- type
- TMyObjectDelegate = procedure (AIntParam: integer; AStringParam: string) of object; //对象的函数/过程
- TMyRegularDelegate = procedure (AIntParam: integer; AStringParam: string); //非对象的(一般的)函数/过程
- //以下举个简单的例子来说明一下Delphi中“委托”的应用。附件为完整程序。 }
- unit UnitFrmTest;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TDelegateType = (dtObject, dtRegular);
- //对象的函数委托
- TObjectNumFuncs = function (const ANumOne: Double;
- const ANumTwo: Double): Double of object;
- //非对象(一般)的函数委托
- TRegularNumFuncs = function (const ANumOne: Double;
- const ANumTwo: Double): Double;
- type
- TfrmTest = class(TForm)
- edtNumOne: TEdit;
- edtNumTwo: TEdit;
- btnAdd: TButton;
- btnSub: TButton;
- btnMultiply: TButton;
- btnDivide: TButton;
- lblResult: TLabel;
- rbObjectDelegate: TRadioButton;
- rbRegularDelegate: TRadioButton;
- procedure rbRegularDelegateClick(Sender: TObject);
- procedure rbObjectDelegateClick(Sender: TObject);
- procedure MyButtonClick(Sender: TObject);
- private
- { Private declarations }
- //指示当前是使用对象的函数,还是非对象的函数
- FDelegateType: TDelegateType;
- { 对象的函数列表 }
- function Add(const ANumOne: Double;
- const ANumTwo: Double): Double;
- function Sub(const ANumOne: Double;
- const ANumTwo: Double): Double;
- function Multiply(const ANumOne: Double;
- const ANumTwo: Double): Double;
- function Divide(const ANumOne: Double;
- const ANumTwo: Double): Double;
- { 对象的函数列表 结束 }
- function DoObjectCalc(const ANumOne: Double;
- const ANumTwo: Double; AMethod: TObjectNumFuncs): Double;
- public
- { Public declarations }
- end;
- { 非对象(一般)的函数列表 }
- function Add(const ANumOne: Double; const ANumTwo: Double): Double;
- function Sub(const ANumOne: Double; const ANumTwo: Double): Double;
- function Multiply(const ANumOne: Double; const ANumTwo: Double): Double;
- function Divide(const ANumOne: Double; const ANumTwo: Double): Double;
- function DoRegularCalc(const ANumOne: Double; const ANumTwo: Double;
- AMethod: TRegularNumFuncs): Double;
- { 非对象(一般)的函数列表 结束 }
- var
- frmTest: TfrmTest;
- implementation
- {$R *.dfm}
- { 非对象(一般)的函数列表 }
- function Add(const ANumOne: Double; const ANumTwo: Double): Double;
- begin
- Result := ANumOne + ANumTwo;
- end;
- function Sub(const ANumOne: Double; const ANumTwo: Double): Double;
- begin
- Result := ANumOne - ANumTwo;
- end;
- function Multiply(const ANumOne: Double; const ANumTwo: Double): Double;
- begin
- Result := ANumOne * ANumTwo;
- end;
- function Divide(const ANumOne: Double; const ANumTwo: Double): Double;
- begin
- try
- Result := ANumOne / ANumTwo;
- except
- on E: EZeroDivide do
- begin
- frmTest.edtNumTwo.SetFocus();
- frmTest.lblResult.Caption := '除数不能为零';
- Abort();
- end;
- end;
- end;
- function DoRegularCalc(const ANumOne: Double; const ANumTwo: Double;
- AMethod: TRegularNumFuncs): Double;
- begin
- Result := AMethod(ANumOne, ANumTwo);
- end;
- { 非对象(一般)的函数列表 结束 }
- { TfrmTest }
- { 对象的函数列表 }
- function TfrmTest.Add(const ANumOne, ANumTwo: Double): Double;
- begin
- Result := ANumOne + ANumTwo;
- end;
- function TfrmTest.Divide(const ANumOne, ANumTwo: Double): Double;
- begin
- try
- Result := ANumOne / ANumTwo;
- except
- on E: EZeroDivide do
- begin
- edtNumTwo.SetFocus();
- lblResult.Caption := '除数不能为零';
- Abort;
- end;
- end;
- end;
- function TfrmTest.DoObjectCalc(const ANumOne, ANumTwo: Double;
- AMethod: TObjectNumFuncs): Double;
- begin
- Result := AMethod(ANumOne, ANumTwo);
- end;
- function TfrmTest.Multiply(const ANumOne, ANumTwo: Double): Double;
- begin
- Result := ANumOne * ANumTwo;
- end;
- procedure TfrmTest.MyButtonClick(Sender: TObject);
- var
- dblNumOne, dblNumTwo, dblResult: Double;
- begin
- if not (Sender is TButton) then Exit;
- dblNumOne := StrToFloatDef(Trim(edtNumOne.Text), 0.0);
- dblNumTwo := StrToFloatDef(Trim(edtNumTwo.Text), 0.0);
- case (Sender as TButton).Tag of
- 0: //加
- begin
- case Self.FDelegateType of
- dtObject:
- begin
- dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Add);
- //若为
- //dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, UnitFrmTest.Add);
- //则会提示以下错误:
- //E2009 Incompatible types: 'regular procedure and method pointer'
- end;
- dtRegular:
- begin
- dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Add);
- //若为
- //dblResult := DoRegularCalc(dblNumOne, dblNumTwo, Self.Add);
- //则会提示以下错误:
- //E2009 Incompatible types: 'regular procedure and method pointer'
- end;
- end;
- end;
- 1: //减
- begin
- case Self.FDelegateType of
- dtObject:
- begin
- dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Sub);
- end;
- dtRegular:
- begin
- dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Sub);
- end;
- end;
- end;
- 2: //乘
- begin
- case Self.FDelegateType of
- dtObject:
- begin
- dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Multiply);
- end;
- dtRegular:
- begin
- dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Multiply);
- end;
- end;
- end;
- 3: //除
- begin
- case Self.FDelegateType of
- dtObject:
- begin
- dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Divide);
- end;
- dtRegular:
- begin
- dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Divide);
- end;
- end;
- end;
- end;
- lblResult.Caption := '结果:' + FloatToStr(dblResult);
- end;
- procedure TfrmTest.rbObjectDelegateClick(Sender: TObject);
- begin
- Self.FDelegateType := dtObject;
- end;
- procedure TfrmTest.rbRegularDelegateClick(Sender: TObject);
- begin
- Self.FDelegateType := dtRegular;
- end;
- function TfrmTest.Sub(const ANumOne, ANumTwo: Double): Double;
- begin
- Result := ANumOne - ANumTwo;
- end;
- { 对象的函数列表 结束 }
- end.
http://blog.csdn.net/procedure1984/article/details/3897028
Delphi中的“委托”的更多相关文章
- c++中实现委托
成员函数指针与高性能的C++委托(上篇) 撰文:Don Clugston 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做"闭包(clo ...
- [转]Delphi 中动态链接库(dll)的建立和使用
动态链接库是一个能够被应用程序和其它的DLL调用的过程和函数的集合体,它里面包含的是公共代码或资源.由于DLL代码使用了内存共享技术,在某些地方windows也给了DLL一些更高的权限,因而DLL中可 ...
- delphi中接口的委托和聚合
Delphi的TRegistry注册表类 方法详解 Delphi的接口编程入门 delphi中接口的委托和聚合 2009-09-27 10:44:44| 分类: 默认分类 | 标签: |举报 |字 ...
- Delphi中stringlist分割字符串的用法
Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...
- delphi中exit,abort,break,continue 的区别
from:http://www.cnblogs.com/taofengli288/archive/2011/09/05/2167553.html delphi中表示跳出的有break,continue ...
- Delphi中使用比较少的一些语法
本文是为了加强记忆而写,这里写的大多数内容都是在编程的日常工作中使用频率不高的东西,但是又十分重要. ---Murphy 1,构造和析构函数: a,构造函数: 一般基于TComponent组件的派生类 ...
- 如何在 Delphi 中静态链接 SQLite
搞了我几个小时,终于成功在 Delphi 中静态链接了 SQLite (v3.5.4),下一步就是研究加密了,呵呵中间其实遇到很多问题,今天累了,就不说了,改天补上 下载测试工程 下面说说方法 1.当 ...
- 翻箱倒柜,《Delphi中建议使用的语句》
(*//标题:Delphi中建议使用的语句整理:Zswang连接:http://www.csdn.net/Expert/TopicView1.asp?id=724036日期:2002-06-22支持: ...
- delphi中break,continue, exit,abort, halt, runerror的异同
delphi中表示跳出的有break,continue, exit,abort, halt, runerror. 1.break 强制退出循环(只能放在循环中),用于从For语句,while语句或re ...
随机推荐
- 转 C#中静态方法与非静态方法区别比较
C#静态方法与非静态方法的区别不仅仅是概念上的,那么他们有什么具体的区别呢?让我们通过本文向你做一下解析. C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用 ...
- SQL知识三(Day 27)
大家好,好几天都没写博客了.因为自己的一些原因,落下了很多.今天没有学什么新的知识,自己就把以前落下的好好看了一下.好了,今天就先总结一下SQL剩下的一些知识吧. 主要学的知识有:循环语句(case语 ...
- PL/SQL编程要点和注意点
http://www.itpub.net/thread-1560427-3-1.html 1. 非关键字小写,关键字大写,用下划线分隔,用前缀区分变量与表列名.不强求变量初始值.2. 永远只捕获可预测 ...
- Jquery 获取IP地址
//获取ip和地址 $(function () { var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callba ...
- hadoop搭建杂记:Linux下hostname的更改办法
VirtualBox搭建hadoop伪分布式模式:更改hostname VirtualBox搭建hadoop伪分布式模式:更改hostname master: ip:192.168.56.120 机器 ...
- python 发送安全邮件
用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...
- fiddler--手机https
1.访问fiddler的主讲加端口,比如我的是:http://192.168.1.103:8080 在点击fiddlerroot certificate 下载安装证书即可
- java 如何得到ISO 8601 时间格式
http://blog.csdn.net/brightleo/article/details/7457004 public class DateUtil { public static String ...
- BZOJ 1096 [ZJOI2007]仓库建设(斜率优化DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1096 [题目大意] 有个斜坡,有n个仓库,每个仓库里面都有一些物品,物品数目为p,仓库 ...
- Asp.net MVC中关于@Html标签的使用
@Html帮助器简单说明,记录些基本的跟HTML中对应的@html帮助器,@Html基本包含了html中的表单控件和常用Html 在@Html中,带有For的主要是针对强类型的Html类型. 用于说明 ...