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