1、第9章 索引器,9.1 索引器的定义和使用 9.2 重载索引器 9.3 接口中的索引器 9.4 习题,9.1 索引器的定义和使用,利用属性可以控制类或结构中字段的访问。例:class ArrayClassprivate int arr=new int9;public int Arrget return arr; set arr=value; ,9.1 索引器的定义和使用(续),ArrayClass arrClass=new ArrayClass();int array=arrClass.Arr;/对arrClass对象的字段元素arr1赋值array1=11; /对arrClass对象的字段元
2、素arr2赋值array2=22;,9.1 索引器的定义和使用(续),索引器提供了另一种可以控制类或结构中字段的访问的方案。每次只能访问集合中的一个元素。语法形式为 public int this int indexget return arrindex;setarrindex=value; 索引器只能以this命名,必须使用方括号提供参数。,9.1 索引器的定义和使用(续),ArrayClass arrClass=new ArrayClass();/设置arrClass对象的字段元素arr1的值arrClass1=11; /读取arrClass对象的字段元素arr2的值int i=arrCl
3、ass2; 例9-1 使用索引器,源代码,运 行,using System; class MyIndexer private int arr = new int5; public int thisint index get if (index =0 ,public int Lengthgetreturn arr.Length; class TestIndexer static void Main()MyIndexer myIndexer = new MyIndexer();myIndexer2 = 22;myIndexer4 = 44;for (int i = 0; i = myIndexer
4、.Length; i+)Console.WriteLine(“myIndexer0 = 1“, i, myIndexeri); ,9.1 索引器的定义和使用(续),索引器中可以定义任意个数、任意类型的参数。 索引器并非只能用于控制集合类型字段的访问。例9-2 使用索引器访问离散字段,源代码,运 行,using System; class PhoneBook private string name;private string officePhone;private string homePhone;private string mobilePhone;private string office
5、Fax;public PhoneBook() public PhoneBook(string name)this.name = name;public string Nameget return name; set name = value; ,public string thisstring type get switch (type.ToLower() /方法ToLower将字符串转换成等效的小写形式case “officephone“: return officePhone;case “homephone“: return homePhone;case “mobilephone“: re
6、turn mobilePhone;case “officefax“: return officeFax;default: return null;set switch (type.ToLower()case “officephone“: officePhone = value; break;case “homephone“: homePhone = value; break;case “mobilephone“: mobilePhone = value; break;case “officefax“: officeFax = value; break;,public void PrintPho
7、neBook()Console.WriteLine(name);Console.WriteLine(“办公电话: 0“, officePhone);Console.WriteLine(“家庭电话: 0“, homePhone);Console.WriteLine(“移动电话: 0“, mobilePhone);Console.WriteLine(“办公传真: 0“, officeFax); public class TestPhoneBook public static void Main()PhoneBook phoneBook = new PhoneBook(“王小波“);phoneBoo
8、k“OfficePhone“ = “01012345678“;phoneBook“MOBILEPHONE“ = “13912345678“;phoneBook“officeFax“ = “01087654321“;phoneBook.PrintPhoneBook(); ,9.1 索引器的定义和使用(续),类中的索引器可以声明为虚拟或抽象的。 索引器不可以声明为静态的。 索引器值不可以用作ref或out参数传递。,课堂练习,定义一个类,包含一个二维数组字段,利用索引器访问数组元素,using System; class MyIndexerprivate int, arr = new int3,2
9、;public int thisint index1,int index2get if (index1 = 0 ,class TestIndexer static void Main()MyIndexer myIndexer = new MyIndexer();myIndexer1,1 = 22;myIndexer2,1 = 44;for (int i = 0; i myIndexer.Length0; i+)for (int j = 0; j myIndexer.Length1; j+)Console.WriteLine(“myIndexer0,1 = 2“, i,j, myIndexeri
10、,j); /*/,9.2 重载索引器,通过提供多个参数数量或类型不同的索引器,可以实现类和结构中索引器的重载。例9-3 索引器重载,源代码,运 行,using System; class Cuboid private string names = “长“, “宽“, “高“ ;private double dimensions = new double3;public double thisstring namegetint i = 0;while (i names.Length ,public double thisint index get return (index = 0 ,9.3 接
11、口中的索引器,索引器可以声明在接口中。但接口本身不提供索引器中访问器的实现。语法形式为: interface IArrayint this int indexget;set;,9.3 接口中的索引器(续),在实现接口的类和结构实现索引器中访问器的实现 class ArrayClass:IArrayprivate int arr=new int9;public int this int indexgetreturn arrindex;setarrindex=value;,9.3 接口中的索引器(续),在类中实现接口中的索引器时,可以将索引器声明为virtual。这样,就可以在该类的派生类中重写它
12、。 如果使用显式接口实现语法实现一个索引器,该索引器就不能声明为virtual。 显式实现的索引器不能使用public修饰。例9-4 接口中的索引器,源代码,运 行,using System; public interface IIndexer int thisint indexget;set; class IIndexerClass : IIndexer private int arr = new int5;public int thisint indexgetif (index = 0 ,set if (index = 0 ,9.4 习 题,1.索引器应该命名为什么? this 2.索引器的参数必须是整数类型吗? 不是,可以是任何类型,一般为整型和字符串类型,