views:

147

answers:

2

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
Thanks! I knew I'd seen something like that somewhere, but I couldn't remember how it was done.
Mason Wheeler
+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