gpt4 book ai didi

delphi - 如何从两个同名接口(interface)实现两个方法?

转载 作者:行者123 更新时间:2023-12-05 02:35:52 26 4
gpt4 key购买 nike

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

假设我想创建一个猫狗仓库。在这种情况下,TAnimalRepository 既有猫也有狗,所以我想在该类中同时实现猫和狗接口(interface)。

例子:

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

然后我想,为什么不尝试为他们继承的接口(interface) 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 继承的,我可以让它工作。但在我的用例中,我希望它们从基类继承。

最佳答案

您需要在 ICatRepositoryIDogRepository 中删除 GetAll 的额外声明。如果您只是将 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 声明不只是替换通用声明,而是向接口(interface)添加另一种方法。

关于delphi - 如何从两个同名接口(interface)实现两个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70436810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com