索引器应用

  1. /**  
  2.  * 索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。被称为有参属性。  
  3.  * 索引器与属性的比较:  
  4.  * □标示方式:属性以名称来标识,索引器以函数签名来标识。  
  5.  * □索引器可以被重载。属性则不可以被重载。  
  6.  * □属性可以为静态的,索引器属于实例成员,不能被声明为static   
  7.  *   
  8.  * 备注:  
  9.  * □所有索引器都使用this关键词来取代方法名。Class或Struct只允许定义一个索引器,而且总是命名为this。   
  10.  * □索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。   
  11.  * □get 访问器返回值。set 访问器分配值。   
  12.  * □this 关键字用于定义索引器。   
  13.  * □value 关键字用于定义由 set 索引器分配的值。   
  14.  * □索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。   
  15.  * □索引器可被重载。   
  16.  * □索引器可以有多个形参,例如当访问二维数组时。   
  17.  * □索引器可以使用百数值下标,而数组只能使用整数下标:如下列定义一个String下标的索引器   
  18.  * □public int this [string name] {...}   
  19.  * □属性和索引器   
  20.  *   ○属性和索引器之间有好些差别:   
  21.  *   ○类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。   
  22.  *   ○属性可以是static(静态的)而索引器则必须是实例成员。  
  23.  */  
  24.   
  25. using System;   
  26. using System.Collections.Generic;   
  27. using System.Linq;   
  28. using System.Text;   
  29. using System.Collections;   
  30.   
  31. namespace IndexExample   
  32. {   
  33.     /// <summary>   
  34.     /// 多参数索引器实例   
  35.     /// </summary>   
  36.     class Program   
  37.     {   
  38.         static void Main(string[] args)   
  39.         {   
  40.             ScoreIndex si = new ScoreIndex();   
  41.             si["张三", 1] = 90;   
  42.             si["张三", 2] = 100;   
  43.             si["张三", 3] = 80;   
  44.             si["李四", 1] = 60;   
  45.             si["李四", 2] = 70;   
  46.             si["李四", 3] = 50;   
  47.             Console.WriteLine("张三的课程号为1的成绩为:{0}", si["张三", 1]);   
  48.   
  49.             Console.WriteLine("张三的所有课程成绩是:");   
  50.             ArrayList arr;   
  51.             arr = si["张三"];   
  52.             foreach (IndexClass ic in arr)   
  53.             {   
  54.                 Console.WriteLine("课程编号:" + ic.CourseId + "\t分数:" + ic.Score);   
  55.             }   
  56.   
  57.             Console.Read();   
  58.         }   
  59.     }   
  60.   
  61.     class IndexClass   
  62.     {   
  63.         private string _name;   
  64.         private int _courseId;   
  65.         private int _score;   
  66.   
  67.         public IndexClass(string _name, int _courseId, int _score)   
  68.         {   
  69.             this._name = _name;   
  70.             this._courseId = _courseId;   
  71.             this._score = _score;   
  72.         }   
  73.   
  74.         public string Name   
  75.         {   
  76.             get  
  77.             {   
  78.                 return this._name;   
  79.             }   
  80.             set  
  81.             {   
  82.                 this._name = value;   
  83.             }   
  84.         }   
  85.   
  86.         public int CourseId   
  87.         {   
  88.             get  
  89.             {   
  90.                 return this._courseId;   
  91.             }   
  92.             set  
  93.             {   
  94.                 this._courseId = value;   
  95.             }   
  96.         }   
  97.   
  98.         public int Score   
  99.         {   
  100.             get  
  101.             {   
  102.                 return this._score;   
  103.             }   
  104.             set  
  105.             {   
  106.                 this._score = value;   
  107.             }   
  108.         }   
  109.     }   
  110.   
  111.     class ScoreIndex   
  112.     {   
  113.         private ArrayList arr;   
  114.   
  115.         public ScoreIndex()   
  116.         {   
  117.             arr = new ArrayList();   
  118.         }   
  119.   
  120.         public int this[string _name, int _courseId]   
  121.         {   
  122.             get  
  123.             {   
  124.                 foreach (IndexClass ic in arr)   
  125.                 {   
  126.                     if (ic.Name == _name && ic.CourseId == _courseId)   
  127.                     {   
  128.                         return ic.Score;   
  129.                     }   
  130.                 }   
  131.                 return -1;   
  132.             }   
  133.             set  
  134.             {   
  135.                 arr.Add(new IndexClass(_name, _courseId, value));   
  136.             }   
  137.         }   
  138.   
  139.         public ArrayList this[string _name]   
  140.         {   
  141.             get  
  142.             {   
  143.                 ArrayList tmp = new ArrayList();   
  144.                 foreach (IndexClass ic in arr)   
  145.                 {   
  146.                     if (ic.Name == _name)   
  147.                         tmp.Add(ic);   
  148.                 }   
  149.                 return tmp;   
  150.             }   
  151.         }   
  152.     }   
  153. }  

 

来源:http://shansun123.javaeye.com/blog/393674

posted @ 2010-01-02 11:45  老王博客  阅读(166)  评论(0)    收藏  举报