当前位置 : 主页 > 编程语言 > delphi >

delphi – 泛型类型命名

来源:互联网 收集:自由互联 发布时间:2021-06-23
我有这样的代码: 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;
网友评论