How to map interface names to different method names?

孤街醉人 提交于 2019-12-10 21:44:24

问题


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.)


回答1:


It is indeed possible. The technique is called Method Resolution Clause.




回答2:


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;


来源:https://stackoverflow.com/questions/1390552/how-to-map-interface-names-to-different-method-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!