c#索引器

来源:互联网 发布:开淘宝店铺货源怎么找 编辑:程序博客网 时间:2024/05/17 00:07
[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             School mc = new School("高三一班");  
  13.             mc.Students["b"].say();  
  14.             mc.Students[1].say();  
  15.   
  16.             Console.ReadKey();  
  17.         }  
  18.     }  
  19.   
  20.     class Student  
  21.     {  
  22.         Student[] stus = new Student[3];  
  23.         public Student(string _name, int _age)  
  24.         {  
  25.             this.Name = _name;  
  26.             this.Age = _age;  
  27.         }  
  28.         public Student()  
  29.         {  
  30.             stus[0] = new Student("a", 21);  
  31.             stus[1] = new Student("b", 33);  
  32.             stus[2] = new Student("c", 44);  
  33.         }  
  34.         public void say()  
  35.         {  
  36.             Console.WriteLine("大家好,我叫{0},今年{1}岁!", name, age);  
  37.         }  
  38.   
  39.         private string name;  
  40.         public string Name  
  41.         {  
  42.             get { return name; }  
  43.             set { name = value; }  
  44.         }  
  45.   
  46.         private int age;  
  47.         public int Age  
  48.         {  
  49.             get { return age; }  
  50.             set { age = value; }  
  51.         }  
  52.   
  53.   
  54.         public Student this[int index]  
  55.         {  
  56.             get { return stus[index]; }  
  57.         }  
  58.   
  59.         public Student this[string name]  
  60.         {  
  61.             get  
  62.             {  
  63.                 int j = -1;  
  64.                 bool equalse = false;  
  65.                 for (int i = 0; i < stus.Length; i++)  
  66.                 {  
  67.                     if (stus[i].name == name)  
  68.                     {  
  69.                         j = i;  
  70.                         equalse = true;  
  71.                         break;  
  72.                     }  
  73.                 }  
  74.                 if (equalse)  
  75.                 {  
  76.                     return stus[j];  
  77.                 }  
  78.                 else  
  79.                 {  
  80.                     return null;  
  81.                 }  
  82.             }  
  83.         }  
  84.     }  
  85.   
  86.     class School  
  87.     {  
  88.         //构造函数  
  89.         public School(string name)  
  90.         {  
  91.             this.Name = name;  
  92.             this.Students = new Student();  
  93.         }  
  94.   
  95.         //班级名字  
  96.         private string name;  
  97.         public string Name  
  98.         {  
  99.             get { return name; }  
  100.             set { name = value; }  
  101.         }  
  102.   
  103.         //班级学生  
  104.         private Student students;  
  105.         public Student Students  
  106.         {  
  107.             get { return students; }  
  108.             set { students = value; }  
  109.         }  
  110.     }  


0 0
原创粉丝点击