怎么动态调用其它单元的函数(参数传递)(100分)

C

chrisn

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么根据函数名动态调用其它单元的函数(参数传递)
不在同一单元的函数调用,参数传递应注意哪些问题
最好能举例说明

 
先uses其它单元,如果参数声明为var时,其实参须为变量,而不能为常量
 
动态调用啊,就是使用一个函数,该函数的一个参数是函数名,

传入另一个函数名,

进行调用
 
定义:
procedure Proc1(a,b,c:integer);
procedure Proc2(x,y:integer;proc:pointer);
procedure Proc3(s:string);
实现:
procedure Proc1(a,b,c:integer);
begin
end;
procedure Proc2(x,y:integer;proc:pointer);
begin
end;
procedure Proc3(s:string);
var
y1,y2:integer;
begin
Proc2(y1,y2,@Proc1);
end;
 
函数不在同一单元内
 
没人有更好的方法吗
 
//不就是使用过程参数吗

//Unit1:
type
TCalculateFunc=function(x:integer):integer;

function XxX(x:integer):integer;
begin
Result:=x*x;
end;

function XpX(x:integer):integer;
begin
Result:=x+x;
end;

//Uint2

procedure ShowResult(CalcFunc:TCalculateFunc; x:integer);
begin
ShowMessage(IntToStr(CalcFunc(x)));
end;

//main
begin
ShowResult(XxX,99);
ShowResult(XpX,99);
end;

如果参数形式不统一,可以使用对象/结构等使之统一
 
顶部