继承接口的接口委派是否需要一个包装类? [英] Does interface delegation of an inherited interface require a wrapper class?

查看:165
本文介绍了继承接口的接口委派是否需要一个包装类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi允许使用 implements 关键字进行界面委派。



例如

  IIndep1 = interface 
function foo2:integer;
结束

IIndep2 = interface
function goo2:integer;
结束

TIndep1And2 = class(TInterfacedObject,IIndep1,IIndep2)
private
FNested:IIndep1; //例如。通过构造函数或内部创建(未显示)
public
构造函数Create(AIndep1:IIndep1);
function goo2:integer;
属性AsIndep1:IIndep1读取FNested implements IIndep1;
结束

这样做很好,但不是继承的接口。 (错误消息缺少接口方法ILev1.foo的实现)

  ILev1 = interface 
function foo:Integer;
结束

ILev2 = interface(ILev1)
function goo:Integer;
结束

TLev2Fails = class(TInterfacedObject,ILev1,ILev2)//单独使用ILev2失败(错误:接口列表中没有提到ILev1)
private
FNested:ILev1; //通过构造函数或内部创建的
public
构造函数Create(AILev1:ILev1);
function goo:Integer;
属性AsLev1:ILev1读取FNested implements ILev1;
结束

解决方法是添加一个额外的祖先类

  TLev1Wrapper = class(TInterfacedObject,ILev1)
private
FNested:ILev1; //通过构造函数或内部创建的
public
构造函数Create(AILev1:ILev1);
属性AsLev1:ILev1读取FNested implements ILev1;
结束

TLev2Works = class(TLev1Wrapper,ILev2)
public
function goo:Integer;
结束

有没有办法避免包装器类祖先?




只是接口委托的一个注释,使用实现的目的是避免直接满足接口,但将该要求传递给聚合或组成的成员。提供完整的界面和手动委派给组合的成员,使使用实现来获取的好处导致了界面。事实上,在这种情况下,实现关键字和属性可能会被删除。

解决方案

有没有办法避免包装器类的祖先?


不,继承的界面有被绑定到一个祖先类。 Delphi不会隐式地绑定继承的接口,因为的原因这里。此外,它不能绑定到当前类,除非它手动将所有调用委托给组合项。



我们只剩下一个选项。一个祖先的包装类,我们可以绑定这个类 ILev1


Delphi allows for interface delegation using the implements keyword.

For example

IIndep1 = interface
  function foo2: integer;
end;

IIndep2 = interface
  function goo2: integer;
end;

TIndep1And2 = class(TInterfacedObject, IIndep1, IIndep2)
private
  FNested : IIndep1; //e.g. passed via constructor or internally created (not shown here)
public
   Constructor Create(AIndep1: IIndep1);
  function goo2: integer;
   property AsIndep1 : IIndep1 read FNested implements IIndep1;
end;

That works well, but not for inherited interfaces. (Error message "Missing implementation of interface method ILev1.foo")

ILev1 = interface
  function foo: Integer;
end;

ILev2 = interface(ILev1)
  function goo: Integer;
end;

TLev2Fails = class(TInterfacedObject, ILev1, ILev2) //Also fails with ILev2 alone (Error: "ILev1 not mentioned in interface list")
private
  FNested : ILev1; //passed via constructor or internally created 
public
   Constructor Create(AILev1: ILev1);
   function goo: Integer;
   property AsLev1 : ILev1 read FNested implements ILev1;
end;

The workaround is to add an extra ancestor class

TLev1Wrapper = class(TInterfacedObject, ILev1)
private
  FNested : ILev1; //passed via constructor or internally created 
public
   Constructor Create(AILev1: ILev1);
   property AsLev1 : ILev1 read FNested implements ILev1;
end;

TLev2Works = class(TLev1Wrapper, ILev2)
public
  function goo: Integer;
end;

Is there a way to avoid the wrapper class ancestor?

[EDIT] Just a note on interface delegation, the purpose of using implements is to avoid satisfying the interface directly, but passing that requirement to an aggregated or composed member. Providing the full interface and manually delegating to a composed member defeats the benefits gained from using implements to direct the interface. In fact, in that case the implements keyword and property may be removed.

解决方案

Is there a way to avoid the wrapper class ancestor?

No, the inherited interface has to be bound to a an ancestor class. Delphi does not implicitly bind inherited interfaces, because of reasons explained here. Also, it cannot bind to the current class, unless it manually delegates all calls to the composed item.

We are left with only one option. An ancestor wrapper class, to which we can bind ILev1

这篇关于继承接口的接口委派是否需要一个包装类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆