Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə163/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   159   160   161   162   163   164   165   166   ...   294
DelphiLanguageGuide

Virtual and Dynamic Methods
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 callnot the declared type of the variabledetermines
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; 
142


    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.
The Delphi for .NET compiler supports the concept of a final virtual method. When the keyword final is applied to a
virtual method, no ancestor class can override that method. Use of the final keyword is an important design decision
that can help document how the class is intended to be used. It can also give the .NET JIT compiler hints that allow
it to optimize the code it produces.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   159   160   161   162   163   164   165   166   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin