天天看點

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.