【发布时间】:2022-01-22 22:55:39
【问题描述】:
我在使用 Delphi 10.4 中的方法解析子句时遇到了一些问题。
假设我想创建一个猫和狗的存储库。在这种情况下,TAnimalRepository 既有猫又有狗,所以我想在该类中同时实现 cat 和 dog 接口。
例子:
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 分成两个不同的类吗?
提前致谢。
哦,重要的一点。如果我的 ICatRepository 和 IDogRepository 没有从 IRepositoryBase 继承,我可以让它工作。但在我的用例中,我希望它们从基类继承。
【问题讨论】: