6

我正在尝试声明要从中继承的接口的自定义列表,以获取特定接口的列表(我知道 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;

TSpecificInterfaceList 不会编译:

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

我想我理论上可以使用 TCustomInterfaceList 但我不想每次使用它时都必须投射“GetFirst”。我的目标是拥有一个既继承基类的行为又包装“GetFirst”的特定类。

我怎样才能做到这一点?

谢谢!

4

3 回答 3

7

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

procedure Add(AInterface: IInterface);
function GetFirst: IInterface;
function GetFirst: ISpecificInterface;

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

使用方法解析子句

TSpecificInterfaceList = class(TCustomInterfaceList, ISpecificInterfaceList)
public
  function GetFirstSpecific: ISpecificInterface;
  function ISpecificInterfaceList.GetFirst = GetFirstSpecific;
end;
于 2015-01-13T19:01:24.377 回答
3

不确定这在 Delphi7 中是否也可以,但您可以尝试在声明中使用方法解析子句。

function interface.interfaceMethod = implementingMethod;

如果可能,这将帮助您解决命名冲突。

于 2015-01-13T19:00:27.780 回答
0

对于方法,如果参数不同,您也可以选择覆盖。

如果您有以后的后代实现后代接口,则接口函数映射非常困难,因为这些函数不会作为接口方法转发给下一个类,因此您需要重新映射它们。

于 2015-01-15T22:47:38.567 回答