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ə162/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   158   159   160   161   162   163   164   165   ...   294
DelphiLanguageGuide

Method Binding
Method bindings can be static (the default), virtual, or dynamic. Virtual and dynamic methods can be overridden,
and they can be abstract. These designations come into play when a variable of one class type holds a value of a
descendant class type. They determine which implementation is activated when a method is called.
141


Static Methods
Methods are by default static. When a static method is called, the declared (compile-time) type of the class or object
variable used in the method call determines which implementation to activate. In the following example, the 
Draw
methods are static.
type 
    TFigure = class 
      procedure Draw; 
    end; 
    TRectangle = class(TFigure) 
      procedure Draw; 
    end;
Given these declarations, the following code illustrates the effect of calling a static method. In the second call to
Figure.Draw
, the 
Figure
 variable references an object of class 
TRectangle
, but the call invokes the
implementation of 
Draw
 in 
TFigure
, because the declared type of the 
Figure
 variable is 
TFigure
.
var 
    Figure: TFigure; 
    Rectangle: TRectangle; 
          
    begin 
            Figure := TFigure.Create; 
            Figure.Draw;              // calls TFigure.Draw 
            Figure.Destroy; 
            Figure := TRectangle.Create; 
            Figure.Draw;              // calls TFigure.Draw 
            TRectangle(Figure).Draw;  // calls TRectangle.Draw 
            Figure.Destroy; 
            Rectangle := TRectangle.Create; 
            Rectangle.Draw;          // calls TRectangle.Draw 
            Rectangle.Destroy; 
    end;

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   158   159   160   161   162   163   164   165   ...   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