Delphi 系统[22]关键字和保留字 read、write、default、nodefault、readonly、writeonly、stored、message

Delphi 系统[22]关键字和保留字  read、write、default、nodefault、readonly、writeonly、stored、message - 定义属性的参数

1、定义:

  • read :用于标识属性读取时所使用的成员或方法。 
  • write :用于标识属性写入时所使用的成员或方法。 
  • default :指示属性的默认值,或指示一个属性为类的默认属性。 只有有序类型的属性才允许默认值的存在, 否则必须在构造函数中初始化属性值。 
  • nodefault :指示一个属性不允许有默认值,这通常用在继承中。 
  • readonly :指示一个属性为只读。 当readonly 设为 True 时, 不允许用户修改属性, 只能通过其他对象来操作 
  • writeonly :指示一个属性为只写。当writeonly 设为 true 时,不允许用户读取属性,只能通过其他对象来操作  
  • stored :指示一个属性的值是否能被保留,若指定了True,则允许对属性值进行赋值撤销的操作。  
  • message :用于声明消息方法。带有 message 的方法必须指出接收的消息类型,并通过引用将消息传入方法中,以便进行处理。用户可以自定义消息,自定义消息也能够被 message 接收,并引发事件。   

2、示例:

{ 属性的读取 read } 
type 
  TMyObject = class(TObject) 
  private 
    FValue: Integer; 
  published 
    property Value: Integer read FValue; { 表明 Value 属性从FValue 成员上读出值 } 
  end; 
 
---------------------------------------------------------------------------------------- 
{ 属性的写入write } 
type 
  TMyObject = class(TObject) 
  private 
    FValue: Integer; 
  published 
    property Value: Integer write FValue;  { 表明 Value 属性的值写入到 FValue 成员上 } 
  end; 
 
--------------------------------------------------------------------------------------- 
{ 默认值和默认属性 default} 
type 
  TMyObject = class(TObject) 
  private 
    FAuto: Boolean; 
    FCount: Integer; 
    FNameList: TStrings; 
  public 
    constructor Create; 
    { 属性默认值 default True、default 0 } 
    property Auto: Boolean read FAuto write FAuto default True; 
    property Count: Integer read FCount write FCount default 0; 
    { 默认属性 default } 
    property Names[Index: Integer]: TStrings read FNameList write FNameList default; 
  end; 
 
constructor TMyObject.Create; 
begin 
  inherited; 
  FNameList := TStrings.Create;   { 分配对象资源 } 
  FAuto := True;  { 设置属性默认值 } 
end; 
 
---------------------------------------------------------------------------------------- 
{ 去掉默认值 nodefault} 
type 
  TMyObjA = class 
  private 
    FValue: Integer; 
  published 
    property Value: Integer read FValue write FValue default 0; 
  end; 
 
  TMyObjB = class(TMyObjA) 
  published 
    property Value: Integer read FValue write FValue nodefault; 
  end; 
 
{ 由上例可知, TMyObjA 中的 Value 有默认值 0,TMyObjB 继承了 TMyObjA,所以也继承 了其默认值, 在此用 NoDefault 去掉默认值。 } 
 
----------------------------------------------------------------------------------------
{ 只读属性 } 
property ReadOnly; 
 
---------------------------------------------------------------------------------------- 
{ 只写属性 } 
property WriteOnly; 
 
---------------------------------------------------------------------------------------- 
{ 保留属性值 stored} 
type 
  TComponent = class 
  private 
    FName: TComponentName; 
  published 
    property Name: TComponentName read FName write SetName stored False; 
  end; 
 
---------------------------------------------------------------------------------------- 
{ 声明消息方法 message} 
unit Form1Unit; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants,Classes, Graphics, Controls, Forms, StdCtrls; 
 
type 
  TForm1 = class(TForm) 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
    procedure Refresh(var Msg: TMessage); message WM_SIZE; 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
{R *.dfm}

{ 此方法捕捉窗口尺寸被改变的消息 }
procedure TForm1.Refresh(var Msg: TMessage);
begin
  { 先将窗口的尺寸显示在标题栏中 }
  Caption := IntToStr(Width) + ' - ' +  IntToStr(Height);
  { 再调用默认消息处理函数,重绘窗口 }
  inherited;
end;

{ 随机调整窗口的大小 }
procedure TForm1.Button1Click(Sender: TObject);
var
  Size: Integer;
begin
  { 先将按钮自身移到窗口左上角,以免窗口缩小后被遮挡 }
  (Sender as TButton).Left := 0;
  (Sender as TButton).Top := 0;

  { 获取一个随机数,可正可负 }
  Randomize;
  Size := Random(100) - 50;
  { 设置窗口的新大小 }
  Width := Width + Size;
  Height := Height + Size;
  { 当窗口大小改变后,就会触发 WM_SIZE 消息,从而调用我们定义的TForm1.Refresh }
end;

end.

  

创建时间:2021.08.12  更新时间:

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/15132581.html