C#索引器(二)
索引器允許類和結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引,索引器類似與屬性,不同之處在于他們的訪問器采用參數(shù)。被稱為有參屬性。
簡(jiǎn)單的索引器實(shí)例:
索引器與屬性的比較:
標(biāo)示方式:屬性以名稱來標(biāo)識(shí),索引器以函數(shù)簽名來標(biāo)識(shí)。
索引器可以被重載。屬性則不可以被重載。
屬性可以為靜態(tài)的,索引器屬于實(shí)例成員,不能被聲明為static
多參數(shù)索引器實(shí)例:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Study
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? ScoreIndex s = new ScoreIndex();
??????????? s["張三", 1] = 90;
??????????? s["張三", 2] = 100;
??????????? s["張三", 3] = 80;
??????????? s["李四", 1] = 60;
??????????? s["李四", 2] = 70;
??????????? s["李四", 3] = 50;
??????????? Console.WriteLine("張三課程編號(hào)為1的成績(jī)?yōu)?#xff1a;" + s["張三",1]);
??????????? Console.WriteLine("張三的所有成績(jī)?yōu)?#xff1a;");
??????????? ArrayList temp;
??????????? temp = s["張三"];
??????????? foreach (IndexClass b in temp)
??????????? {
??????????????? Console.WriteLine("姓名:" + b.Name + "課程編號(hào):" + b.CourseID + "分?jǐn)?shù):" + b.Score);
??????????? }
??????????? Console.ReadKey();
??????? }
??? }
??? class IndexClass
??? {
??????? private string _name;
??????? private int _courseid;
??????? private int _score;
??????? public IndexClass(string _name, int _courseid, int _score)
??????? {
??????????? this._name = _name;
??????????? this._courseid = _courseid;
??????????? this._score = _score;
??????? }
??????? public string Name
??????? {
??????????? get { return _name; }
??????????? set { this._name = value; }
??????? }
??????? public int CourseID
??????? {
??????????? get { return _courseid; }
??????????? set { this._courseid = value; }
??????? }
??????? public int Score
??????? {
??????????? get { return _score; }
??????????? set { this._score = value; }
??????? }
??? }
??? class ScoreIndex
??? {
??????? private ArrayList arr;
??????? public ScoreIndex()
??????? {
??????????? arr = new ArrayList();
??????? }
??????? public int this[string _name, int _courseid]
??????? {
??????????? get
??????????? {
??????????????? foreach (IndexClass a in arr)
??????????????? {
??????????????????? if (a.Name == _name && a.CourseID == _courseid)
??????????????????? {
??????????????????????? return a.Score;
??????????????????? }
??????????????? }
??????????????? return -1;
??????????? }
??????????? set
??????????? {
??????????????? arr.Add(new IndexClass(_name, _courseid, value)); //arr["張三",1]=90
??????????? }
??????? }
??????? //重載索引器
??????? public ArrayList this[string _name]
??????? {
??????????? get
??????????? {
??????????????? ArrayList temp = new ArrayList();
??????????????? foreach (IndexClass b in arr)
??????????????? {
??????????????????? if (b.Name == _name)
??????????????????? {
??????????????????????? temp.Add(b);
??????????????????? }
??????????????? }
??????????????? return temp;
??????????? }
??????? }
??? }
}
備注:
所有索引器都使用this關(guān)鍵詞來取代方法名。Class或Struct只允許定義一個(gè)索引器,而且總是命名為this。
索引器允許類或結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引。索引器類似于屬性,不同之處在于它們的訪問器采用參數(shù)。
get 訪問器返回值。set 訪問器分配值。
this 關(guān)鍵字用于定義索引器。
value 關(guān)鍵字用于定義由 set 索引器分配的值。
索引器不必根據(jù)整數(shù)值進(jìn)行索引,由您決定如何定義特定的查找機(jī)制。
索引器可被重載。
索引器可以有多個(gè)形參,例如當(dāng)訪問二維數(shù)組時(shí)。
索引器可以使用百數(shù)值下標(biāo),而數(shù)組只能使用整數(shù)下標(biāo):如下列定義一個(gè)String下標(biāo)的索引器
public int this [string name] {...}
屬性和索引器
屬性和索引器之間有好些差別:
類的每一個(gè)屬性都必須擁有唯一的名稱,而類里定義的每一個(gè)索引器都必須擁有唯一的簽名(signature)或者參數(shù)列表(這樣就可以實(shí)現(xiàn)索引器重載)。
屬性可以是static(靜態(tài)的)而索引器則必須是實(shí)例成員。
轉(zhuǎn)載于:https://www.cnblogs.com/jiang_jiajia10/archive/2009/03/13/1410798.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: java+分支合并主线_git如何合并两
- 下一篇: 看到一个暴强的翻译,闲的蛋疼,写个c#版