我试图创建一个实现接口的类,但我得到这些错误: [dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface.QueryInterface[dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing
[dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface.QueryInterface [dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface._AddRef [dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface._Release [dcc32 Fatal Error] MainUnit.pas(8): F2063 Could not compile used unit 'dl_tPA_MailJournal.pas'
代码是:
unit dl_tPA_MailJournal;
interface
uses
Windows,
Generics.Collections,
SysUtils,
uInterfaces;
type
TtPA_MailJournal = class(TObject, ITable)
public
function GetanQId: integer;
procedure SetanQId(const Value: integer);
function GetadDate: TDateTime;
procedure SetadDate(const Value: TDateTime);
function toList: TList<string>;
constructor Create(aId : Integer; aDate : TDateTime);
private
property anQId : integer read GetanQId write SetanQId;
property adDate : TDateTime read GetadDate write SetadDate;
end;
implementation
{ TtPA_MailJournal }
constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime);
begin
SetanQId(aId);
SetadDate(aDate);
end;
function TtPA_MailJournal.GetadDate: TDateTime;
begin
Result := adDate;
end;
function TtPA_MailJournal.GetanQId: integer;
begin
Result := anQId ;
end;
procedure TtPA_MailJournal.SetadDate(const Value: TDateTime);
begin
adDate := Value;
end;
procedure TtPA_MailJournal.SetanQId(const Value: integer);
begin
anQId := Value;
end;
function TtPA_MailJournal.toList: TList<string>;
var
aListTable: TList<TtPA_MailJournal>;
aTable: TtPA_MailJournal;
aListString: TList<String>;
begin
aTable.Create(1,now);
aListTable.Add(aTable);
aTable.Create(2,now);
aListTable.Add(aTable);
aListString.Add(aListTable.ToString);
Result := aListString;
end;
end.
界面是:
unit uInterfaces;
interface
uses
Generics.Collections;
type
ITable = Interface
['{6CED8DCE-9CC7-491F-8D93-996BE8E4D388}']
function toList: TList<string>;
end;
implementation
end.
问题是您使用TObject作为您的类的父级.你应该使用
TInterfacedObject.
在Delphi中,每个接口都继承自IInterface,因此至少有以下3 methods:
> _AddRef
> _Release
> QueryInterface
您必须通过自己实现这些方法或从包含这些方法的基础对象继承来实现这3种方法.
因为您从TObject继承,但是您没有实现这3个方法,所以会出现编译错误.如果您阅读编译器错误,您将看到它实际上为您解释了这个遗漏.
TInterfacedObject已经为您实现了这些方法.
实现IInterface (aka IUnknown)的其他基础对象是:TAggregatedObject和TContainedObject.然而,这些是特殊用途的工具,只有在您真正了解自己在做什么时才能使用.
将类的定义更改为
TTPA_MailJournal = class(TInterfacedObject, ITable)
你的代码将编译.
有关详细信息,请参阅Delphi basics.
