Originale-mail to me for new edition

 

Overloading methods

 

A method can be redeclared using the overload directive. In this case, if the redeclared method has a different parameter signature from its ancestor, it overloads the inherited method without hiding it. Calling the method in a descendant class activates whichever implementation matches the parameters in the call.

If you overload a virtual method, use the reintroduce directive when you redeclare it in descendant classes. For example,

type

  T1 = class(TObject)

    procedure Test(I: Integer); overload; virtual;

  end;

  T2 = class(T1)

    procedure Test(S: string); reintroduce; overload;

  end;

   ...

  SomeObject := T2.Create;

  SomeObject.Test('Hello!');  // calls T2.Test

  SomeObject.Test(7);         // calls T1.Test

Within a class, you cannot publish multiple overloaded methods with the same name. Maintenance of runtime type information requires a unique name for each published member.

type

  TSomeClass = class

  published

    function Func(P: Integer): Integer;

    function Func(P: Boolean): Integer  // error

     ...

Methods that serve as property read or write specifiers cannot be overloaded.

The implementation of an overloaded method must repeat the parameter list from the class declaration. For more information about overloading, see Overloading procedures and functions.

 

Topic groups

 

See also

Inheritance and scope

Methods: Overview

Overloading procedure and functions

Reintroduce

Virtual and dynamic methods

 

 

译文

 

重载方法

 

可以使用指示字overload对方法进行再声明。这时,如果再声明的方法与其祖先具有不同的参数标记,那么该方法重载了继承得到的方法而没有对其隐藏。调用后裔类中的方法将会根据调用时给出的参数决定激活哪一个方法。

如果要重载一个虚拟方法,那么在后裔类中对其再声明时应使用reintroduce指示字。例如,

type

  T1 = class(TObject)

    procedure Test(I: Integer); overload; virtual;

  end;

  T2 = class(T1)

    procedure Test(S: string); reintroduce; overload;

  end;

   ...

  SomeObject := T2.Create;

  SomeObject.Test('Hello!');  // 调用 T2.Test

  SomeObject.Test(7);         // 调用 T1.Test

在同一个类中不能公布(publish)多个重载的同名方法。运行时信息(RTTI)的维持要求每个公布(published)成员具有唯一的名称。

type

  TSomeClass = class

  published

    function Func(P: Integer): Integer;

    function Func(P: Boolean): Integer  // 错误

     ...

被说明符readwrite限定、服务于属性的方法不能被重载。

重载的方法在其实现(定义声明)时必需再次重申其参数列表。有关重载的更多信息,见重载过程和函数

 

主题组

 

相关主题

继承和作用域

方法:概述

重载过程和函数

再引入(Reintroduce

虚拟方法和动态方法