繁体   English   中英

`overload`关键字有什么区别吗?

[英]Does the `overload` keyword make any difference?

有时我在没有重载的方法后面有“overload”关键字。

除了代码的可读性和可维护性之外,这还有其他我应该注意的影响吗?

最大的区别在于,当方法的参数不正确时,对于非重载方法,错误消息明显更好。

program Test;

procedure F(X: Integer);
begin
end;

procedure G(X: Integer); overload;
begin
end;

var
  P: Pointer = nil;

begin
  F(P); // E2010 Incompatible types: 'Integer' and 'Pointer'
  G(P); // E2250 There is no overloaded version of 'G' that can be called with these arguments
end.

更巧妙的是,重载方法可能会超载您不知道的函数。 考虑标准的IfThen函数。 StrUtils.IfThen存在一次:

function IfThen(AValue: Boolean; const ATrue: string;
  AFalse: string = ''): string; overload; inline;

但它被标记为overload 这是因为它重载了Math.IfThen ,并且如果一个单元同时使用MathStrUtils ,则不合格的IfThen将根据参数解析为正确的函数,并且无论uses列表中单元的顺序如何。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM