加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 我无法用接口编译类

发布时间:2020-12-15 09:09:48 所属栏目:大数据 来源:网络整理
导读:我试图创建一个实现接口的类,但我得到这些错误: [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 int
我试图创建一个实现接口的类,但我得到这些错误:

[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)的其他基础对象是:TAggregatedObjectTContainedObject.然而,这些是特殊用途的工具,只有在您真正了解自己在做什么时才能使用.

将类的定义更改为

TTPA_MailJournal = class(TInterfacedObject,ITable)

你的代码将编译.

有关详细信息,请参阅Delphi basics.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读