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、示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | { 属性的读取 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 希望内容对你有所帮助,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
2020-08-12 Delphi XE10 错误提示:security alert vmware
2019-08-12 [原创]Delphi 字符串函数(字符串判断 TryStrToFloat 、TryStrToInt、TryStrToInt64、TryStrToBool、TryStrToCurr、TryStrToDate、TryStrToTime、TryStrToDateTime)