欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net get set用法小結(jié)第2/3頁

 更新時間:2007年12月12日 21:27:16   作者:  

以下是一個簡單的例子,演示了屬性的基本形式和用法:
using System;
using System.Collections.Generic;
using System.Text; 
namespace 屬性的用法
{
    public class Student
    {
        private string stuName = "阿會楠";
        public string studentName
        {
            get { return stuName; }
            set { stuName = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            Console.Write(stu.studentName);
            Console.ReadKey();
        }
    }
}

       上面代碼中定義了一個屬性studentName,它包含get訪問器和set訪問器。屬性studentName封裝了類Student中的字段stuName,字段如果沒有加訪問控制符,被默認為private,外界不能直接訪問它,現(xiàn)在外界可以通過studentName屬性自由地存取stuName字段了。

       屬性的get和set都是可執(zhí)行的程序語句組合,具有行為的特點;而使用具有get訪問器和set訪問器的屬性時候就像使用字段一樣,即可以作為左值接受數(shù)據(jù),又可以作為右值輸出數(shù)據(jù),系統(tǒng)正是按照屬性出現(xiàn)在語句中的位置,自動地選擇是調(diào)用get還是調(diào)用set。

相關文章

最新評論