Delphi 中的公共类成员和发布类成员有什么区别?
请有人解释一下 Delphi 中的公开类成员和发布类成员之间有什么区别?
我尝试查看 Delphi 帮助,我知道这些成员具有相同的可见性,但我不太了解它们有何不同以及何时应该使用已发布的成员而不是公共成员。
多谢。
Please could someone explain me what's the difference between public and published class members in Delphi?
I tried to look at Delphi help and I understand that these members have the same visibility, but I don't understand very well how they differ and when should I use published members instead of public ones.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
编译器为已发布成员生成 RTTI(运行时类型信息)元数据,但不为公共成员生成 RTTI(运行时类型信息)元数据(默认情况下)。这样做的主要效果是,对象的已发布属性将在设计时出现在对象检查器中。
我不知道您是否正在编写组件,但如果您这样做,您可能知道属性和事件通常是发布的,以便可以使用对象检查器来设置它们。
Public
MyProperty
在对象检查器中不可见。已发布的
MyProperty
将在对象检查器中可见。The compiler generates RTTI (Run-Time Type Information) metadata for published members, but not for public members (by default). The main effect of this is that the published properties of an object will appear in the Object Inspector at design time.
I do not know if you are writing components, but if you do, you probably know that properties and events are normally published, so that they can be set using the Object Inspector.
Public
MyProperty
will not be visible in the Object Inspector.Published
MyProperty
will be visible in the Object Inspector.正如您已经说过的,公共属性和已发布的属性具有相同的可见性。已发布的属性包含在 RTTI 中,而公共属性则不包含。
Public properties and published properties have the same visibility, as you already stated. Published properties are included in RTTI, public properties aren't.
附带说明一下,published 还有一个特殊之处:
类成员的默认可见性是
published
,因此请检查是否有不安全的代码,例如:Name
、Password
和DecryptPassword()
是“全球”可见的。As a side note, there is another special thing with published:
The default visibility of class members is
published
, so check for unsafe code like:Name
,Password
andDecryptPassword()
are visible 'world-wide'.已发布的属性将导出运行时类型信息 (RTTI)。
看看此处有关 Delphi 中的 RTTI
Published properties will export Runtime Type Information (RTTI).
Have a look here about RTTI in Delphi
似乎已经有很多好的答案,指出了 Object INspector、RTTI、
等等。这些都是拼图的碎片。
如果去掉published关键字,整个Delphi RAD工具设计将需要某种方式来指定哪些属性存储在DFM中,在组件属性检查器中检查,并且可以在表单或数据模块运行时从DFM重新加载被创建。
简而言之,这就是 Published 的目的。对我来说有趣的是,QT(最初是 TrollTech,后来成为诺基亚的一部分,后来仍然分拆到 Digia)的设计者必须为其 C++ RAD 库“QT”模拟这种级别的 RTTI,添加“已发布”的等效项和“属性”等价物,而纯 C++ 仍然缺乏这种基本设施。
It seems there are lots of good answers already, pointing out the Object INspector, RTTI,
etc. These are all pieces of the puzzle.
If you take away the published keyword, the entire Delphi RAD tool design would require some way to specify which properties are stored in a DFM, inspected in the component property inspector, and can be reloaded at runtime from a DFM when the form or data module is created.
This, in a word, is what Published is for. It is interesting to me that the designers of QT (originally TrollTech, later part of Nokia, later still spun off to Digia) had to emulate this level of RTTI for their C++ RAD library "QT", adding a "published" equivalent and a "property" equivalent, while pure C++ still lacks this fundamental facility.
仅为
已发布
类成员生成运行时类型信息 (RTTI)。Runtime Type Informations (RTTI) are only generated for
published
class members.在运行时,已发布部分和公共部分中的条目同样可以访问。
它们之间的主要区别在于,组件的发布项在设计时出现在对象检查器中。
发生这种情况的原因是,对于已发布部分中的字段,RTTI 是自动生成的。
对象检查器拾取它并使用它来识别要添加到其属性和事件列表中的内容。
At run-time, entries in the published and public sections are equally accessible.
The principal difference between them is that published items of a component appear in the Object Inspector at design-time.
This happens because, for fields in published section RTTI is automatically generated.
The Object Inspector picks this up and uses it to identify what to add to its list of properties and events.
除了其他答案之外:
已发布的属性由流系统自动存储。
例如,如果您有 TComponent 的后代实例并使用 WriteComponent 将其写入 TStream,则所有发布的属性(好吧,不是全部,但这是另一个问题)都将写入流,而无需任何进一步的编码。
当然,流媒体系统只能做到这一点,因为 RTTI 可用于那些已发布的属性。
In addition to the other answers:
Published properties are automatically stored by the streaming system.
For instance if you have a TComponent's descendant instance and write it to a TStream with WriteComponent, all (well, not all, but that is another question) published properties are written to the stream without any further coding.
Of course, the streaming system only can do that because the RTTI is available for those published properties.