Delphi 系统[27]关键字和保留字 implements
Delphi 系统[27]关键字和保留字 implements
1、定义:
- implements :指出了一个属性从接口继承,此时属性被转换成接口对象。通过接口动态绑定属性,并动态的设定属性值。
2、示例及说明:
implements 指令允许您将接口的实现委托给实现类中的属性。例如:
1 | property MyInterface: IMyInterface read FMyInterface implements IMyInterface; |
声明一个名为MyInterface的属性,该属性实现接口IMyInterface。
implements指令必须是属性声明中的最后一个说明符,并且可以列出多个接口,用逗号分隔。委托属性逗号分隔。委托属性:
- 必须是类或接口类型。
- 不能是数组属性或具有索引说明符。
- 必须具有读取说明符。如果属性使用读取方法,则该方法必须使用默认的寄存器调用约定,并且不能是动态的(尽管可以是虚拟的)或指定消息指令。
注:用于实现委托接口的类应派生自 TAggregatedObject。
2.1 类-类型属性
如果委托属性属于类类型,则在搜索封闭类及其祖先之前,将搜索该类及其祖先以查找实现指定接口的方法。因此,可以在属性指定的类中实现一些方法,在声明属性的类中实现其他方法。方法解析子句可以用通常的方式来解析歧义或指定特定的方法。接口不能由多个类类型属性实现。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | type IMyInterface = interface procedure P1; procedure P2; end ; TMyImplClass = class procedure P1; procedure P2; end ; TMyClass = class (TInterfacedObject, IMyInterface) FMyImplClass: TMyImplClass; property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface; procedure IMyInterface . P1 = MyP1; procedure MyP1; end ; procedure TMyImplClass . P1; ... procedure TMyImplClass . P2; ... procedure TMyClass . MyP1; ... var MyClass: TMyClass; MyInterface: IMyInterface; begin MyClass := TMyClass . Create; MyClass . FMyImplClass := TMyImplClass . Create; MyInterface := MyClass; MyInterface . P1; // calls TMyClass.MyP1; MyInterface . P2; // calls TImplClass.P2; end ; |
2.2 接口-类型属性
如果委托属性属于接口类型,则该接口或其派生的接口必须出现在声明该属性的类的祖先列表中。委托属性必须返回一个对象,该对象的类完全实现implements指令指定的接口,并且该对象不使用方法解析子句。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | type IMyInterface = interface procedure P1; procedure P2; end ; TMyClass = class (TObject, IMyInterface) FMyInterface: IMyInterface; property MyInterface: IMyInterface read FMyInterface implements IMyInterface; end ; var MyClass: TMyClass; MyInterface: IMyInterface; begin MyClass := TMyClass . Create; MyClass . FMyInterface := ... // 类实现IMyInterface的某个对象 MyInterface := MyClass; MyInterface . P1; end ; |
创建时间:2021.08.16 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报