begin
Result := X div Y;
end;
这些声明创建了两个函数,它们都叫做Divide,使用不同类型的参数。当调用Divide时,编译器通过查看实参的类型来决定哪个函数被调用。比如,Divide(6.0, 3.0)调用第一个Divide函数,因为它的参数是实数类型。
You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine’s declarations, but that are assignment-compatible with the parameters in more than one declaration. This happens most frequently when a routine is overloaded with different integer types or different real types-for example, 你能给一个重载的例程传递这样的参数:它们的类型和所有例程声明中的参数类型都不同,但又和多个例程声明中的参数是赋值兼容的。对于使用了不同的整数(或实数)类型的重载例程来说,这种情况最经常发生,比如,
procedure Store(X: Longint); overload;
procedure Store(X: Shortint); overload;
在这种情况下,只要不产生模棱两可,编译器将调用如下的例程:传递给它的实参和它声明的参数相匹配,并且声明的参数范围最小。(记住,实数相关的常量值总是Extended类型。)
重载例程必须能以参数的数目或类型区分开来,因此,下面的声明将产生编译错误:
function Cap(S: string): string; overload;
...
procedure Cap(var Str: string); overload;
- 84 -
Procedures and functions
...
但声明
function Func(X: Real; Y: Integer): Real; overload;
...
function Func(X: Integer; Y: Real): Real; overload;
...
是合法的。
当重载例程被声明为forward、或在单元的接口部分声明时,在它的定义声明部分必须重新列出它的参数。
编译器能区分两个包含AnsiString/Pchar以及WideString/WideChar的重载例程。在这种情况下,字符串常量或文字被解释为native字符串或字符类型,也就是AnsiString/Pchar。
procedure test(const S: String); overload;
procedure test(const W: WideString); overload;
var
a: string;
b: widestring;
begin
a := 'a';
b := 'b';
test(a);// 调用String版本
test(b);// 调用widestring版本
test('abc');// 调用 String版本
test(WideString('abc')); // 调用widestring版本
end;
在声明重载例程时能使用Variant类型的参数。Variant类型被认为比简单类型更通用,精确的类型匹配比Variant更有优先权。If a Variant is passed into such an overload situation, and an overload that takes a Variant exists in that parameter position, it is considered to be an exact match for the Variant type.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23665-60.html
╯□╰)o