1、屬性
所謂屬性其實就是特殊的類別成員,它實作了對私有類別域的受控存取。在C#語言中有兩種屬性方法,其一是get,透過它可以傳回私有域的值,其二是set,透過它就可以設定私有域的值。比如說,以下面的程式碼為例,建立學生姓名屬性,控制對name欄位的受控存取:
using System; public class Student { private string name; /// <summary> /// 定义学生的姓名属性 /// </summary> public string Name { get { return name; } set { name = value; } } } class Program { static void Main(string[] args) { Student student = new Student(); student.Name = "Jeff Wong"; Console.WriteLine(student.Name); Console.Read(); } }
2、索引器
簡單說來,所謂索引器就是一類特殊的屬性,透過它們你就可以像引用數組一樣引用自己的類別。顯然,這項功能在創建集合類別的場合特別有用,而在其他某些情況下,例如處理大型檔案或抽象某些有限資源等,能讓類別具有類似陣列的行為當然也是非常有用的。例如,上例中,我們假設一個班級有若干個學生,建構索引器就可以很方便地呼叫:
using System; using System.Collections.Generic; public class Student { public List<Student> listStudents = new List<Student>(); /// <summary> /// 构建索引器 /// </summary> /// <param name="i"></param> /// <returns></returns> public Student this[int i] { get { return listStudents[i]; } set { listStudents[i] = value; } } private string name; /// <summary> /// 属性 /// </summary> public string Name { get { return name; } set { name = value; } } public Student(string name) { this.name = name; } public Student() { this.listStudents.Add(new Student("jeff wong")); this.listStudents.Add(new Student("jeffery zhao")); this.listStudents.Add(new Student("terry lee")); this.listStudents.Add(new Student("dudu")); } } class Program { static void Main(string[] args) { Student student = new Student(); int num = student.listStudents.Count; Console.WriteLine("All the students:"); for (int i = 0; i < num; i++) { Console.WriteLine(student[i].Name); //通过索引器,取所有学生名 } //设置索引器的值 student[0].Name = "jeff"; Console.WriteLine("After modified,all the students:"); for (int i = 0; i < num; i++) { Console.WriteLine(student[i].Name); } Console.Read(); } }
上面程式碼中,我們看到索引器的存取器帶一個參數(參數為整數),其實可以建構多個參數的索引器。也以上述代碼為例,我們要根據學生學號和姓名得到學生的考試總分,修改後代碼如下:
using System; using System.Collections.Generic; public class Student { public List<Student> listStudents = new List<Student>(); public Student this[int i,string name] { get { foreach (Student stu in listStudents.ToArray()) { if (stu.sid == i && stu.name == name) //按照学号和姓名取出学生 { return stu; } } return null; } set { listStudents[i] = value; } } private int sid; //学号 public int Sid { get { return sid; } set { sid = value; } } private string name;//姓名 public string Name { get { return name; } set { name = value; } } private int score; //总分 public int Score { get { return score; } set { score = value; } } public Student(int sid, string name, int score) { this.sid = sid; this.name = name; this.score = score; } public Student() { this.listStudents.Add(new Student(1, "jeff wong", 375)); this.listStudents.Add(new Student(2,"jeffery zhao",450)); this.listStudents.Add(new Student(3,"terry lee",400)); this.listStudents.Add(new Student(4,"dudu",500)); } } class Program { static void Main(string[] args) { Student student = new Student(); Student stu = student[1, "jeff wong"]; Console.WriteLine("student number:" + stu.Sid + ",name:" + stu.Name + ",score:" + stu.Score); Console.Read(); } }
3、總結:
<1>、
屬性的定義:
訪問修飾符傳回型別屬性名稱
{
get{語句集合}
get{語句集合}
get{語句集合}
set{語句集合}
索引器使得物件可依與陣列相似的方法索引。
this 關鍵字用於定義索引器。
get 訪問器傳回值。 set 存取器指派值。
索引器不必根據整數值進行索引,由你決定如何定義特定的查找機制。
索引器可被重載。
a、類別的每個屬性都必須擁有唯一的名稱,而類別裡定義的每一個索引器都必須擁有唯一的簽章(signature)或參數清單(這樣就可以實作索引器重載)。
b、屬性可以是static(靜態的)而索引器則必須是實例成員。
<4>、索引器重載實例:
using System; using System.Collections.Generic; public class Student { public List<Student> listStudents = new List<Student>(); public Student this[int i,string name] { get { foreach (Student stu in listStudents.ToArray()) { if (stu.sid == i && stu.name == name) //按照学号和姓名取出学生 { return stu; } } return null; } set { listStudents[i] = value; } } /// <summary> /// 索引器重载 /// </summary> /// <param name="i"></param> /// <returns></returns> public Student this[int i] //i从0开始 { get { return listStudents[i]; } set { listStudents[i] = value; } } private int sid; //学号 public int Sid { get { return sid; } set { sid = value; } } private string name;//姓名 public string Name { get { return name; } set { name = value; } } private int score; //总分 public int Score { get { return score; } set { score = value; } } public Student(int sid, string name, int score) { this.sid = sid; this.name = name; this.score = score; } public Student() { this.listStudents.Add(new Student(1, "jeff wong", 375)); this.listStudents.Add(new Student(2,"jeffery zhao",450)); this.listStudents.Add(new Student(3,"terry lee",400)); this.listStudents.Add(new Student(4,"dudu",500)); } } class Program { static void Main(string[] args) { Student student = new Student(); Student stu = student[1, "jeff wong"]; Console.WriteLine("student number:" + stu.Sid + ",name:" + stu.Name + ",score:" + stu.Score); Console.WriteLine("all the students:"); for (int i = 0; i < student.listStudents.Count; i++) { Console.WriteLine("student number:" + student[i].Sid + ",name:" + student[i].Name + ",score:" + student[i].Score); } Console.Read(); } }