在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
所谓属性其实就是特殊的类成员,它实现了对私有类域的受控访问。在C#语言中有两种属性方法,其一是get,通过它可以返回私有域的值,其二是set,通过它就可以设置私有域的值。比如说,以下面的代码为例,创建学生姓名属性,控制对name字段的受控访问:
Student
2、索引器{ 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(); } } 简单说来,所谓索引器就是一类特殊的属性,通过它们你就可以像引用数组一样引用自己的类。显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。比如,上例中,我们假设一个班级有若干个学生,构建索引器就可以很方便地调用:
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(); } }
Student
3、总结:{ 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(); } } <1>、 属性的定义: { 索引器的定义: 访问修饰符 返回类型 this[参数类型 参数...] 索引器使得对象可按照与数组相似的方法进行索引。
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(); } } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论