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等,以便于在组件的 ...
随机推荐
- Scrapy 分布式爬取
由于受到计算机能力和网络带宽的限制,单台计算机运行的爬虫咋爬取数据量较大时,需要耗费很长时间.分布式爬取的思想是“人多力量大”,在网络中的多台计算机同时运行程序,公童完成一个大型爬取任务, Scrap ...
- 【转】Chrome开发者工具详解
https://www.jianshu.com/p/7c8552f08e7a Chrome开发者工具详解(1)-Elements.Console.Sources面 Chrome开发者工具详解(2)-N ...
- System.arraycopy方法解释
数组拷贝 public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int lengt ...
- jdk动态代理和cglib动态代理底层实现原理超详细解析(jdk动态代理篇)
代理模式是一种很常见的模式,本文主要分析jdk动态代理的过程 1.举例 public class ProxyFactory implements InvocationHandler { private ...
- android 简单列表对话框(AlertDialog.Builder().setItems())
? 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 33 34 35 36 ...
- ie9下浏览器 cosole.log()会阻止j下面的s加载
ie9下浏览器 cosole.log()会阻止j下面的s加载,删掉多余的console.log().
- SpringBoot项目中自定义注解的使用
1.定义注解接口 @Documented @Retention(RUNTIME) @Target(METHOD) public @interface MyLog { String value() ...
- 136、Java的内部类
01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; class ...
- js——form表单验证
用js实现一个简易的表单验证 效果: 代码: <html> <head> <title>js校验form表单</title> <meta char ...
- ProtoBuf开发者指南
目录 1 概览 1.1 什么是protocol buffer 1.2 他们如何工作 1.3 为什么不用XML? 1.4 听起来像是为我的解决方案,如何开始? 1.5 一点历史 ...