You can override the default
name-based mappings by including method resolution clauses in a class
declaration. When a class implements two or more interfaces that have
identically named methods, use method resolution clauses to resolve the naming
conflicts.
A method resolution clause has the
form
procedure interface.interfaceMethod
= implementingMethod;
or
function interface.interfaceMethod
= implementingMethod;
where implementingMethod is a method
declared in the class or one of its ancestors. The implementingMethod
can be a method declared later in the class declaration, but cannot be a
private method of an ancestor class declared in another module.
For example, the class declaration
type
TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
function IMalloc.Alloc = Allocate;
procedure IMalloc.Free = Deallocate;
...
end;
maps IMalloc’s Alloc and Free
methods onto TMemoryManager’s Allocate and Deallocate
methods.
A method resolution clause cannot alter a mapping introduced by an ancestor class.
在类声明中,可以通过方法决定子句(method
resolution clauses)覆盖基于映射的缺省方法名。当一个类实现了两个或更多接口,而这些接口又有相同的方法名时,使用方法决定子句可以解决命名冲突。
方法决定子句具有如下形式
procedure interface.interfaceMethod
= implementingMethod;
或
function interface.interfaceMethod
= implementingMethod;
这里的implementingMethod是类或某个祖先类中声明的方法。implementingMethod可以是后来在类声明中声明的方法,但不能是其他模块中声明的祖先类中的私有方法。
例如,
type
TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
function IMalloc.Alloc = Allocate;
procedure IMalloc.Free = Deallocate;
...
end;
上面的类声明将接口IMalloc的方法Alloc和Free分别映射到TMemoryManager类的Allocate和Deallocate方法。
方法决定子句不能在祖先类中的映射之后引入。(即如果祖先类中已经有相关映射,那么派生类中则不能再用方法决定子句引入。)