您的位置:首页 > 编程语言 > Delphi

delphi中函数指针的用法

2007-05-10 17:29 330 查看
delphi中可以通过函数指针把一个函数作为参数来传递,然后在另外一个函数中调用。

1) 首先,申明函数指针类型TFunctionParameter。

type
TFunctionParameter = function(const value : integer) : string;

2) 定义准备被作为参数传递的函数

function One(const value : integer) : string;
begin
result := IntToStr(value) ;
end;

function Two(const value : integer) : string;
begin
result := IntToStr(2 * value) ;
end;

3) 定义将要使用动态函数指针参数的函数

function DynamicFunction(f : TFunctionParameter; const value : integer) : string;
begin
result := f(value) ;
end;

4) 上面这个动态函数的使用实例

var
s : string;
begin
s := DynamicFunction(One,2006) ;
ShowMessage(s) ; //will display "2006"

s := DynamicFunction(Two,2006) ;
ShowMessage(s) ; // will display "4012"
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: