我有这样的代码: TServer = classprivate fOnMsgFromServer: TProcString, String, String;public procedure RegisterOnMsgFromServer(aCallBack: TProcString, String, String); procedure Execute;end;procedure TServer.RegisterOnMsgFromServer(
TServer = class private fOnMsgFromServer: TProc<String, String, String>; public procedure RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>); procedure Execute; end; procedure TServer.RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>); begin fOnMsgFromServer := aCallBack; end; procedure TServer.Execute; begin fOnMsgFromServer('userName', 'password', 'message'); end;
问题在于程序执行当我想把参数放到fOnMsgFromServer时. “helper”显示我(Arg1:string; Arg2:string; Arg3:string),我只是不知道哪个参数是哪个.
有没有解决方案来命名这个论点?
如果使用通用TProc< T1,T2,T3>,则无法避免这些通用名称.类型.它的声明如下:type TProc<T1,T2,T3> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);
如您所见,这就是名称的起源.如果使用此类型,则会遇到这些名称.
相反,您应该声明一个定制的引用过程类型,而不是使用泛型类型.这不仅可以让你为论证传授有意义的名字,还可以让你不停地重复自我.
type TMsgFromServerProc = reference to procedure(UserName, Password, Msg: string); TServer = class private fOnMsgFromServer: TMsgFromServerProc; public procedure RegisterOnMsgFromServer(aCallBack: TMsgFromServerProc); end;