If I have an object that implements an interface, it maps the interface methods automatically onto the class's methods with the same name and signature by default. Is there any way to override this and map an interface method onto a method with the same signature but a different name? (This could be useful, for example, if I implement two interfaces, both of which have a method with the same name and signature, and I want to be able to do different things with them.)
+4
A:
It is indeed possible. The technique is called Method Resolution Clause.
Erwin
2009-09-07 19:22:16
Thanks! I knew I'd seen something like that somewhere, but I couldn't remember how it was done.
Mason Wheeler
2009-09-07 19:30:16
+1
A:
This is a code example of Erwin's answer
type
ISomeInterface = interface
procedure SomeMethod;
end;
IOtherInterface = interface
procedure SomeMethod;
end;
TSomeClass = class(TInterfacedObject, ISomeInterface, IOtherInterface)
public
procedure ISomeInterface.SomeMethod = SomeInterfaceSomeMethod;
procedure IOtherInterface.SomeMethod = OtherInterfaceSomeMethod;
procedure SomeMethod; // TSomeClass.SomeMethod
procedure SomeInterfaceSomeMethod; // ISomeInterface.SomeMethod
procedure OtherInterfaceSomeMethod; // IOtherInterface.SomeMethod
end;
Lars Truijens
2009-09-08 06:54:40