【问题标题】:Function with different parameter type in DelphiDelphi中具有不同参数类型的函数
【发布时间】:2015-06-30 17:37:58
【问题描述】:

如何编写两个具有相同名称但参数类型不同的函数,如下所示:

  public
    { Public declarations }
    function MsgW(const Msg:String):Integer;
    function MsgW(const Msg:String;title:String):Integer;


function MsgW(const Msg:String;title:String):Integer;
Begin
  Result := MessageboxW(0,Pchar(Msg),Pchar(title),MB_OK);
End;

function MsgW(const Msg:String):Integer;
Begin
  Result := MessageboxW(0,Pchar(Msg),'MessageBoxW',MB_OK);
End;

【问题讨论】:

    标签: delphi


    【解决方案1】:

    使用overload 指令

    function MsgW(const Msg:String):Integer; overload;
    function MsgW(const Msg:String;title:String):Integer; overload;
    

    Overloading Methods

    【讨论】:

    • 奇怪的是,这个相当敷衍的回答却得到了如此多的支持。
    • @MartynA SO 以神秘的方式工作。有时你会在帮助某人只获得他的接受时向后弯腰,有时你会在几秒钟内写下答案,哇......
    • 我意识到 SO 以神秘(而且越来越令人沮丧,imo)的方式工作。我并不是要暗示 +1 有什么不好的地方,我只是认为 SO 的总体思路是好的 qs 和好的答案通过投票的力量渗透到顶部,并且在像这样的答案的情况下,我曾希望选民们可能比他们更能缓和他们的热情。也许是元数据,但我开始认为元数据最终表明用户应该更多地出去。
    • FWIW,我会简单地把它写成一个函数,带有一个默认参数:function MsgW(const Msg: string; const Title: string = ''): Integer; 并且没有重载。
    • @MartynA 这是一个众所周知的现象。人们会赞成他们知道是准确的。像这样的问题,因为问题很简单,答案很简单,而且答案广为人知,得到了很多支持,因为有很多人相信自己判断它是正确的。另一方面,对于具有必要知识的人很少回答的晦涩棘手问题的准确答案往往会获得较少的赞成票。
    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多