Delegating to a Class-Type Property If the delegate property is of a class type, that class and its ancestors are searched for methods implementing the
specified interface before the enclosing class and its ancestors are searched. Thus it is possible to implement some
methods in the class specified by the property, and others in the class where the property is declared. Method
resolution clauses can be used in the usual way to resolve ambiguities or specify a particular method. An interface
cannot be implemented by more than one class-type property. For example,
type
IMyInterface = interface
procedure P1;
procedure P2;
end;
TMyImplClass = class
procedure P1;
procedure P2;
end;
TMyClass = class(TInterfacedObject, IMyInterface)
FMyImplClass: TMyImplClass;
property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
procedure IMyInterface.P1 = MyP1;
procedure MyP1;
end;
procedure TMyImplClass.P1;
.
.
.
procedure TMyImplClass.P2;
.
.
.
procedure TMyClass.MyP1;
.
.
.
var
MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyImplClass := TMyImplClass.Create;
MyInterface := MyClass;
MyInterface.P1; // calls TMyClass.MyP1;
MyInterface.P2; // calls TImplClass.P2;
end;
198