Name
Implements Directive
Syntax
Property declarationreadGetterimplementsInterfaces...;
Description
A class can delegate the implementation of one or more interfaces to
a property with the implements directive. The
property’s Getter must be a field or
a simple method (no array or indexed properties allowed). The method
can be virtual, but not dynamic or a message handler. The
Getter field or method must have a class
or interface type. If the property implements more than one
interface, separate the interface identifiers with
commas.
The listed interfaces must appear in the class declaration.
Tips and Tricks
The
implementsdirective is often used to implement COM-style aggregation. TheComObjunit declares theTAggregatedObjectclass as the base class for the inner object.If the
Getteris of class type, you must be careful about the lifetime of the object. Once Delphi casts the object reference to an interface, Delphi’s automatic reference counting manages the object’s lifetime. Once the last interface is out of scope, the object will be freed automatically. The simplest way to manage this situation is for theGettermethod to create a new object every time it is called.If the
Getteris of class type, you can use method resolution clauses to redirect some of the interface methods to the containing class.If the
Getteris of interface type, the returned interface must implement all the listed interfaces. You cannot use method resolution to alter the interfaces.
Example
interface // ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access