我在 Delphi 10.4 中使用方法決議子句時遇到了一些問題。
假設我想創建一個 cat 和 dog 存盤庫。在這種情況下,TAnimalRepository 有貓和狗,所以我想在那個類中實作貓和狗的介面。
例子:
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 繼承,我可以讓它作業。但在我的用例中,我希望它們從基類繼承。
uj5u.com熱心網友回復:
你需要擺脫的補充宣告GETALL在ICatRepository和IDogRepository。如果您只是將 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宣告不僅替換了通用宣告,而且還向介面添加了另一種方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/389668.html
上一篇:Delphi10.1FMX如何將RoundRect位圖和TPath復制到TImage上
下一篇:TEdgeBrowser-OnNewWindowRequested-在另一個TEdgeBrowser中打開新視窗
