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

📄 write-only property.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>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>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; someProperty = propVal;<br>
      &nbsp;&nbsp;&nbsp; }</font><font color="#0000ff" size="2"><br>
      <br>
      &nbsp;&nbsp;&nbsp; public</font><font size="2"> </font><font color="#0000ff" size="2">int</font><font size="2"> 
      SomeProperty<br>
      &nbsp;&nbsp;&nbsp; {<br>
      </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>
      &nbsp;&nbsp;&nbsp; }<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>
      &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(5);<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Property 
      Value: {0}&quot;, propHold.SomeProperty);<br>
      <br>
      </font><font size="2">&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-3 shows how to implement a read-only property.&nbsp; The 
      PropertyHolder class has a SomeProperty property that only implements a 
      get accessor.&nbsp; It leaves out the set accessor.&nbsp; 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.&nbsp; The instantiation of the propHold object uses 
      the constructor of the PropertyHolder that takes an int parameter.&nbsp; 
      In this case, it's set to 5.&nbsp; 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.&nbsp; If you inserted &quot;propHold.SomeProperty = 7&quot; into 
      the listing, the program would not compile, because SomeProperty is 
      read-only.&nbsp; When the SomeProperty property is used in the 
      Console.WriteLine method, it works fine.&nbsp; 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -