zoukankan      html  css  js  c++  java
  • Delphi中的“委托”

     .NET中有委托(Delegate)的概念,其声明形式如下所示:

     
      public delegate void MyDelegate(int aIntParam, string aStringParam);
     
      依个人所见,委托实际上就是规定一种接口,提供一种规范,任何符合该委托签名的函数/过程都属于同一类。
     
      在Delphi中,也有类似于“委托”的概念(不过可没有C#的功能丰富,不过两者从根本上说都应该是函数指针),如下所示:

     
      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中“委托”的应用。附件为完整程序。
      
    [delphi] view plaincopy
     
    1. {type 
    2.     TMyDelegateFunc = function (AIntParam: integer; AStringParam: string): Boolean; 
    3.     TMyDelegateProc = procedure (AIntParam: integer; AStringParam: string); 
    4.  
    5.   //在以上的声明中,还可以用of object关键字来规定所定义的“委托”是应用于对象的函数/过程,还是应用于非对象的函数/过程,例: 
    6.  
    7.   
    8.   type 
    9.     TMyObjectDelegate = procedure (AIntParam: integer; AStringParam: string) of object; //对象的函数/过程 
    10.     TMyRegularDelegate = procedure (AIntParam: integer; AStringParam: string); //非对象的(一般的)函数/过程 
    11.  
    12.   //以下举个简单的例子来说明一下Delphi中“委托”的应用。附件为完整程序。    }  
    13.     
    14. unit UnitFrmTest;  
    15. interface  
    16. uses  
    17.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
    18.   Dialogs, StdCtrls;  
    19. type  
    20.   TDelegateType = (dtObject, dtRegular);  
    21.     
    22.   //对象的函数委托  
    23.   TObjectNumFuncs = function (const ANumOne: Double;  
    24.     const ANumTwo: Double): Double of object;  
    25.   //非对象(一般)的函数委托  
    26.   TRegularNumFuncs = function (const ANumOne: Double;  
    27.     const ANumTwo: Double): Double;  
    28. type  
    29.   TfrmTest = class(TForm)  
    30.     edtNumOne: TEdit;  
    31.     edtNumTwo: TEdit;  
    32.     btnAdd: TButton;  
    33.     btnSub: TButton;  
    34.     btnMultiply: TButton;  
    35.     btnDivide: TButton;  
    36.     lblResult: TLabel;  
    37.     rbObjectDelegate: TRadioButton;  
    38.     rbRegularDelegate: TRadioButton;  
    39.     procedure rbRegularDelegateClick(Sender: TObject);  
    40.     procedure rbObjectDelegateClick(Sender: TObject);  
    41.     procedure MyButtonClick(Sender: TObject);  
    42.   private  
    43.     { Private declarations }  
    44.     //指示当前是使用对象的函数,还是非对象的函数  
    45.     FDelegateType: TDelegateType;  
    46.       
    47.     { 对象的函数列表 }  
    48.     function Add(const ANumOne: Double;  
    49.       const ANumTwo: Double): Double;  
    50.     function Sub(const ANumOne: Double;  
    51.       const ANumTwo: Double): Double;  
    52.     function Multiply(const ANumOne: Double;  
    53.       const ANumTwo: Double): Double;  
    54.     function Divide(const ANumOne: Double;  
    55.       const ANumTwo: Double): Double;  
    56.     { 对象的函数列表 结束 }  
    57.     function DoObjectCalc(const ANumOne: Double;  
    58.       const ANumTwo: Double; AMethod: TObjectNumFuncs): Double;  
    59.   public  
    60.     { Public declarations }  
    61.   end;  
    62.   
    63. { 非对象(一般)的函数列表 }  
    64.   function Add(const ANumOne: Double; const ANumTwo: Double): Double;  
    65.   function Sub(const ANumOne: Double; const ANumTwo: Double): Double;  
    66.   function Multiply(const ANumOne: Double; const ANumTwo: Double): Double;  
    67.   function Divide(const ANumOne: Double; const ANumTwo: Double): Double;  
    68.   function DoRegularCalc(const ANumOne: Double; const ANumTwo: Double;  
    69.     AMethod: TRegularNumFuncs): Double;  
    70. { 非对象(一般)的函数列表 结束 }  
    71. var  
    72.   frmTest: TfrmTest;  
    73. implementation  
    74. {$R *.dfm}  
    75. { 非对象(一般)的函数列表 }  
    76. function Add(const ANumOne: Double; const ANumTwo: Double): Double;  
    77. begin  
    78.   Result := ANumOne + ANumTwo;  
    79. end;  
    80. function Sub(const ANumOne: Double; const ANumTwo: Double): Double;  
    81. begin  
    82.   Result := ANumOne - ANumTwo;  
    83. end;  
    84. function Multiply(const ANumOne: Double; const ANumTwo: Double): Double;  
    85. begin  
    86.   Result := ANumOne * ANumTwo;  
    87. end;  
    88. function Divide(const ANumOne: Double; const ANumTwo: Double): Double;  
    89. begin  
    90.   try  
    91.     Result := ANumOne / ANumTwo;  
    92.   except  
    93.     on E: EZeroDivide do  
    94.     begin  
    95.       frmTest.edtNumTwo.SetFocus();  
    96.       frmTest.lblResult.Caption := '除数不能为零';  
    97.       Abort();  
    98.     end;  
    99.   end;  
    100. end;  
    101. function DoRegularCalc(const ANumOne: Double; const ANumTwo: Double;  
    102.   AMethod: TRegularNumFuncs): Double;  
    103. begin  
    104.   Result := AMethod(ANumOne, ANumTwo);  
    105. end;  
    106. { 非对象(一般)的函数列表 结束 }  
    107. { TfrmTest }  
    108. { 对象的函数列表 }  
    109. function TfrmTest.Add(const ANumOne, ANumTwo: Double): Double;  
    110. begin  
    111.   Result := ANumOne + ANumTwo;  
    112. end;  
    113. function TfrmTest.Divide(const ANumOne, ANumTwo: Double): Double;  
    114. begin  
    115.   try  
    116.     Result := ANumOne / ANumTwo;  
    117.   except  
    118.     on E: EZeroDivide do  
    119.     begin  
    120.       edtNumTwo.SetFocus();  
    121.       lblResult.Caption := '除数不能为零';  
    122.       Abort;  
    123.     end;  
    124.   end;  
    125. end;  
    126. function TfrmTest.DoObjectCalc(const ANumOne, ANumTwo: Double;  
    127.   AMethod: TObjectNumFuncs): Double;  
    128. begin  
    129.   Result := AMethod(ANumOne, ANumTwo);  
    130. end;  
    131. function TfrmTest.Multiply(const ANumOne, ANumTwo: Double): Double;  
    132. begin  
    133.   Result := ANumOne * ANumTwo;  
    134. end;  
    135. procedure TfrmTest.MyButtonClick(Sender: TObject);  
    136. var  
    137.   dblNumOne, dblNumTwo, dblResult: Double;  
    138. begin  
    139.   if not (Sender is TButton) then Exit;  
    140.   dblNumOne := StrToFloatDef(Trim(edtNumOne.Text), 0.0);  
    141.   dblNumTwo := StrToFloatDef(Trim(edtNumTwo.Text), 0.0);  
    142.     
    143.   case (Sender as TButton).Tag of  
    144.     0: //加  
    145.       begin  
    146.         case Self.FDelegateType of  
    147.           dtObject:  
    148.             begin  
    149.               dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Add);  
    150.               //若为  
    151.               //dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, UnitFrmTest.Add);  
    152.               //则会提示以下错误:  
    153.               //E2009 Incompatible types: 'regular procedure and method pointer'  
    154.             end;  
    155.           dtRegular:  
    156.             begin  
    157.               dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Add);  
    158.               //若为  
    159.               //dblResult := DoRegularCalc(dblNumOne, dblNumTwo, Self.Add);  
    160.               //则会提示以下错误:  
    161.               //E2009 Incompatible types: 'regular procedure and method pointer'  
    162.             end;  
    163.         end;  
    164.       end;  
    165.     1: //减  
    166.       begin  
    167.         case Self.FDelegateType of  
    168.           dtObject:  
    169.             begin  
    170.               dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Sub);  
    171.             end;  
    172.           dtRegular:  
    173.             begin  
    174.               dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Sub);  
    175.             end;  
    176.         end;  
    177.         
    178.       end;  
    179.     2: //乘  
    180.       begin  
    181.         case Self.FDelegateType of  
    182.           dtObject:  
    183.             begin  
    184.               dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Multiply);  
    185.             end;  
    186.           dtRegular:  
    187.             begin  
    188.               dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Multiply);  
    189.             end;  
    190.         end;  
    191.         
    192.       end;  
    193.     3: //除  
    194.       begin  
    195.         case Self.FDelegateType of  
    196.           dtObject:  
    197.             begin  
    198.               dblResult := Self.DoObjectCalc(dblNumOne, dblNumTwo, Self.Divide);  
    199.             end;  
    200.           dtRegular:  
    201.             begin  
    202.               dblResult := DoRegularCalc(dblNumOne, dblNumTwo, UnitFrmTest.Divide);  
    203.             end;  
    204.         end;  
    205.         
    206.       end;  
    207.       
    208.   end;  
    209.   lblResult.Caption := '结果:' + FloatToStr(dblResult);  
    210. end;  
    211. procedure TfrmTest.rbObjectDelegateClick(Sender: TObject);  
    212. begin  
    213.   Self.FDelegateType := dtObject;  
    214. end;  
    215. procedure TfrmTest.rbRegularDelegateClick(Sender: TObject);  
    216. begin  
    217.   Self.FDelegateType := dtRegular;  
    218. end;  
    219. function TfrmTest.Sub(const ANumOne, ANumTwo: Double): Double;  
    220. begin  
    221.   Result := ANumOne - ANumTwo;  
    222. end;  
    223. { 对象的函数列表 结束 }  
    224. end.  

    http://blog.csdn.net/procedure1984/article/details/3897028

  • 相关阅读:
    Linux_vi编辑器
    Linux_几个符号命令
    Linux_权限
    Linux_用户/用户组
    Linux_文件及文件夹[创建][复制][移动][删除][重命名]
    Linux_文件查看
    Linux_初识
    码农网站
    学习网站
    软件设计师考试范围
  • 原文地址:https://www.cnblogs.com/findumars/p/5087511.html
Copyright © 2011-2022 走看看