教程-Delphi Property详解
<p>http://anony3721.blog.163.com/blog/static/51197420107105132120/?ignoreua</p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">Property
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> Keyword Defines controlled access <span style="color: rgba(0, 0, 255, 1)">to</span> <span style="color: rgba(0, 0, 255, 1)">class</span> fields System <span style="color: rgba(0, 0, 255, 1)">unit</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> ?<span style="color: rgba(128, 0, 128, 1)">1</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">read</span> Getter|<span style="color: rgba(0, 0, 255, 1)">nodefault</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> ?<span style="color: rgba(128, 0, 128, 1)">2</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Setter;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> ?<span style="color: rgba(128, 0, 128, 1)">3</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Setter;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> ?<span style="color: rgba(128, 0, 128, 1)">4</span>.Property Name : Type Index Constant <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default : Constant|nodefault;</span><span style="color: rgba(0, 128, 0, 1)">}</span> <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">stored : Boolean</span><span style="color: rgba(0, 128, 0, 1)">}</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> ?<span style="color: rgba(128, 0, 128, 1)">5</span>.Property Name : Type Index Constant <span style="color: rgba(0, 0, 255, 1)">write</span> Setter <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default : Constant|nodefault;</span><span style="color: rgba(0, 128, 0, 1)">}</span> <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">stored : Boolean</span><span style="color: rgba(0, 128, 0, 1)">}</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> ?<span style="color: rgba(128, 0, 128, 1)">6</span>.Property Name : Type Index Constant <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 0, 255, 1)">write</span> Setter <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default : Constant|nodefault;</span><span style="color: rgba(0, 128, 0, 1)">}</span> <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">stored : Boolean</span><span style="color: rgba(0, 128, 0, 1)">}</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> ?<span style="color: rgba(128, 0, 128, 1)">7</span>.Property Name : BaseType <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default;</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> ?<span style="color: rgba(128, 0, 128, 1)">8</span>.Property Name : BaseType <span style="color: rgba(0, 0, 255, 1)">write</span> Setter; <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default;</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> ?<span style="color: rgba(128, 0, 128, 1)">9</span>.Property Name : BaseType <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 0, 255, 1)">write</span> Setter; <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">default;</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(128, 0, 128, 1)">10</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">read</span> Getter <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Interfaces...;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(128, 0, 128, 1)">11</span>.Property Name; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Redeclared base class property</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(128, 0, 128, 1)">12</span>.Property Name : Type; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Dispinterface only </span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(128, 0, 128, 1)">13</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">readonly</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Dispinterface only </span>
<span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(128, 0, 128, 1)">14</span>.Property Name : Type <span style="color: rgba(0, 0, 255, 1)">writeonly</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Dispinterface only</span></pre>
</div>
<p> </p>
<table style="width: 984px" border="0" cellspacing="0" cellpadding="0" align="center">
<tbody>
<tr valign="top">
<td width="380">
<table border="0" cellspacing="0" cellpadding="7">
<tbody>
<tr>
<td class="title" bgcolor="#fef5b5"><strong>Description</strong></td>
</tr>
<tr bgcolor="#fffeeb">
<td>The <span style="color: rgba(153, 102, 0, 1)"><strong>Property</strong></span> keyword defines a controlled access to class fields. <br> <br>It is an Object Oriented concept that internals of an object should be hidden from the outside. Whilst you can allow fields (data) in a class to be directly externally accessible (by placing in the public or published sections), this is unwise. Instead, <span style="color: rgba(153, 102, 0, 1)"><strong>Property</strong></span> can be used to define how the data is read and written. <br> <br><span style="color: rgba(255, 0, 0, 1)">Versions 1, 2 and 3</span> These basic forms define read, write or read and write access to class fields. The data <strong>Type</strong> is returned from the field or method called <strong>Getter</strong>. The data is updated via the <strong>Setter</strong> field or method. <br> <br>Note that you must use a different name for the <strong>Name</strong> and for <strong>Getter</strong> and <strong>Setter</strong>. For example: <span style="color: rgba(32, 0, 128, 1)">Property Age read fAge write fAge; </span>You would use field names when there is no vetting or retrieval processing required. When using a method to read or write, the read or written value can be a lot simpler than the stored value. The stored value can even be entirely different. <br> <br><span style="color: rgba(255, 0, 0, 1)">Versions 4, 5 and 6</span> Using the <strong>Index</strong> keyword tells Delphi to pass the <strong>Constant</strong> value as the argument to the <strong>Getter</strong> and <strong>Setter</strong> methods. These must be functions that take this constant index value as an argument. <br> <br>For example: <span style="color: rgba(32, 0, 128, 1)">Property Item2 : string Index 2 read ItemGetter; </span>where ItemGetter is defined as : <span style="color: rgba(32, 0, 128, 1)">Function ItemGetter(Index : Integer) : string; </span><strong>Default</strong> provides run time information for the property. <strong>NoDefault</strong> does not. <strong>Stored</strong> is beyond the scope of Delphi Basics. <br> <br><span style="color: rgba(255, 0, 0, 1)">Versions 7, 8 and 9</span> This is a generalised version of versions 4,5 and 6. It requests the user to provide the index value for the Getter and Setter methods. <br> <br><strong>Default</strong> allows the Getter and Setter method calls to be replaced as in the following example : <span style="color: rgba(32, 0, 128, 1)">myValue := MyClass.Getter(23); </span>can be replaced by : <span style="color: rgba(32, 0, 128, 1)">myValue := MyCLass; </span><br> <br><span style="color: rgba(255, 0, 0, 1)">Version 10</span> Allows the implementation of an Interface method to be delegated to a property. Access to the property invokes the interface implementation. <br> <br><span style="color: rgba(255, 0, 0, 1)">Version 11</span> By redclaring a parent class property, you can do so in a public or published clause, thereby raising the access rights of the property. <br> <br><span style="color: rgba(255, 0, 0, 1)">Versions 12, 13 and 14</span> Are relevant for dispinterfaces, outside the scope of delphi basics.</td>
</tr>
<tr>
<td> </td>
</tr>
<tr bgcolor="#dadaf0">
<td class="h2"><strong>Related commands</strong></td>
</tr>
<tr bgcolor="#f9f9ff">
<td>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr valign="top">
<td><strong>Index</strong></td>
<td width="8"> </td>
<td align="left">Principally defines indexed class data properties</td>
</tr>
<tr valign="top">
<td><strong>Private</strong></td>
<td width="8"> </td>
<td align="left">Starts the section of private data and methods in a class</td>
</tr>
<tr valign="top">
<td><strong>Protected</strong></td>
<td width="8"> </td>
<td align="left">Starts a section of class private data accesible to sub-classes</td>
</tr>
<tr valign="top">
<td><strong>Public</strong></td>
<td width="8"> </td>
<td align="left">Starts an externally accessible section of a class</td>
</tr>
<tr valign="top">
<td><strong>Published</strong></td>
<td width="8"> </td>
<td align="left">Starts a published externally accessible section of a class</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div> </div>
<div>
<table style="width: 100%" border="0" cellspacing="0" cellpadding="6">
<tbody>
<tr>
<td class="h1" bgcolor="#bcf6bb"><strong>Example code : Illustrating basic and indexed properties</strong></td>
</tr>
<tr bgcolor="#fafafa">
<td><span style="font-family: "Courier New"; font-size: small"><span style="color: rgba(0, 128, 0, 1)"><strong>// Full Unit code.</strong></span><br><span style="color: rgba(0, 128, 0, 1)"><strong>// -----------------------------------------------------------</strong></span><br><span style="color: rgba(0, 128, 0, 1)"><strong>// You must store this code in a unit called Unit1 with a form</strong></span><br><span style="color: rgba(0, 128, 0, 1)"><strong>// called Form1 that has an OnCreate event called FormCreate.</strong></span><br><br><strong>unit Unit1;</strong><br><br><strong>interface</strong><br><br><strong>uses</strong><br>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>Dialogs;<br><br><strong>type</strong><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Class with Indexed properties</strong></span><br>TRectangle = class<br>private<br>fArea : LongInt;<br>fCoords : array of Longint;<br>function GetCoord(Index: Integer): Longint;<br>procedure SetCoord(Index: Integer; Value: Longint);<br>public<br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Area : Longint read fArea;<br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Left : Longint Index 0 read GetCoord write SetCoord;<br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Top : Longint Index 1 read GetCoord write SetCoord;<br><br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Right : Longint Index 2 read GetCoord write SetCoord;<br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Bottom : Longint Index 3 read GetCoord write SetCoord;<br> <span style="color: rgba(192, 0, 0, 1)"><strong>Property</strong></span> Coords : Longint read GetCoord write SetCoord;<br><br>constructor Create;<br>end;<br><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// The form class itself</strong></span><br>TForm1 = class(TForm)<br>procedure FormCreate(Sender: TObject);<br>end;<br><br><strong>var</strong><br>Form1: TForm1;<br><br><strong>implementation</strong><br><br><strong>{$R *.dfm}</strong><br><br><span style="color: rgba(0, 128, 0, 1)"><strong>// TRectangle property 'Getter' routine</strong></span><br><span style="color: rgba(0, 128, 0, 1)"><strong>// TRectangle constructor</strong></span><br><strong>constructor TRectangle.Create;</strong><br><strong>begin</strong><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Give default rectangle coordinates</strong></span><br>left := 0;<br>right := 100;<br>top := 0;<br>bottom := 100;<br>fArea := 100 * 100;<br><strong>end;</strong><br><br><strong>function TRectangle.GetCoord(Index: Integer): Longint;</strong><br><strong>begin</strong><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Only allow valid index values</strong></span><br>if (Index >= 0) and (Index <= 3)<br>then Result := fCoords<br>else Result := -1;<br><strong>end;</strong><br><br><span style="color: rgba(0, 128, 0, 1)"><strong>// TRectangle property 'Setter' routine</strong></span><br><strong>procedure TRectangle.SetCoord(Index, Value: Integer);</strong><br><strong>begin</strong><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Only allow valid index values</strong></span><br>if (Index >= 0) and (Index <= 3)<br>then<br>begin<br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Save the new value</strong></span><br>fCoords := Value;<br><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// And recreate the rectangle area</strong></span><br>fArea := (right - left) * (bottom - top);<br>end;<br><strong>end;</strong><br><br><span style="color: rgba(0, 128, 0, 1)"><strong>// Main line code</strong></span><br><strong>procedure TForm1.FormCreate(Sender: TObject);</strong><br><strong>var</strong><br>myRect : TRectangle;<br>i : Integer;<br><br><strong>begin</strong><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// Create my little rectangle with default coordinates</strong></span><br>myRect := TRectangle.Create;<br><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// And set the corner coordinates</strong></span><br>myRect.Left := 22; <span style="color: rgba(0, 128, 0, 1)"><strong>// Left using direct method</strong></span><br>myRect.Top := 33;<br>myRect.SetCoord(2,44); <span style="color: rgba(0, 128, 0, 1)"><strong>// Right using indexed method</strong></span><br>myRect.SetCoord(3,55);<br><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// And ask for these values</strong></span><br>for i:= 0 to 3 do<br>ShowMessageFmt('myRect coord %d = %d',);<br><br> <span style="color: rgba(0, 128, 0, 1)"><strong>// And show the rectangle area</strong></span><br>ShowMessageFmt('myRect area = %d',);<br><strong>end;</strong><br><br><strong>end.</strong><br></span></td>
</tr>
<tr bgcolor="#d2eccf">
<td align="right"><span style="font-family: Arial, Helvetica, sans-serif; font-size: xx-small">Show full unit code</span></td>
</tr>
<tr bgcolor="#d2eccf">
<td><span style="font-family: "Courier New"; font-size: small">myRect coord 0 = 22<br>myRect coord 1 = 33<br>myRect coord 2 = 44<br>myRect coord 3 = 55<br>myRect area = 484</span></td>
</tr>
</tbody>
</table>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div id="AllanboltSignature">
<div>作者:疯狂Delphi</div>
<div>出处:https://www.cnblogs.com/FKdelphi/</div>
<div>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.</div>
</div>
<div class="div_masklayer" id="div_masklayer"></div>
<div class="div_popup" id="Div_popup">
<p class="mid">欢迎关注我,一起进步!扫描下方二维码即可加我</p>
<img class="img_zfb" id="img_zfb" width="150" src="https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_251208113944_QQ.png">
<img class="img_zfb" id="img_zfb" width="150" src="https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_251208115029_WX.png">
</div><br><br>
来源:https://www.cnblogs.com/FKdelphi/p/10836623.html
頁:
[1]