C#溫故而知新學習系列之物件導向編程—自動屬性(十一)

來源:互聯網
上載者:User

  閱讀目錄

  一:自動屬性

  二:建立自動屬性

  三:執行個體

  一:自動屬性

    . 當屬性訪問器中不需要其他邏輯時,自動實作屬性可時屬性的聲明變得更加簡潔。

  二:建立自動屬性

    . 一旦在類中聲明了自動屬性,那麼編譯器將建立一個私人的匿名後備欄位,但是這個私人欄位只能通過屬性的get和set訪問器進行訪問。

    . 自動屬性必須同時聲明get和set訪問器,假如要建立唯讀自動屬性,必須在set訪問器前加上privvate關鍵字

  三:執行個體

 1  using System; 2   using System.Collections.Generic; 3   using System.Linq; 4   using System.Text; 5  6   namespace _12_AutoAttribute 7   { 8       class Student 9       {10           public int Age11           {12               get;13               set;14           }15           public bool Sex16           {17               get;18               private set;19           }20       }21 22       class Program23       {24           static void Main(string[] args)25           {26               Student stu = new Student();27               stu.Age = 25;28               Console.WriteLine(stu.Age.ToString());29               Console.ReadKey();30           }31       }32   }

  運行效果

  

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.