📄 accessing class fields with properties.htm
字号:
<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>Accessing Class Fields With Properties</title>
</head>
<body background="../basic3.gif">
<table border="0" cellspacing="0" width="596">
<tr>
<td>
<p align="center"><font color="#FF0000" size="4"><b>Accessing Class Fields
With Properties</b></font></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><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"> </font><font color="#0000ff" size="2">int</font><font size="2">
SomeProperty<br>
{<br>
</font><font size="2"> </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>
</font><font size="2"> </font><font color="#0000ff" size="2">set</font><br>
<font size="2">{<br>
someProperty = </font><font color="#0000ff" size="2">value</font><font size="2">;<br>
}<br>
}<br>
}<br>
</font><font color="#0000ff" size="2"><br>
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();<br>
<br>
propHold.SomeProperty = 5;<br>
<br>
Console.WriteLine("Property
Value: {0}", propHold.SomeProperty);<br>
<br>
</font><font color="#0000ff" size="2">return</font><font size="2">
0;<br>
}<br>
}</font></td>
</tr>
<tr>
<td>Listing 10-2 shows how to create and use a property. The
PropertyHolder class has the "SomeProperty" property
implementation. Notice that the first letter of the first word is
capitalized. That's the only difference between the names of the
property "SomeProperty" and the field "someProperty".
The property has two accessors, get and set. The get accessor
returns the value of the someProperty field. The set accessor sets
the value of the someProperty field with the contents of
"value". The "value" shown in the set accessor
is a C# reserved word. It's normally an error to use the
"value" keyword in any other context.</td>
</tr>
<tr>
<td>The PropertyTester class uses the SomeProperty property in the
PropertyHolder class. The first line of the Main method creates a
PropertyHolder object named propHold. Next the value of the
someProperty field of the propHold object is set to 5 by using the
SomeProperty property. It's that simple -- just assign the value to
the property as if it were a field.</td>
</tr>
<tr>
<td>After that, the Console.WriteLine method prints the value of the
someProperty field of the propHold object. It does this by using the
SomeProperty property of the propHold object. Again, it's that
simple -- just use the property as if it were a field itself.</td>
</tr>
<tr>
<td>Properties can be made read-only. This is accomplished by having
only a get accessor in the property implementation.</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -