【问题标题】:Interface delegation not working when using derived interfaces [duplicate]使用派生接口时接口委托不起作用[重复]
【发布时间】:2016-05-18 09:35:27
【问题描述】:

在 Delphi Seattle 10 中,我尝试使用 implements 关键字委托接口的实现,但是当从第一个接口派生的接口也包含在类声明中时,不接受委托。

以下代码的编译失败,并显示“缺少接口方法 IInterface1.DoSomethingFor1 的实现”消息。如果我从类声明中删除 IInterface2,则代码会编译。如果我从其他东西派生 IInterface2,那么 IInterface1 它也会编译。

我做错了什么?或者我怎样才能做到这一点?

type
  IInterface1 = interface(IInterface)
    ['{03FB3E2E-C0DD-4E17-B6CD-D333E1E7255E}']
    procedure DoSomethingFor1;
  end;

  Iinterface2 = interface(IInterface1)
    ['{89C266E2-2816-46AD-96AA-DD74E78A4D1E}']
  end;

  T1 = class(TInterfacedObject, IInterface1, IInterface2)
  private
    Fi1: IInterface1;
  public
    property I1_Delegate: IInterface1 read Fi1 implements IInterface1;
  end;

【问题讨论】:

标签: delphi


【解决方案1】:

您需要显式实现IInterface1IInterface2。你只实现前者。因此编译错误。你的实现类应该是这样的:

type
  TImplementingClass = class(TInterfacedObject, IInterface1, IInterface2)
  private
    Fi2: IInterface2;
  public
    property I2_Delegate: IInterface2 read Fi2 implements IInterface1, IInterface2;
  end;

【讨论】:

  • 我将 IInterface1 实现添加到类声明中,认为有必要委托实现,但看到这一点,我可以在类声明中完全省略 IInterface1 的使用。实现Interface2就够了。
  • 但错误消息据说是“缺少接口方法 IInterface1.DoSomethingFor1 的实现”而不是“缺少接口方法 IInterface2.DoSomethingFor1 的实现” “...
猜你喜欢
  • 1970-01-01
  • 2020-11-10
  • 2014-08-18
  • 2021-02-20
  • 2010-12-12
  • 2011-02-10
  • 2017-11-13
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多