07.Delphi接口的生命周期
在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.
07.Delphi接口的生命周期的更多相关文章
- 【07】react 之 生命周期
阅读目录(Content) 实例化 getDefaultProps getInitialState componentWillMount render componentDidMount 存在期 co ...
- ApplicationListener接口的生命周期
create:游戏初次创建时被调用,一般在这里创建ApplicationListener对象,初始化布景信息,将数据或UI布局绑定到监听器等,执行完该方法后会执行resize方法,是游戏创建立即执行的 ...
- Delphi 接口使用中,对象生命周期管理,如何释放需要注意的问题
网上有篇文章<Delphi接口编程的两大陷阱>,里面提到接口的生存期管理的问题.但该文章里面提到的两个问题,其实都是对 Delphi 不理解导致的. 先说该篇文章中提到的第一个问题为什 ...
- Angular2 组件生命周期
1. 说明 Angular每个组件都存在一个生命周期,从创建,变更到销毁.Angular提供组件生命周期钩子,把这些关键时刻暴露出来,赋予在这些关键结点和组件进行交互的能力. 2. 接口 按照生命周期 ...
- ExecutorService生命周期
ExecutorService接口继承了Executor接口,定义了一些生命周期的方法 public interface ExecutorService extends Executor { void ...
- Spring应用上下文中Bean的生命周期
Bean装载到Spring应用上下文的生命周期,如图: Bean在Spring容器中从创建到销毁经历了若干个阶段,每一阶段都可以对Spring如何管理Bean进行个性化定制,以下我们通过代码去验证生命 ...
- Spring 中 Bean 的生命周期
所谓 Bean 的生命周期,就是一个 Bean 从创建到销毁,所经历的各种方法调用.大致包含下面几个方法(不是全部) Bean 的实例化,调用了构造方法. 使用 setter 方法填充属性. 一旦依赖 ...
- 深究Spring中Bean的生命周期
前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...
- 生命周期感知 Lifecycle
奉上翻译原文地址: 处理生命周期 :翻译过程中加上了自己的一点理解.理解不对的地方直接评论就好. 生命周期感知组件可以感知其他组件的生命周期,例如 Activity,Fragment等,以便于在组件的 ...
随机推荐
- Vue学习笔记:计算属性
使用函数的缺点 如果我们想要将数据经过转化后再显示,或者多个数据结合起来进行显示,一般可以直接在数据渲染或者数据绑定的时候书写表达式 如果表达式过于复杂,或者逻辑太多的时候,我们可以将其封装在函数里, ...
- 第九届蓝桥杯B组决赛 调手表(完全背包)
问题描述 M78 星云的一个小时有 n 分钟. 大家都知道,手表只有一个按钮可以把当前的数加一.在调分钟的时候,如果当前显示的数是 0 ,那么按一下按钮就会变成 1,再按一次变成 2 .如果当前的数是 ...
- PAT T1004 To Buy or Not to Buy - Hard Version
暴力搜索加剪枝~ #include<bits/stdc++.h> using namespace std; ; string t; string s[maxn]; int pos[maxn ...
- 【转】issue management in your test project
What is Issue Management? Issue Management is the process to make others aware of the problem and th ...
- shell脚本添加脚本执行时间和当前运行次数current running time
#!/bin/bash ############################ #Author:Bing #Create time:3/31/2017 ####################### ...
- 重構電影網源碼 1905.com - 數據庫結構表
最近閒來無事,想著克隆一個電影網站. WWW.ROAK.COM 技術語言:JAVA EE * j2ee核心组件:jsp.servlet.jdbc.ejb.jndi * 数据通信:xml标记语言 * ...
- WebMagic基础与Maven管理依赖
2. 快速开始 WebMagic主要包含两个jar包:webmagic-core-{version}.jar和webmagic-extension-{version}.jar.在项目中添加这两个包的依 ...
- 事件类型-UI事件、焦点事件
DOM3级事件包括以下几类事件: UI事件:当用户与页面上的元素交互时触发 焦点事件:当元素获得或失去焦点时触发 鼠标事件:当用户通过鼠标在页面上执行操作时触发 滚轮事件:当使用鼠标滚轮时触发 文本事 ...
- python3 引入selenium库报错ModuleNotFoundError: No module named 'selenium'
解决方法: pip install -U selenium
- siblings() 获取同胞元素的用法
1. $("h2").siblings().css({"color":"red","border":"2px ...