为什么某些Delphi组件需要“ AOwner:TComponent”?要构造它们? [英] Why Do Some Delphi Components Require "AOwner: TComponent" To Construct Them?

查看:173
本文介绍了为什么某些Delphi组件需要“ AOwner:TComponent”?要构造它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎完全没有必要要求TComponent作为所有者来实例化某种对象。为什么有这么多的Delphi组件需要此功能?

It seems completely irrelevant to require a TComponent as an owner to instantiate an object of some kind. Why are there so many Delphi components that require this?

例如,TXMLDocument需要一个TComponent对象进行实例化。

For example, TXMLDocument requires a TComponent object to instantiate.

这是为什么,如果有充分的理由,我应该在那里使用什么来做正确的事?

Why is this and if there's a good reason, what should I be using in there to "do the right thing"?

推荐答案

所有者组件应该管理所有它自己的组件。

The owner component is supposed to manage all its owned components. The owned components gets destroyed automatically when the owner is destroyed.

这有助于开发人员将组件从工具面板中拖放到窗体上,然后将其连接起来这些事件可以使他完成工作,而不必担心管理组件的寿命。

This helps the developer who just drags components from the tool-palette, drops them on the form and just hooks up the events to get his work done without worrying about managing the lifetime of the components.

该表单是放置在其上的所有组件的所有者。 Application对象是表单的所有者。当应用程序关闭时,Application对象将被销毁,这将销毁表单和所有组件。

The form is the owner of all components dropped on it. The Application object is owner of the form. When the app is closed the Application object is destroyed which in turn destroys the forms and all the components.

但是创建组件时,所有者并不是真正必需的。如果您将Nil传递给参数,则将在没有所有者的情况下创建组件,在这种情况下,您有责任管理组件的寿命。

But the owner is not really necessary when a components is created. If you pass Nil to the parameter, the component will be created without an owner and in this case it will be your responsibility to manage the component's lifetime.

这篇关于为什么某些Delphi组件需要“ AOwner:TComponent”?要构造它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆