What's the difference between public and published class members in Delphi?

浪子不回头ぞ 提交于 2019-12-05 08:40:28

问题


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.


回答1:


Public properties and published properties have the same visibility, as you already stated. Published properties are included in RTTI, public properties aren't.




回答2:


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

public
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will not be visible in the Object Inspector.

Published

published
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will be visible in the Object Inspector.




回答3:


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:

  TTopSecret = class(TObject)
    Name: string;
    Password: string;

    function DecryptPassword(const AValue): string;  
  public
    constructor Create(const AName, AEncryptedPassword: string);
  end; 

Name, Password and DecryptPassword() are visible 'world-wide'.




回答4:


Published properties will export Runtime Type Information (RTTI).

Have a look here about RTTI in Delphi




回答5:


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.




回答6:


Runtime Type Informations (RTTI) are only generated for published class members.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/3157648/whats-the-difference-between-public-and-published-class-members-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!