如何实现两个具有相同名称的方法的接口?

Ste*_*ley 5 delphi inheritance interface tinterfacedobject

我试图声明一个自定义的接口列表,我想继承它以获取特定接口的列表(我知道IInterfaceList,这只是一个例子).我正在使用Delphi 2007,因此我无法访问实际的泛型(可惜我).

这是一个简化的例子:

   ICustomInterfaceList = interface
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   TCustomInterfaceList = class(TInterfacedObject, ICustomInterfaceList)
   public
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   ISpecificInterface = interface(IInterface)
   end;

   ISpecificInterfaceList = interface(ICustomInterfaceList)
      function GetFirst: ISpecificInterface;
   end;

   TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList)
   public
      function GetFirst: ISpecificInterface;
   end;
Run Code Online (Sandbox Code Playgroud)

TSpecificInterfaceList将无法编译:

E2211'GetFirst'声明与接口'ISpecificInterfaceList'中的声明不同

我想我理论上可以使用TCustomInterfaceList,但我不想每次使用时都要使用"GetFirst".我的目标是拥有一个特定的类,它继承基类的行为并包装"GetFirst".

我怎样才能做到这一点?

谢谢!

Dav*_*nan 6

ISpecificInterfaceList定义了三种方法.他们是:

procedure Add(AInterface: IInterface);
function GetFirst: IInterface;
function GetFirst: ISpecificInterface;
Run Code Online (Sandbox Code Playgroud)

因为您的两个函数共享相同的名称,所以需要帮助编译器识别哪个函数是哪个.

使用方法解决条款.

TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList)
public
  function GetFirstSpecific: ISpecificInterface;
  function ISpecificInterfaceList.GetFirst = GetFirstSpecific;
end;
Run Code Online (Sandbox Code Playgroud)