内容简介:翻译自:https://stackoverflow.com/questions/14342263/how-do-i-check-whether-an-interfaced-object-implements-another-interface
我有这些接口声明:
IListener = interface procedure HandleEvent(AEvent: TMyEvent); end; IExtendedListener = interface(IListener) ['{85A3456A-D5E5-4F37-ABDD-A75A7B3B494C}'] // required by GetInterface procedure HandleExtendedEvent(AExtendedEvent: TMyExtendedEvent); end;
给定实现IListener的东西的接口引用,我如何检查它是否也实现了IExtendedListener?我正在使用代码下面的代码,因为这是一个编译时错误,如果Listener是IExtendedListener然后….
TSomeClass.Notify(AEvent: TMyExtendedEvent); var Listener: IListener; ExtListener: IExtendedListener; Obj: TObject; begin for Listener in FListeners do begin // works but smells funny Obj := Listener as TObject; Obj.GetInterface(IExtendedListener, ExtListener); if Assigned(ExtListener) then ExtNotifyee.HandleExtendedEvent(AEvent); end; end;
您应该使用Sysutils.Supports来检查特殊接口
uses SysUtils; TSomeClass.Notify(AEvent: TMyExtendedEvent); var Listener : IListener; ExtListener : IExtendedListener; begin for Listener in FListeners do if Supports( Listener, IExtendedListener, ExtListener ) then ExtListener.HandleExtendedEvent( AEvent ); end;
翻译自:https://stackoverflow.com/questions/14342263/how-do-i-check-whether-an-interfaced-object-implements-another-interface
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 面向对象接口多态
- 区块链技术语言(二十五):面向对象——接口
- Golang面向对象编程之继承&虚基类【组合&接口】
- 面向对象基本原则(1)- 单一职责原则与接口隔离原则
- [ PHP 内核与扩展开发系列] 类与面向对象:接口实现与类的继承
- c# – JSON.NET:如何反序列化基于parent(holder)对象值的接口属性?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Design in a Nutshell
Jennifer Niederst / O'Reilly Media, Inc. / 2006-02-21 / USD 34.99
Are you still designing web sites like it's 1999? If so, you're in for a surprise. Since the last edition of this book appeared five years ago, there has been a major climate change with regard to web......一起来看看 《Web Design in a Nutshell》 这本书的介绍吧!