write-only property.htm
来自「点net代码20个」· HTM 代码 · 共 93 行
HTM
93 行
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Write</title>
</head>
<body>
<table border="1" cellspacing="0" width="596">
<tr>
<td align="center"><b><font color="#FF0000" size="4">Write-Only Property</font></b></td>
</tr>
<tr>
<td>
<p><font color="#0000ff" size="2">using</font><font size="2"> System;<br>
<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2">
PropertyHolder<br>
{<br>
</font><font color="#0000ff" size="2">private</font><font size="2"> </font><font color="#0000ff" size="2">int</font><font size="2">
someProperty = 0;<br>
<br>
</font><font color="#0000ff" size="2">public</font><font size="2">
PropertyHolder(</font><font color="#0000ff" size="2">int</font><font size="2">
propVal)<br>
{<br>
someProperty = propVal;<br>
}</font><font color="#0000ff" size="2"><br>
<br>
public</font><font size="2"> </font><font color="#0000ff" size="2">int</font><font size="2">
SomeProperty<br>
{<br>
</font><font color="#0000ff" size="2">get</font><br>
<font size="2">{<br>
</font><font size="2"> </font><font color="#0000ff" size="2">return</font><font size="2">
someProperty;<br>
}<br>
}<br>
}<br>
<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2">
PropertyTester<br>
{<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2">
</font><font color="#0000ff" size="2">int</font><font size="2"> Main(</font><font color="#0000ff" size="2">string</font><font size="2">[]
args)<br>
{<br>
PropertyHolder propHold = </font><font color="#0000ff" size="2">new</font><font size="2">
PropertyHolder(5);<br>
<br>
Console.WriteLine("Property
Value: {0}", propHold.SomeProperty);<br>
<br>
</font><font size="2"> </font><font color="#0000ff" size="2">return</font><font size="2">
0;<br>
}<br>
}</font></td>
</tr>
<tr>
<td>Listing 10-3 shows how to implement a read-only property. The
PropertyHolder class has a SomeProperty property that only implements a
get accessor. It leaves out the set accessor. This particular
PropertyHolder class has a constructor which accepts an integer parameter.</td>
</tr>
<tr>
<td>The Main method of the PropertyTester class creates a new PropertyHolder
object named propHold. The instantiation of the propHold object uses
the constructor of the PropertyHolder that takes an int parameter.
In this case, it's set to 5. This initializes the someProperty field
of the propHold object to 5.</td>
</tr>
<tr>
<td>Since the SomeProperty property of the PropertyHolder class is
read-only, there is no other way to set the value of the someProperty
field. If you inserted "propHold.SomeProperty = 7" into
the listing, the program would not compile, because SomeProperty is
read-only. When the SomeProperty property is used in the
Console.WriteLine method, it works fine. This is because it's a read
operation which only invokes the get accessor of the SomeProperty
property.</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?