An interface, like a class,
inherits all of its ancestors’ methods. But interfaces, unlike classes, do not
implement methods. What an interface inherits is the obligation to
implement methods, an obligation that devolves onto any class supporting the
interface.
The declaration of an interface
can specify an ancestor interface. If no ancestor is specified, the interface
is a direct descendant of IInterface, which is defined in the System
unit and is the ultimate ancestor of all other interfaces. IInterface
declares three methods: QueryInterface, _AddRef, and _Release.
Note: IInterface
is equivalent to IUnknown. You should generally use IInterface
for platform independent applications and reserve the use of IUnknown
for specific programs that include Windows dependencies.
QueryInterface provides the means to move freely among the different interfaces that an object supports. _AddRef and _Release provide lifetime memory management for interface references. The easiest way to implement these methods is to derive the implementing class from the System unit’s TInterfacedObject. It is also possible to dispense with any of these methods by implementing it as an empty function; COM objects (Windows only), however, must be managed through _AddRef and _Release.
与类相似,接口继承了其所有祖先的方法。而与类不同的是,接口不实现方法。接口继承的是实现方法的职责(obligation),该职责传递到任何支持接口的类。
接口声明可以指定其祖先接口。如果没有祖先接口被指定,那么接口是IInterface的一个直接后裔,IInterface声明于System单元,并且是其他所有接口的最终祖先。在IInterface中声明了三个方法:QueryInterface、_AddRef和_Release。
注意: IInterface等价于IUnknown。通常应在那些与平台无关的应用程序中使用IInterface,而对指定的程序、包括Windows相关的程序,使用IUnknown。
QueryInterface提供在一个对象的不同接口中自由迁移的手段。_AddRef and _Release为接口引用在生命时间提供内存管理。实现这些方法最容易的途径是起源于对System单元中TInterfacedObject类的实现。通过作为空函数实现这些方法,分发其中任何一个都是可能的;而COM对象(仅适用于Windows)必须通过_AddRef和_Release被管理。