Methods

Top  Previous  Next

A method is a piece of code that can be called from other places in the code, and on it's own can call other methods, or itself again. Methods are used to reuse the same logic from multiple places. Methods can have 0 or more parameters, and an optional return type.

 

 example using a static method.

 

 

A method is a procedure or function associated with a class. A call to a method specifies the object (or, if it is a class method, the class) that the method should operate on.

For example, SomeObject.Free calls the Free method in SomeObject.

 

About Methods

 

Within a class declaration, methods appear as procedure and function headings, which work like forward declarations. Somewhere after the class declaration, but within the same module, each method must be implemented by a defining declaration. For example, suppose the declaration of TMyClass includes a method called DoSomething:

 

 type

    TMyClass = class(TObject)

       ...

       procedure DoSomething;

       ...

    end;

 

 

A defining declaration for DoSomething must occur later in the module:

 procedure TMyClass.DoSomething;

 begin

      ...

 end;

 

 

While a class can be declared in either the interface or the implementation section of a unit, defining declarations for a class methods must be in the implementation section.

In the heading of a defining declaration, the method name is always qualified with the name of the class to which it belongs. The heading can repeat the parameter list from the class declaration; if it does, the order, type, and names of the parameters must match exactly, and if the method is a function, the return value must match as well.

Method declarations can include special directives that are not used with other functions or procedures. Directives should appear in the class declaration only, not in the defining declaration, and should always be listed in the following order:

reintroduce; overload; binding; calling convention; abstract; warning

where:

·binding is virtual, dynamic, or override;
·calling convention is register, pascal, cdecl, stdcall, or safecall; and
·warning is platform, deprecated, or library.

 

 

Inherited

 

The reserved word inherited plays a special role in implementing polymorphic behavior. It can occur in method definitions, with or without an identifier after it.

If inherited is followed by the name of a member, it represents a normal method call or reference to a property or field, except that the search for the referenced member begins with the immediate ancestor of the enclosing method's class. For example, when:

  inherited Create(...);

 

 

occurs in the definition of a method, it calls the inherited Create.

When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method or, if the enclosing method is a message handler, to the inherited message handler for the same message. In this case, inherited takes no explicit parameters, but passes to the inherited method the same parameters with which the enclosing method was called. For example:

  inherited;

 

occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters that were passed to the descendant.

 

 

Self

 

Within the implementation of a method, the identifier Self references the object in which the method is called. For example, here is the implementation of TCollection's Add method in the Classes unit:

 

 function TCollection.Add: TCollectionItem;

 begin

     Result := FItemClass.Create(Self);

 end;

 

 

The Add method calls the Create method in the class referenced by the FItemClass field, which is always a TCollectionItem descendant. TCollectionItem.Create takes a single parameter of type TCollection, so Add passes it the TCollection instance object where Add is called. This is illustrated in the following code:

var MyCollection: TCollection;

     ...

     MyCollection.Add   // MyCollection is passed to the 

                        // TCollectionItem.Create method

 

Self is useful for a variety of reasons. For example, a member identifier declared in a class type might be redeclared in the block of one of the class' methods. In this case, you can access the original member identifier as Self.Identifier.

 

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 descendent class type. They determine which implementation is activated when a method is called.

 

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;

 

 

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 descendent classes. When an overridden method is called, the actual (run-time) 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 descendent 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 run time:

 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 compiler also supports the concept of final virtual method. When the keyword final is applied to a virtual method, no descendent 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 compiler hints that allow it to optimize the code it produces.

 

Virtual versus Dynamic

In Delphi for Win32, virtual and dynamic methods are semantically equivalent. However, they differ in the implementation of method-call dispatching at run time: 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 that are inherited by many descendent classes in an application, but only occasionally overridden.

Note: Only use dynamic methods if there is a clear, observable benefit. Generally, use virtual methods.

 

Overriding versus Hiding

If a method declaration specifies the same method identifier and parameter signature as an inherited method, but does not include override, the new declaration merely hides the inherited one without overriding it. Both methods exist in the descendent class, where the method name is statically bound. For example:

 type

    T1 = class(TObject)

       procedure Act; virtual;

    end;

 

    T2 = class(T1)

       procedure Act;   // Act is redeclared, but not overridden

    end;

 

 var

    SomeObject: T1;

 

 begin

    SomeObject := T2.Create;

    SomeObject.Act;    // calls T1.Act

 end;

 

 

Reintroduce

The reintroduce directive suppresses compiler warnings about hiding previously declared virtual methods. For example:

 procedure DoSomething; reintroduce// The ancestor class also 

                                     // has a DoSomething method

 

Use reintroduce when you want to hide an inherited virtual method with a new one.

 

Abstract Methods

An abstract method is a virtual or dynamic method that has no implementation in the class where it is declared. Its implementation is deferred to a descendent class. Abstract methods must be declared with the directive abstract after virtual or dynamic. For example:

 

 procedure DoSomething; virtualabstract;

 

You can call an abstract method only in a class or instance of a class in which the method has been overridden.

 

Class Methods

 

Most methods are called instance methods, because they operate on an individual instance of an object. A class method is a method (other than a constructor) that operates on classes instead of objects. There are two types of class methods: ordinary class methods and class static methods.

 

 

Ordinary Class Methods

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 can 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 instance fields, instance properties, and normal (object) methods. You can use Self to call constructors and other class methods, or to access class properties and class fields.

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.

 

 

Class Static Methods

 

Like class methods, class static methods can be accessed without an object reference. Unlike ordinary class methods, class static methods have no Self parameter at all. They also cannot access any instance members. (They still have access to class fields, class properties, and class methods.) Also unlike class methods, class static methods cannot be declared virtual.

Methods are made class static by appending the word static to their declaration, for example:

 type

    TMyClass = class

      strict private

        class var

          FX: Integer;

 

      strict protected

        // Note: Accessors for class properties

        // must be declared class static.

        class function GetX: Integer; static;

        class procedure SetX(val: Integer); static;

 

      public

        class property X: Integer read GetX write SetX;

        class procedure StatProc(s: String); static;

    end;

 

 

Like a class method, you can call a class static method through the class type (for example, without having an object reference), such as:

 TMyClass.X := 17;

 TMyClass.StatProc('Hello');

 

 

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 descendent 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 descendent classes. For example:

 type

   T1 = class(TObject)

     procedure Test(I: Integer); overloadvirtual;

   end;

 

   T2 = class(T1)

     procedure Test(S: string); reintroduceoverload;

   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.