在Delphi的接口中,是不需要释放的,调用完之后,接口的生命周期就结束了,如下面的例子

unit mtReaper;

interface

type
// 定义一个接口
IBase = interface
['{F3E97960-3F35-11D7-B847-001060806215}']
end; TSun = class(TInterfacedObject, IBase)
private
FObject: TObject;
public
constructor Create(AObject: TObject);
destructor Destroy; override;
end; implementation uses
SysUtils; constructor TSun.Create(AObject: TObject);
begin
FObject := AObject;
end; destructor TSun.Destroy;
begin
// 销毁时,释放传入的对象
FreeAndNil(FObject);
inherited;
end; end.

调用单元如下

unit frmMain;

// Download by http://www.codefans.net
interface uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls; type
TDeath = class(TObject)
public
destructor Destroy; override;
end; TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure WaitAWhile;
public
{Public declarations}
end; var
Form1: TForm1; implementation uses
mtReaper;
{$R *.dfm} destructor TDeath.Destroy;
begin
showMessage('对象销毁了!');
inherited;
end; procedure TForm1.WaitAWhile;
var
i: Integer;
begin
for i := to do
begin
Caption := Inttostr(i);
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var
m_Death: TDeath;
begin
m_Death := TDeath.Create;
try
WaitAWhile;
finally
m_Death.Free;
end;
end; procedure TForm1.Button2Click(Sender: TObject);
var
m_Death: TDeath;
m_Base: IBase;
begin
m_Death := TDeath.Create;
// interface接口,不用手动释放,调用完之后,生命就结束了
m_Base := TSun.Create(m_Death);
WaitAWhile;
end; end.

最新文章

  1. java.io.Serializable 序列化接口
  2. CSS伪类与CSS伪元素的区别及由来
  3. I am back-电商网站开发&jQuery
  4. [IBM DB2] db2 terminate 和 db2 connect reset 有什么区别?
  5. CentOS6 下rsync服务器配置
  6. HDU2588 GCD(欧拉函数)
  7. servlet 配置
  8. Agile.Net 组件式开发平台 - 脚本管理组件
  9. Android中的事件分发机制总结
  10. 浅析五大ASP.NET数据控件
  11. php回调函数callback函数实例
  12. cookie有效期到了后,是由浏览器还是由系统还删除的
  13. Python你必须知道的十个库
  14. Ruby: Count unique elements and their occurences in an array
  15. 使用poi根据模版生成word文档,支持插入数据和图片
  16. 【死磕 Spring】----- IOC 之 加载 Bean
  17. vue+element-ui实现行数可控的表格输入
  18. 各类排序算法的实现C#版
  19. 苹果pns推送和唤醒
  20. 【BZOJ1800】[AHOI2009]飞行棋(暴力)

热门文章

  1. burpsuite使用--暴力破解
  2. 在webView中的返回键
  3. Set和Map集合的比较
  4. awk及sum求和!
  5. pdf.js-----后端返回utf-8数据流,前端处理数据展示pdf
  6. Kali aircrack-ng wifi密码破解(暴力)
  7. MySQL帮助文档的使用
  8. 一 SpringMvc概述&入门配置
  9. js对象无法当成参数传递 解决方法
  10. Linux和云供应商Red Hat被IBM以34亿美元的价格收购