Maison > Article > développement back-end > c#属性和索引器
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、总结:
f35d6e602fd7d0f0edfa6f7d103c1b57、
属性的定义:
访问修饰符 返回类型 属性名
{
get{语句集合}
set{语句集合}
}
索引器的定义:
访问修饰符 返回类型 this[参数类型 参数...]
{
get{语句集合}
set{语句集合}
}
2cc198a1d5eb0d3eb508d858c9f5cbdb、
索引器使得对象可按照与数组相似的方法进行索引。
this 关键字用于定义索引器。
get 访问器返回值。set 访问器分配值。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由你决定如何定义特定的查找机制。
索引器可被重载。
5bdf4c78156c7953567bb5a0aef2fc53、属性和索引器的主要区别:
a、类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。
b、属性可以是static(静态的)而索引器则必须是实例成员。
23889872c2e8594e0f446a471a78ec4c、索引器重载实例:
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(); } }
更多c#属性和索引器相关文章请关注PHP中文网!