Delphi中具有不同参数类型的函数

Rep*_*til 1 delphi

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

  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;
Run Code Online (Sandbox Code Playgroud)

Dal*_*kar 11

使用overload指令

function MsgW(const Msg:String):Integer; overload;
function MsgW(const Msg:String;title:String):Integer; overload;
Run Code Online (Sandbox Code Playgroud)

重载方法

  • FWIW,我只想把它写成一个函数,使用一个默认参数:`function MsgW(const Msg:string; const Title:string =''):Integer;`并且没有重载. (5认同)
  • @MartynA这是一个众所周知的现象.人们会赞同他们所知道的准确性.像这样的问题答案很简单,因为问题很简单,答案广为人知,得到了很多赞成,因为有很多人相信自己会认为它是正确的.另一方面,准确回答模糊的棘手问题,其中很少有人具备必要的知识来回答往往会得到更少的赞成. (2认同)