对镜弹箜篌 发表于 2012-12-17 21:47:38

delphi 函数过程类型 委托...?

<div id="cnblogs_post_body">算是一种减少单元之间的引用的方法
类似委托
<div class="cnblogs_code" >http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttp://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gifView Code <div id="cnblogs_code_open_8f11664e-2229-4d28-8cb2-c29de8139342" class="cnblogs_code_hide"> 1 unit Unit5; 2 3 interface 4 5 uses System.Classes, System.SysUtils, Vcl.Dialogs; 6 7 type 8   // 声明过程类型 9   TObjFuns = procedure(const str1, str2: string) of object;10 11   IFormattedNumber = interface12   ['{19AE4E57-A022-45B1-AA42-43FF5142D685}']13   function FormattedString: string;14   function GetName: string;15   end;16 17   TFormattedInteger = class(TInterfacedObject, IFormattedNumber)18   private19     FValue: Integer;20   public21   // 声明过程变量22     FPro: TObjFuns;23   constructor Create(AValue: Integer);24   destructor Destroy; override;25   function FormattedString: string;26   function GetName: string;27   procedure DoDelegate(str1, str2: string);28   end;29 30   TFormattedHexInteger = class(TFormattedInteger, IFormattedNumber)31   destructor Destroy; override;32   function FormattedString: string;33   function GetName: string;34   end;35 36 implementation37 38 { TFormattedInteger }39 40 constructor TFormattedInteger.Create(AValue: Integer);41 begin42   inherited Create;43   DoDelegate('TFormattedInteger', 'Destroy');44   FValue := AValue;45 end;46 47 destructor TFormattedInteger.Destroy;48 begin49   DoDelegate('TFormattedInteger', 'Destroy');50   inherited Destroy;51 end;52 53 procedure TFormattedInteger.DoDelegate(str1, str2: string);54 begin55   // 使用过程变量56   if Assigned(FPro) then57     FPro(str1, str2);58 end;59 60 function TFormattedInteger.FormattedString: string;61 begin62   Result := 'This integer is' + IntToStr(FValue);63 end;64 65 function TFormattedInteger.GetName: string;66 begin67   Result := 'TFormattedInteger.GetName';68 end;69 70 { TFormattedHexInteger }71 72 destructor TFormattedHexInteger.Destroy;73 begin74   DoDelegate('TFormattedHexInteger', 'Destroy');75   inherited Destroy;76 end;77 78 function TFormattedHexInteger.FormattedString: string;79 begin80   Result := 'The hex integer is $' + IntToHex(FValue, 4);81 end;82 83 function TFormattedHexInteger.GetName: string;84 begin85   Result := 'TFormattedHexInteger.GetName';86 end;87 88 end.
页: [1]
查看完整版本: delphi 函数过程类型 委托...?