📄 example of traditional class field access.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>Example of Traditional Class Field Access</title>
</head>
<body background="../basic3.gif">
<table border="0" cellspacing="0" width="596">
<tr>
<td>
<p align="center"><font color="#FF0000" size="4"><b>Example of Traditional
Class Field Access</b></font></td>
</tr>
<tr>
<td>
<p><font color="#0000ff" size="2">using</font><font size="2"> System;<br>
</font><font color="#0000ff" size="2"><br>
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"> </font><font size="2">
</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"> </font><font size="2">
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">int</font><font size="2">
getSomeProperty()<br>
{<br>
</font> <font color="#0000ff" size="2">
return</font><font size="2"> someProperty;<br>
}<br>
</font><font color="#0000ff" size="2"><br>
public</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2">
setSomeProperty(</font><font color="#0000ff" size="2">int</font><font size="2">
propValue)<br>
{<br>
someProperty = propValue;<br>
}<br>
</font><font color="#0000ff" size="2"><br>
</font><font size="2">}<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"> </font><font size="2">
</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.setSomeProperty(5);<br>
<br>
Console.WriteLine("Property
Value: {0}", propHold.getSomeProperty());<br>
<br>
</font><font color="#0000ff" size="2">return</font><font size="2">
0;<br>
}<br>
}</font></td>
</tr>
<tr>
<td>Listing 10-1 shows the traditional method of accessing class fields.
The PropertyHolder class has the field we're interested in accessing.
It has two methods, getSomeProperty and setSomeProperty. The
getSomeProperty method returns the value of the someProperty field.
The setSomeProperty method sets the value of the someProperty field.</td>
</tr>
<tr>
<td>The PropertyTester class uses the methods of the PropertyHolder class to
get the value of the someProperty field in the PropertyHolder class.
The Main method instantiates a new PropertyHolder object, propHold.
Next it sets the someMethod of propHold to the value 5 by using the
setSomeProperty method. Then the program prints out the property
value with a Console.WriteLine method call. The argument used to
obtain the value of the property is a call to the getSomeProperty method
of the propHold object. It prints out "Property Value: 5"
to the console.</td>
</tr>
<tr>
<td>This method of accessing information in a field has been good because it
supports the object-oriented concept of encapsulation. If the
implementation of someProperty changed from an int type to a byte type,
this would still work. Now the same thing can be accomplished much
smoother with properties.</td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -