⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accessing class fields with properties.htm

📁 点net代码20个
💻 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>
      &nbsp;&nbsp;&nbsp; {<br>
      </font><font size="2">&nbsp;</font><font color="#0000ff" size="2">get</font><br>
      &nbsp;<font size="2">{<br>
      </font><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff" size="2">return</font><font size="2"> 
      someProperty;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
      </font><font size="2">&nbsp;</font><font color="#0000ff" size="2">set</font><br>
      &nbsp;<font size="2">{<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      someProperty = </font><font color="#0000ff" size="2">value</font><font size="2">;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
      &nbsp;&nbsp;&nbsp; }<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>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PropertyHolder propHold = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      PropertyHolder();<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; propHold.SomeProperty = 5;<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Property 
      Value: {0}&quot;, propHold.SomeProperty);<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff" size="2">return</font><font size="2"> 
      0;<br>
      &nbsp;&nbsp;&nbsp; }<br>
      }</font></td>
  </tr>
  <tr>
    <td>Listing 10-2 shows how to create and use a property.&nbsp; The 
      PropertyHolder class has the &quot;SomeProperty&quot; property 
      implementation.&nbsp; Notice that the first letter of the first word is 
      capitalized.&nbsp; That's the only difference between the names of the 
      property &quot;SomeProperty&quot; and the field &quot;someProperty&quot;.&nbsp; 
      The property has two accessors, get and set.&nbsp; The get accessor 
      returns the value of the someProperty field.&nbsp; The set accessor sets 
      the value of the someProperty field with the contents of 
      &quot;value&quot;.&nbsp; The &quot;value&quot; shown in the set accessor 
      is a C# reserved word.&nbsp; It's normally an error to use the 
      &quot;value&quot; keyword in any other context.</td>
  </tr>
  <tr>
    <td>The PropertyTester class uses the SomeProperty property in the 
      PropertyHolder class.&nbsp; The first line of the Main method creates a 
      PropertyHolder object named propHold.&nbsp; Next the value of the 
      someProperty field of the propHold object is set to 5 by using the 
      SomeProperty property.&nbsp; 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.&nbsp; It does this by using the 
      SomeProperty property of the propHold object.&nbsp; 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.&nbsp; 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 + -