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ə183/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   179   180   181   182   183   184   185   186   ...   294
DelphiLanguageGuide

Class Operators
Every class inherits from TObject methods called ClassType and ClassParent that return, respectively, a reference
to the class of an object and of an object's immediate ancestor. Both methods return a value of type TClass (where
TClass = class of TObject
), which can be cast to a more specific type. Every class also inherits a method
called InheritsFrom that tests whether the object where it is called descends from a specified class. These methods
are used by the is and as operators, and it is seldom necessary to call them directly.
The is Operator
The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object. The
expression
objectisclass
returns True if object is an instance of the class denoted by class or one of its descendants, and False otherwise.
(If object is nil, the result is False.) If the declared type of object is unrelated to class - that is, if the types are distinct
and one is not an ancestor of the othera compilation error results. For example,
if ActiveControl is TEdit then TEdit(ActiveControl).SelectAll;
This statement casts a variable to 
TEdit
 after first verifying that the object it references is an instance of 
TEdit
 or
one of its descendants.
The as Operator
The as operator performs checked typecasts. The expression
objectasclass
returns a reference to the same object as object, but with the type given by class. At runtime, object must be an
instance of the class denoted by class or one of its descendants, or be nil; otherwise an exception is raised. If the
declared type of object is unrelated to class - that is, if the types are distinct and one is not an ancestor of the other
- a compilation error results. For example,
158


with Sender as TButton do 
 begin 
  Caption := '&Ok'; 
  OnClick := OkClick; 
 end;
The rules of operator precedence often require as typecasts to be enclosed in parentheses. For example,
(Sender as TButton).Caption := '&Ok';
Class Methods
A class method is a method (other than a constructor) that operates on classes instead of objects. The definition of
a class method must begin with the reserved word class. For example,
type 
  TFigure = class 
  public 
     class function Supports(Operation: string): Boolean; virtual; 
     class procedure GetInfo(var Info: TFigureInfo); virtual; 
     ... 
  end;
The defining declaration of a class method must also begin with class. For example,
class procedure TFigure.GetInfo(var Info: TFigureInfo); 
begin 
    ... 
end;
In the defining declaration of a class method, the identifier Self represents the class where the method is called
(which could be a descendant of the class in which it is defined). If the method is called in the class 
C
, then Self is
of the type class of 
C
. Thus you cannot use Self to access fields, properties, and normal (object) methods, but you
can use it to call constructors and other class methods.
A class method can be called through a class reference or an object reference. When it is called through an object
reference, the class of the object becomes the value of Self.
159


Exceptions
This topic covers the following material:
A conceptual overview of exceptions and exception handling
Declaring exception types
Raising and handling exceptions

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   179   180   181   182   183   184   185   186   ...   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