我想学习一个插件框架,但是那个框架里面大量用到了接口,于是不得不把接口看一下了。总感觉接口编程这一块非常的绕,每一行都注释了。

unit Unit1;

interface

uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls; type
// 父接口
IGreetable = interface
['{FE5A34E5-21AB-4120-971B-FDC3241AD55D}']
// 用于返回一段文本
function SayHello: string;
end; TMan = class(TObject, IGreetable)
function SayHello: string;
// 接口都会有的3个函数(QueryInterface,_AddRef,_Release)
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
// 弹出一段文本
procedure Greeting(Intf: IGreetable);
{Private declarations}
public
{Public declarations}
end; var
Form1: TForm1; implementation {$R *.DFM} // 这里实现自己定义的_AddRef、_Release和QueryInterface方法 // 用于查找接口
function TMan.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
// 找到返回0,找不到返回'No such interface supported'
if GetInterface(IID, Obj) then
Result :=
else
Result := Windows.E_NoInterface;
end; // 接口调用计数
function TMan._AddRef: Integer;
begin
Result := -
end; // 接口释放计数
function TMan._Release: Integer;
begin
Result := -
end; // 客户函数,返回一段文本
function TMan.SayHello: string;
begin
Result := 'Hello';
end; procedure TForm1.Greeting(Intf: IGreetable);
begin
// 输出一段话
ShowMessage(Intf.SayHello);
end; procedure TForm1.Button1Click(Sender: TObject);
var
// 定义一个父接口
Intf: IGreetable;
begin
// 子接口创建
Intf := TMan.Create;
// 调用的是子接口中的Greeting函数
Greeting(Intf);
end; end.

最新文章

  1. js构建ui的统一异常处理方案(一)
  2. Java并发编程学习笔记(三)——对象的组合
  3. Redis基础(转)
  4. HDU 5056 Boring Count --统计
  5. [OpenS-CAD]屏幕坐标转换分析
  6. CodeSmith连接Mysql配置
  7. nginx打开目录游览功能
  8. 9 款赏心悦目的 HTML5/CSS3 特效
  9. 侧滑RESideMenu的使用
  10. linux vi 中s 替换方法
  11. 获取windows身份认证网站页面内容
  12. Day033--Python--进程
  13. 框架源码系列三:手写Spring AOP(AOP分析、AOP概念学习、切面实现、织入实现)
  14. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
  15. Windows下libjpeg-trubo-1.5.3编译(VS2015)
  16. 使用.NET向webService传double、int、DateTime 服务器得到的数据时null的问题(转http://blog.csdn.net/slimboy123/article/details/4366701)
  17. 为何gpio_to_irq不能静态使用?【转】
  18. javascript事件处理程序的3个阶段
  19. 用join取代not in
  20. mac 安装 pecl

热门文章

  1. 笔记-python-standard library-8.3.collections
  2. Spring SpringMVC 和 Springboot 的关系(转载)
  3. Java 常用转换
  4. linux 基本命令学习总结
  5. [理解] Linux 作为一个服务器是怎样的存在 (一)
  6. shell 脚本通过Webhook 发送消息到微信群
  7. html常用整理
  8. Hadoop大实验——MapReduce的操作
  9. 六 一对多关联查询&关联查询小结
  10. rar 配合 python 实现 excel密码保护 破解