delphi中可以通过函数指针把一个函数作为参数来传递,然后在另外一个函数中调用。
1、首先,申明函数指针类型TFunctionParameter。
type TFunction = function(const value : integer) : string;
2、定义准备被作为参数传递的函数
function Opcname(const value : integer) : string;
begin
result := IntToStr(value) ;
end;
function TsnName(const value : integer) : string;
begin
result := IntToStr(2 * value) ;
end;
3、定义将要使用动态函数指针参数的函数
function DynamicFunction(f : TFunction ; const value : integer) : string;
begin
result := f(value) ;
end;
4、上面这个动态函数的使用实例
var
s : string;
begin
s := DynamicFunction(Opcname,2333) ;
ShowMessage(s) ; //will display "2333"
s := DynamicFunction(TsnName,1111) ;
ShowMessage(s) ; // will display "2222"
end;
以上实例测试正常,可以在delphi xe的所有版本执行测试通过