Interfaces

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



Interfaces provide a way of separating interface declarations from the actual code implementation, thus promoting code re-use. You can consider an interface as the specification of a small number of related methods/properties/events which will be implemented later by one or more classes.

Consider the following code unit:

namespace ConsoleApplication1;

interface

type
  IMovement = public interface
    method Walk;
    method Run;
    method Swim;
    property NoOfLegs: Integer read write;
    event Movement: EventHandler;
  end;

implementation

end.

Take note of the NoOfLegs property - it only declares that its values can be read and written - doesn't define method names to be used. This is up to the class(es) implementing the interface, e.g.:

type
  MyMovement = class(IMovement)
    fNoOfLegs: Integer;
    ...
    method SetNoOfLegs(aLegs: Integer);
    ...
    property NoOfLegs: Integer read fNoOfLegs write SetNoOfLegs;
  end;

Note: there is no need for virtual/override directives because interfaces are virtual by definition.

Delphi Prism provides the ability to implement generic types (for 2.0 framework or later).

Single Method Interfaces

When an interface only has 1 method and no ancestry types, it can be used as a delegate type. The method will then act as the signature of the delegate and the compiler will let you assign lambdas and method pointers to it.

See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox