需求
上一篇讲了字典,他能让我们使用除了int型编号以外的数据类型进行索引,但是这仍然无法满足我们所有的需求:比如你既想通过学号搜索到学生数据,又想通过姓名搜索到学生数据,为此建立多个字典又嫌麻烦;又或者你想获得搜索到数据的拷贝而不是引用……这个时候索引器就派上用场了。
是什么
索引器是封装了一组值的智能数组,它让用户可以自定义访问类型并以数组形式访问类中的数据。索引器实际上是有参属性。
注意
-
索引器不是方法——没有一对包含参数的圆括号,但有一对指定了索引的方括号。索引指定要访问哪一个元素。
-
所有索引器都使用this关键字取代方法名。每个类或结构只允许定义个索引器(虽然可重载并有多个实现),而且总是命名为this。
-
和属性一样,索引器也包含get和set这两个访问器。本例的get和set访问器包含前面讨论过的按位表达式。
-
索引器声明中指定的index将调用索引器时指定的索引值来填充。get和set访问器方法可以读取这个实参,判断应访问哪一个元素。
索引器和数组的区别
- 索引器能使用非数值下标,而数组只能使用整数下标。
- 索引器能重载,数组则不能。
- 索引器不能作为ref和out参数使用,数组则能。
怎么用
下面以学生学号(id),姓名(name) 和年龄(age)为学生属性来说明索引器的基本使用方法。
学生类
public class student:ICloneable{
public student(int id,string name,int age){
this.id = id;
this.name = name;
this.age = age;
}
private int _id;
private string _name;
private int _age;
public int id{
get{ return _id;}
set{ _id = value;}
}
public string name{
get{ return _name;}
set{ _name = value;}
}
public int age{
get{ return _age;}
set{ _age = value;}
}
public void showData(){
Debug.Log ("id:"+this.id+" name:"+this.name+" age:"+this.age);
}
//获取数据的拷贝而不是引用
public object Clone (){
return (object)this.MemberwiseClone ();
}
}
创建索引器
public class studentIndexer{
public studentIndexer(){
students = new List<student> ();
}
private List<student> students;
//通过id来进行索引
public student this[int id]{
get{
foreach (student stu in students) {
if (stu.id == id)
return (student)stu.Clone();
}
return null;
}
set{ students.Add (value);}
}
//索引器重载1:通过name进行索引
public student this[string name]{
get{
foreach (student stu in students) {
if (stu.name == name)
return (student)stu.Clone();
}
return null;
}
set{ students.Add (value);}
}
//索引器重载2:通过id和name进行多参索引
public int this[int id,string name]{
get{
foreach (student stu in students) {
if (stu.id == id&&stu.name==name)
return stu.age;
}
return 0;
}
set{ students.Add (new student(id,name,value));}
}
}
使用
private studentIndexer stuIndexer;
void Start () {
stuIndexer = new studentIndexer ();
//通过id给索引器增加元素
stuIndexer [11] = new student (11,"张三",22);
//通过name给索引器增加元素
stuIndexer ["李四"] = new student (12,"李四",23);
//通过id和name两个参数给索引器增加元素
stuIndexer [13, "王五"] = 24;
//通过id和name两个参数获取索引内容
Debug.Log (stuIndexer [11, "张三"]);
//通过id获取索引内容
stuIndexer [12].showData ();
//通过name获取索引内容
stuIndexer ["王五"].showData ();
}
转载请注明出处:https://blog.csdn.net/ylbs110/article/details/50664165
完整代码
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class MyIndexer : MonoBehaviour {
private studentIndexer stuIndexer;
void Start () {
stuIndexer = new studentIndexer ();
//通过id给索引器增加元素
stuIndexer [11] = new student (11,"张三",22);
//通过name给索引器增加元素
stuIndexer ["李四"] = new student (12,"李四",23);
//通过id和name两个参数给索引器增加元素
stuIndexer [13, "王五"] = 24;
//通过id和name两个参数获取索引内容
Debug.Log (stuIndexer [11, "张三"]);
//通过id获取索引内容
stuIndexer [12].showData ();
//通过name获取索引内容
stuIndexer ["王五"].showData ();
}
}
public class studentIndexer{
public studentIndexer(){
students = new List<student> ();
}
private List<student> students;
//通过id来进行索引
public student this[int id]{
get{
foreach (student stu in students) {
if (stu.id == id)
return (student)stu.Clone();
}
return null;
}
set{ students.Add (value);}
}
//索引器重载1:通过name进行索引
public student this[string name]{
get{
foreach (student stu in students) {
if (stu.name == name)
return (student)stu.Clone();
}
return null;
}
set{ students.Add (value);}
}
//索引器重载2:通过id和name进行索引
public int this[int id,string name]{
get{
foreach (student stu in students) {
if (stu.id == id&&stu.name==name)
return stu.age;
}
return 0;
}
set{ students.Add (new student(id,name,value));}
}
}
public class student:ICloneable{
public student(int id,string name,int age){
this.id = id;
this.name = name;
this.age = age;
}
private int _id;
private string _name;
private int _age;
public int id{
get{ return _id;}
set{ _id = value;}
}
public string name{
get{ return _name;}
set{ _name = value;}
}
public int age{
get{ return _age;}
set{ _age = value;}
}
public void showData(){
Debug.Log ("id:"+this.id+" name:"+this.name+" age:"+this.age);
}
//获取数据的拷贝而不是引用
public object Clone (){
return (object)this.MemberwiseClone ();
}
}