To make a method virtual or
dynamic, include the virtual or dynamic directive in its
declaration. Virtual and dynamic methods, unlike static methods, can be overridden
in descendant classes. When an overridden method is called, the actual
(runtime) type of the class or object used in the method call not the declared
type of the variable determines which implementation to activate.
To override a method, redeclare it
with the override directive. An override declaration must match
the ancestor declaration in the order and type of its parameters and in its
result type (if any).
In the following example, the Draw
method declared in TFigure is overridden in two descendant classes.
type
TFigure = class
procedure Draw; virtual;
end;
TRectangle = class(TFigure)
procedure Draw; override;
end;
TEllipse = class(TFigure)
procedure Draw; override;
end;
Given these declarations, the
following code illustrates the effect of calling a virtual method through a
variable whose actual type varies at runtime.
var
Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw; // calls
TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw; // calls
TEllipse.Draw
Figure.Destroy;
end;
Only virtual and dynamic methods
can be overridden. All methods, however, can be overloaded; see Overloading methods.
Virtual and dynamic methods are
semantically equivalent. They differ only in the implementation of method-call
dispatching at runtime. Virtual methods optimize for speed, while dynamic
methods optimize for code size.
In general, virtual methods are
the most efficient way to implement polymorphic behavior. Dynamic methods are
useful when a base class declares many overridable methods which are inherited
by many descendant classes in an application, but only occasionally overridden.
要使一个方法成为虚拟的或动态的,需要在其声明中包括指示字virtual或dynamic。虚拟方法和动态方法不同于静态方法,它们可以在其后裔类中被覆盖(overridden)。当一个覆盖方法被调用时,方法调用中使用的类或对象的实际(运行时)类型决定了哪一个实现是有效的,而非变量声明的类型决定。
要覆盖一个方法,需要在再声明中使用指示字override。一个override声明必需与其祖先声明在参数顺序和类型以及结果(如果有)等各方面匹配。
下面的例子中,TFigure中声明的Draw方法在两个后裔类中都被覆盖。
type
TFigure = class
procedure Draw; virtual;
end;
TRectangle = class(TFigure)
procedure Draw; override;
end;
TEllipse = class(TFigure)
procedure Draw; override;
end;
对于上面给出的声明,下面的代码举例说明了在运行时通过一个变量调用虚拟方法时,其类型变化的效果。
var
Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw; //调用的是TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw; //调用的是TEllipse.Draw
Figure.Destroy;
end;
只有虚拟方法和动态方法可以被覆盖。然而,所有的方法都可以被重载(overloaded),见重载方法。
虚拟方法和动态方法在语义上是等价的。其不同之处仅在于运行时对方法调用如何实现调度。虚拟方法在速度方面相对更优化,而动态方法在代码尺寸方面相对更优化。
一般情况下,虚拟方法是实现多种行为最有效的途径。当一个基本类声明了许多可覆盖的方法,并且在应用程序中将被许多后裔类继承但很少覆盖时,动态方法是有用的。