随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


问题来源: http://www.cnblogs.com/del/archive/2008/08/15/1268301.html#1806783

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  public
    function MySqr(const num: Integer): Integer; {这是类中的一个方法, 准备做参数使用}
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Type
  TFun = function(const num: Integer): Integer of object; {定义的类型参数同上}

{这是上面那个方法的实现}
function TForm1.MySqr(const num: Integer): Integer;
begin
  Result := num * num;
end;

{把 TFun 类型的方法做参数}
procedure MyProc(var x: Integer; fun: TFun);
begin
  x := fun(x);
end;

{测试}
procedure TForm1.FormCreate(Sender: TObject);
var
  n: Integer;
begin
  n := 9;
  MyProc(n, MySqr);
  ShowMessage(IntToStr(n)); {81}
end;

end.

posted on 2010-04-21 16:03  万一  阅读(...)  评论(...编辑  收藏