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

delphi – 如何实现两个具有相同名称的方法的接口?

发布时间:2020-12-15 09:31:41 所属栏目:大数据 来源:网络整理
导读:我试图声明一个自定义的接口列表,我想继承它以获取特定接口的列表(我知道IInterfaceList,这只是一个例子).我正在使用Delphi 2007,因此我无法访问实际的泛型(可惜我). 这是一个简化的例子: ICustomInterfaceList = interface procedure Add(AInterface: IInt
我试图声明一个自定义的接口列表,我想继承它以获取特定接口的列表(我知道IInterfaceList,这只是一个例子).我正在使用Delphi 2007,因此我无法访问实际的泛型(可惜我).

这是一个简化的例子:

ICustomInterfaceList = interface
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   TCustomInterfaceList = class(TInterfacedObject,ICustomInterfaceList)
   public
      procedure Add(AInterface: IInterface);
      function GetFirst: IInterface;
   end;

   ISpecificInterface = interface(IInterface)
   end;

   ISpecificInterfaceList = interface(ICustomInterfaceList)
      function GetFirst: ISpecificInterface;
   end;

   TSpecificInterfaceList = class(TCustomInterfaceList,ISpecificInterfaceList)
   public
      function GetFirst: ISpecificInterface;
   end;

TSpecificInterfaceList将无法编译:

E2211 Declaration of ‘GetFirst’ differs from declaration in interface ‘ISpecificInterfaceList’

我想我理论上可以使用TCustomInterfaceList,但我不想每次使用时都要使用“GetFirst”.我的目标是拥有一个特定的类,它继承基类的行为并包装“GetFirst”.

我怎样才能做到这一点?

谢谢!

解决方法

ISpecificInterfaceList定义了三种方法.他们是:

procedure Add(AInterface: IInterface);
function GetFirst: IInterface;
function GetFirst: ISpecificInterface;

因为您的两个函数共享相同的名称,所以需要帮助编译器识别哪个函数是哪个.

使用method resolution clause.

TSpecificInterfaceList = class(TCustomInterfaceList,ISpecificInterfaceList)
public
  function GetFirstSpecific: ISpecificInterface;
  function ISpecificInterfaceList.GetFirst = GetFirstSpecific;
end;

(编辑:李大同)

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

    推荐文章
      热点阅读