日期:2008-6-14
學習內容:索引器
遺留問題:
學習總結:
1. 索引器
索引器(indexer):索引器允許類或結構的實例按照與數組相同的方式進行索引。索引器類似與屬性,不同之處在於它們的訪問器採用參數。
數組類的使用實例:
using System;//引用System命名空間
class Arrclass
{
private readonly string name;
public Arrclass(string name)//Arrclass的構造函數,並且帶有參數name
{
this.name = name;
}
public string Name //定義一個只讀訪問器Name
{
get
{
return name;
}
}
}
class Test
{
static void Main ()
{
Arrclass[] a=new Arrclass[10];//定義一個存儲Arrclass類實例的數組,並且指定長度爲個元素
a[0]=new Arrclass("張三");//利用數組下標索引,給數組賦值
a[1]=new Arrclass("李四");
a[2]=new Arrclass("王五");
Console.WriteLine("a[0]="+a[0].Name);//輸出Arrclass類實例的數組中元素的值
Console.WriteLine("a[1]="+a[1].Name);
Console.WriteLine("a[2]="+a[2].Name);
}
}
索引器的使用實例:
using System;//引用System命名空間
class Indexclass
{
private string[] name = new string[10];//定義一個字符型數組,數組的長度爲,把它作爲類的一個私有成員
public string this[int index]//定義索引器,它有點像屬性,只不過是帶參數
{
get
{
return name[index];
}
set
{
name[index] = value;
}
}
}
class Test
{
static void Main ()
{
Indexclass b = new Indexclass();//聲明一個Indexclass類的實例
b[0] = "張三";//爲實例賦值
b[1] = "李四";
b[2] = "王五";
Console.WriteLine("b[0]=" + b[0]);
Console.WriteLine("b[1]=" + b[1]);
Console.WriteLine("b[2]=" + b[2]);
}
}
索引器除了索引值爲整型外,還可以爲其他類型,用其他類型的索引值,需要用到HashTable 類
在.net framework中,Hashtable是System.Collections命名空間提供的一個容器,用於處理和表現類似key/value的鍵值對,其中key通常用來快速查找,同時key是區分大小寫;value用於存儲對應於key的值。Hashtable中key/value鍵值對均爲object類型,所以Hashtable可以支持任何類型的key/value鍵值對。、
索引值爲字符串型的實例:
using System;//引用System命名空間
using System.Collections;//引用此命名空間,因爲Hashtable類包含在其中
class Indexclass
{
private Hashtable name = new Hashtable();//聲明一個Hashtable類的實例並把他作爲Indexclass類的成員
public string this[string index]//定義索引器,它有點像屬性,只不過是帶參數,並且索引值爲字符串型
{
get
{
return name[index].ToString();//因爲Hashtable類的鍵值返回的是object類型,需要把他轉換成字符串型
}
set
{
name.Add(index, value);//向Hashtable中寫入值需要用它本身的add方法
}
}
}
class Test
{
static void Main ()
{
Indexclass b = new Indexclass();//聲明一個Indexclass類的實例
b["A001"] = "張三";//爲實例賦值
b["A002"] = "李四";
b["A003"] = "王五";
Console.WriteLine("b['A001']=" + b["A001"]);
Console.WriteLine("b['A002']=" + b["A002"]);
Console.WriteLine("b['A003']=" + b["A003"]);
}
}
還有一個知識點需要說明:索引器可以重載,下面通過一個實例說明重載。本例的功能是可以通過學號查找學生姓名,或者姓名查找學生學號
using System;//引用System命名空間
using System.Collections;//引用此命名空間,因爲Hashtable類包含在其中
class Indexclass
{
private Hashtable name = new Hashtable();//聲明一個Hashtable類的實例並把他作爲Indexclass類的成員
public string this[int index]//定義索引器,它有點像屬性,只不過是帶參數,並且索引值爲字符串型
{
get
{
return name[index].ToString();//因爲Hashtable類的鍵值返回的是object類型,需要把他轉換成字符串型
}
set
{
name.Add(index, value);//向Hashtable中寫入值需要用它本身的add方法
}
}
public int this[string aName]//定義索引器,和上一個索引器構成重載
{
get
{
foreach (DictionaryEntry d in name)
{
if (d.Value.ToString() == aName)
return Convert.ToInt32(d.Key);
}
return -1;
}
set
{
name.Add(value, aName);
}
}
}
class Test
{
static void Main ()
{
Indexclass b = new Indexclass();//聲明一個Indexclass類的實例
//調用A索引器
b[100] = "張三";//爲實例賦值
b[200] = "李四";
b[300] = "王五";
Console.WriteLine("編號爲100的員工是:" + b[100]);
Console.WriteLine("編號爲200的員工是:" + b[200]);
Console.WriteLine("編號爲300的員工是:" + b[300]);
Console.WriteLine();
//調用B索引器
Console.WriteLine("張三的編號是:" + b["張三"]);
Console.WriteLine("李四的編號是:" + b["李四"]);
Console.WriteLine("王五的編號是:" + b["王五"]);
//調用A索引器,實際開發中並不會這樣用
b["趙六"] = 400;
b["錢七"] = 500;
Console.WriteLine();
Console.WriteLine("編號爲400的員工是:" + b[400]);
Console.WriteLine("編號爲500的員工是:" + b[500]);
}
}
索引器和數組的比較:
1. 索引器的索引值(index)類型不受限爲整數;用來訪問數組的索引值(index),其類型一定爲整數,然而索引器可以定義其他類型的索引值。
2. 索引器允許重載(overloading)
3. 索引器不是一個變量;索引和數組的不同地方在於,索引器並沒有直接對應應用數據存儲地方,而數組則有。索引器有get訪問器和set訪問器,用來知名要讀取或寫入索引元素時,需要執行的代碼。
索引器與屬性的不同點:
1. 標識方式:屬性以名稱來標識;索引器則以函數簽名來標識。
2. 索引器可以被重載:因爲屬性是以名稱標識的,所以不能被重載;索引器是用函數簽名標識的,因此可以重載。
3. 索引器不可以聲明爲static:屬性可以爲static,而索引器永遠屬於實例成員,不能爲static.
帶有多參數的索引器的使用範例:
using System;
using System.Collections;
//定義一個學生類,包括學生的姓名,課程ID,成績
class CourseScore
{
private string name;
private int courseID;
private int score;
public CourseScore(string name,int courseID,int score)
{
this.name = name;
this.courseID = courseID;
this.score = score;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int CourseID
{
get
{
return courseID;
}
set
{
courseID = value;
}
}
public int Score
{
get
{
return score;
}
set
{
score = value;
}
}
}
//定義一個CourseScoreIndexer類的索引器
class CourseScoreIndexer
{
private ArrayList arrCourseScore;//聲明一個ArrayList類的對象
public CourseScoreIndexer()
{
arrCourseScore = new ArrayList();
}
public int this[string name, int courseID]
{
get
{
foreach (CourseScore cs in arrCourseScore)
{
if (cs.Name == name && cs.CourseID ==courseID)
return cs.Score;
}
return -1;
}
set
{
arrCourseScore.Add(new CourseScore(name,courseID,value));
}
}
}
class Test
{
static void Main ()
{
CourseScoreIndexer csi = new CourseScoreIndexer();
csi["張三", 2] = 90;
csi["李四", 1] = 80;
csi["張三", 1] = 85;
csi["李四", 2] = 75;
Console.WriteLine(csi["張三", 2]);
}
}
如何實現把張三的全部成績打印出來呢?索引器重載
using System;
using System.Collections;
//定義一個學生類,包括學生的姓名,課程ID,成績
class CourseScore
{
private string name;
private int courseID;
private int score;
public CourseScore(string name,int courseID,int score)
{
this.name = name;
this.courseID = courseID;
this.score = score;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int CourseID
{
get
{
return courseID;
}
set
{
courseID = value;
}
}
public int Score
{
get
{
return score;
}
set
{
score = value;
}
}
}
//定義一個CourseScoreIndexer類的索引器,用以存儲和查詢CourseScore類
class CourseScoreIndexer
{
private ArrayList arrCourseScore;//聲明一個ArrayList類的對象
public CourseScoreIndexer()
{
arrCourseScore = new ArrayList();
}
public int this[string name, int courseID]
{
get
{
foreach (CourseScore cs in arrCourseScore)
{
if (cs.Name == name && cs.CourseID ==courseID)
return cs.Score;
}
return -1;
}
set
{
arrCourseScore.Add(new CourseScore(name,courseID,value));
}
}
public ArrayList this[string name]
{
get
{
ArrayList tmparr = new ArrayList();
foreach (CourseScore cs in arrCourseScore)
{
if (cs.Name == name)
tmparr.Add(cs);
}
return tmparr;
}
}
}
class Test
{
static void Main ()
{
CourseScoreIndexer csi = new CourseScoreIndexer();
csi["張三", 2] = 90;
csi["李四", 1] = 80;
csi["張三", 1] = 85;
csi["李四", 2] = 75;
Console.WriteLine(csi["張三", 2]);
Console.WriteLine("張三的所有課程成績爲:");
ArrayList arrtemp;
arrtemp=csi["張三"];
foreach (CourseScore cs in arrtemp)
Console.WriteLine("姓名爲:" + cs.Name + "課程號:" + cs.CourseID + "成績:" + cs.Score);
}
}