如何从两个同名接口实现两个方法?

How do I implement two methods from two interfaces with the same name?

我在使用 Delphi 10.4 中的方法解析子句时遇到一些问题。

假设我想创建一个猫和狗存储库。在这种情况下,TAnimalRepository 有猫和狗,所以我想在 class.

中实现猫和狗接口

示例:

IRepositoryBase<T> = Interface
    ['{776B3383-AF6E-44F7-BF32-1ACCF9A6C964}']
    function GetAll(items: TList<T>; out errMsg: String): Boolean;
End;

TCat = class
end;

ICatRepository = Interface(IRepositoryBase<TCat>)
    ['{C37CF989-E069-450B-8937-E46F6BFA66E9}']
    function GetAll(items: TList<TCat>; out errMsg: String): Boolean;
End;

TDog = class
end;

IDogRepository = Interface(IRepositoryBase<TDog>)
    ['{56B28463-0007-497E-8015-D794873328FF}']
    function GetAll(items: TList<TDog>; out errMsg: String): Boolean;
End;

TAnimalRepository = class(TInterfacedObject, ICatRepository, IDogRepository)
    function ICatRepository.GetAll = GetAllCats;
    function IDogRepository.GetAll = GetAllDogs;
public
    function GetAllCats(items: TList<TCat>; out errMsg: String): Boolean;
    function GetAllDogs(items: TList<TDog>; out errMsg: String): Boolean;
end;

由于我们有两个同名的不同方法,我尝试使用此处描述的方法解析子句: https://docwiki.embarcadero.com/RADStudio/Sydney/en/Implementing_Interfaces

function ICatRepository.GetAll = GetAllCats;
function IDogRepository.GetAll = GetAllDogs;

但是当我尝试编译上面的代码时,它失败了:

[dcc32 Error] MethodResolutionExample.dpr(33): E2291 Missing implementation of interface method MethodResolutionExample.IRepositoryBase<MethodResolutionExample.TDog>.GetAll

[dcc32 Error] MethodResolutionExample.dpr(33): E2291 Missing implementation of interface method MethodResolutionExample.IRepositoryBase<MethodResolutionExample.TCat>.GetAll

然后我想,为什么不尝试为他们继承的接口 IRepositoryBase 添加一个方法解析,就像这样:

TAnimalRepository = class(TInterfacedObject, ICatRepository, IDogRepository)
    function ICatRepository.GetAll = GetAllCats;
    function IRepositoryBase<TCat>.GetAll = GetAllCats;  // <--- This
    function IDogRepository.GetAll = GetAllDogs;
    function IRepositoryBase<TDog>.GetAll = GetAllDogs;  // <--- This
public
    function GetAllCats(items: TList<TCat>; out errMsg: String): Boolean;
    function GetAllDogs(items: TList<TDog>; out errMsg: String): Boolean;
end;

但现在它变得时髦了。看起来编译器无法处理方法解析子句中的泛型,因为我现在收到很多解析错误,开头为:

[dcc32 Error] MethodResolutionExample.dpr(35): E2023 Function needs result type

它抱怨这一行:

function IRepositoryBase<TCat>.GetAll = GetAllCats;

我做错了什么?我真的必须将我的 TAnimalRepository 分成两个不同的 classes 吗?

提前致谢。

哦,重要的注意事项。如果我的 ICatRepository 和 IDogRepository 不是从 IRepositoryBase 继承的,我可以让它工作。但在我的用例中,我希望它们从基础 class.

继承

您需要删除 ICatRepositoryIDogRepositoryGetAll 的附加声明.如果您只是将 GUID 保留在这些界面中,一切都会按预期工作。

type
  IRepositoryBase<T> = interface
    ['{776B3383-AF6E-44F7-BF32-1ACCF9A6C964}']
    function GetAll(items: TList<T>; out errMsg: string): Boolean;
  end;

  TCat = class
  end;

  ICatRepository = interface(IRepositoryBase<TCat>)
    ['{C37CF989-E069-450B-8937-E46F6BFA66E9}']
  end;

  TDog = class
  end;

  IDogRepository = interface(IRepositoryBase<TDog>)
    ['{56B28463-0007-497E-8015-D794873328FF}']
  end;

  TAnimalRepository = class(TInterfacedObject, ICatRepository, IDogRepository)
    function ICatRepository.GetAll = GetAllCats;
    function IDogRepository.GetAll = GetAllDogs;
  public
    function GetAllCats(items: TList<TCat>; out errMsg: string): Boolean;
    function GetAllDogs(items: TList<TDog>; out errMsg: string): Boolean;
  end;

额外的 GetAll 声明不只是替换通用声明,而是向接口添加另一种方法。