Published in Delphi

The published attribute of a class in Delphi is an attribute that can be seen and used in the attribute list

Published members (published) and public members (public) have the same visibility, but the published statement will be displayed in the property bar, public will not!

    The restricted access attributes of published and public are the same; however, published is generally used in component programming, not often used in applications.

Transfer from OBJECT PASCAL

     "Published members and public members (public) have the same visibility. The difference is that the runtime information RTTI (runtime type information) generated for the published members. RTTI allows applications to dynamically query the domain and attributes of the object, locate the object The method of RTTI is used to access the value of the property in the following situations: when saving and loading the file, when displaying the property in the Object Inspector (Object Inspector), the specified method (ie event handler) and the specified property (ie Event).

    Advertisement attributes are limited to certain data types. Types such as ordinal, string, class, interface, and method pointers can be published; it can be seen that the provided base types can also be published for collection types with ordinal values ​​between 0 and 31 in the upper and lower bounds, that is, the collection must be Byte, word or double word (Byte, Word or Double Word); except Real48, any real number type can be published. Array type properties (unlike array properties mentioned below) cannot be published. "

 

通过RTTI可以获得一个类的所有Published属性
var
mTypeInfo: PTypeInfo;
mTypeData: PTypeData;
mPropList: PPropList;
mPropInfo: PPropInfo;
mPropCount, i: Integer;
begin
mTypeInfo := Form1.ClassInfo;

mTypeData := GetTypeData(mTypeInfo);
if mTypeInfo^.Kind <> tkClass then Exit;

mPropCount := mTypeData^.PropCount;
if mPropCount <= 0 then Exit;

GetMem(mPropList, mPropCount * SizeOf(PPropList));
try
GetPropInfos(mTypeInfo, mPropList);
for i := 0 to mPropCount - 1 do
begin
mPropInfo := mPropList^[i];
ShowMessage(mPropInfo^.Name);
end;
finally
FreeMem(mPropList, mPropCount * SizeOf(PPropList));
end;
end; 

 

 

Under TForm1 = class (TForm), there are more Button1 variables and Button1Click methods, which are actually published types. When the class or parent class uses the "{$ M +}" compilation instruction, the default is the published member. Published members can be seen in the Object Inspector, we can manually add the code to:

type

  TForm1 = class(TForm)

  published

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  private

    procedure PriShowMsg();

    { Private declarations }

  public

    procedure PubShowMsg();

    { Public declarations }

  end;

Define the above method in the implementation:

procedure TForm1.PriShowMsg;

begin

  ShowMessage ('private method call');

end;

 

procedure TForm1.PubShowMsg;

begin

 

ShowMessage ('public method call');

end;

Call the above two methods in the OnClick event method of Button1:

procedure TForm1.Button1Click(Sender: TObject);

 begin

   PriShowMsg;

   PubShowMsg;

end;

Guess you like

Origin www.cnblogs.com/QuincyYi/p/12729143.html