什么是索引指示器


Posted in 面试题 onAugust 20, 2012
实现索引指示器(indexer)的类可以象数组那样使用其实例后的对象,但与数组不同的是索引指示器的参数类型不仅限于int
简单来说,其本质就是一个含参数属性
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example08
{
public class Point
{
private double x, y;
public Point(double X, double Y)
{
x = X;
y = Y;
}
//重写ToString方法方便输出
public override string ToString()
{
return String.Format(“X: {0} , Y: {1}”, x, y);
}
}
public class Points
{
Point[] points;
public Points(Point[] Points)
{
points = Points;
}
public int PointNumber
{
get
{
return points.Length;
}
}
//实现索引访问器
public Point this[int Index]
{
get
{
return points[Index];
}
}
}
//索引指示器的实质是含参属性,参数并不只限于int
class WeatherOfWeek
{
public string this[int Index]
{
get
{
//注意case段使用return直接返回所以不需要break
switch (Index)
{
case 0:
{
return “Today is cloudy!”;
}
case 5:
{
return “Today is thundershower!”;
}
default:
{
return “Today is fine!”;
}
}
}
}
public string this[string Day]
{
get
{
string TodayWeather = null;
//switch的标准写法
switch (Day)
{
case “Sunday”:
{
TodayWeather = “Today is cloudy!”;
break;
}
case “Friday”:
{
TodayWeather = “Today is thundershower!”;
break;
}
default:
{
TodayWeather = “Today is fine!”;
break;
}
}
return TodayWeather;
}
}
}
class Program
{
static void Main(string[] args)
{
Point[] tmpPoints = new Point[10];
for (int i = 0; i {
tmpPoints[i] = new Point(i, Math.Sin(i));
}
Points tmpObj = new Points(tmpPoints);
for (int i = 0; i {
Console.WriteLine(tmpObj[i]);
}
string[] Week = new string[] { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Staurday”};
WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();

for (int i = 0; i
{

Console.WriteLine(tmpWeatherOfWeek[i]);

}

foreach (string tmpDay in Week)

{

Console.WriteLine(tmpWeatherOfWeek[tmpDay]);

}

Console.ReadLine();

}

}

}

结果:
X: 0 , Y: 0
X: 1 , Y: 0.841470984807897
X: 2 , Y: 0.909297426825682
X: 3 , Y: 0.141120008059867
X: 4 , Y: -0.756802495307928
X: 5 , Y: -0.958924274663138
X: 6 , Y: -0.279415498198926
X: 7 , Y: 0.656986598718789
X: 8 , Y: 0.989358246623382
X: 9 , Y: 0.412118485241757
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is fine!

Tags in this post...

面试题 相关文章推荐
求两个数的乘积和商数,该作用由宏定义来实现
Mar 13 面试题
经典c++面试题五
Dec 17 面试题
当我正在为表建立索引的时候,SQL Server 会禁止对表的访问吗
Apr 28 面试题
端口镜像是怎么实现的
Mar 25 面试题
网络技术支持面试题
Apr 22 面试题
什么是属性访问器
Oct 26 面试题
常用UNIX 命令(Linux的常用命令)
Jul 10 面试题
what is the difference between ext2 and ext3
Aug 25 面试题
说一下Linux下有关用户和组管理的命令
Jan 04 面试题
企业面试题试卷附带答案
Dec 20 面试题
正隆泰信息技术有限公司上机题
Jun 14 面试题
Java如何支持I18N?
Oct 31 面试题
new修饰符是起什么作用
Jun 28 #面试题
Can a struct inherit from another struct? (结构体能继承结构体吗)
Sep 25 #面试题
C#笔试题集合
Jun 21 #面试题
this关键字的含义
Apr 08 #面试题
Can a struct inherit from another class? (结构体能继承类吗)
Jul 22 #面试题
.net C#面试题
Aug 28 #面试题
可以使用抽象函数重写基类中的虚函数吗
Jun 02 #面试题
You might like
用PHP读注册表
2006/10/09 PHP
实用函数10
2007/11/08 PHP
php操作sqlserver关于时间日期读取的小小见解
2009/11/29 PHP
THINKPHP项目开发中的日志记录实例分析
2014/12/01 PHP
php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
2016/05/09 PHP
浅谈PHP的数据库接口和技术
2016/12/09 PHP
一个非常实用的php文件上传类
2017/07/04 PHP
浅谈laravel-admin form中的数据,在提交后,保存前,获取并进行编辑
2019/10/21 PHP
HTML中事件触发列表与解说
2007/07/09 Javascript
找到了一篇jQuery与Prototype并存的冲突的解决方法
2007/08/29 Javascript
Jsonp 跨域的原理以及Jquery的解决方案
2010/05/18 Javascript
JS 自定义带默认值的函数
2011/07/21 Javascript
javascript返回顶部效果(自写代码)
2013/01/06 Javascript
jQuery操作checkbox选择(list/table)
2013/04/07 Javascript
IE下通过a实现location.href 获取referer的值
2014/09/04 Javascript
javascript框架设计之种子模块
2015/06/23 Javascript
javascript的几种写法总结
2016/09/30 Javascript
Express之get,pos请求参数的获取
2017/05/02 Javascript
vue左侧菜单,树形图递归实现代码
2018/08/24 Javascript
基于Vue 2.0 监听文本框内容变化及ref的使用说明介绍
2018/08/24 Javascript
webuploader分片上传的实现代码(前后端分离)
2018/09/10 Javascript
vue动态循环出的多个select出现过的变为disabled(实例代码)
2019/11/10 Javascript
python通过scapy获取局域网所有主机mac地址示例
2014/05/04 Python
Python中列表元素转为数字的方法分析
2016/06/14 Python
python中找出numpy array数组的最值及其索引方法
2018/04/17 Python
完美解决python3.7 pip升级 拒绝访问问题
2019/07/12 Python
python2 中 unicode 和 str 之间的转换及与python3 str 的区别
2019/07/25 Python
学会迭代器设计模式,帮你大幅提升python性能
2021/01/03 Python
世界上最好的旅行夹克:BauBax
2018/12/23 全球购物
售后服务经理岗位职责范本
2014/02/22 职场文书
教师党的群众路线对照检查材料
2014/09/24 职场文书
工作业绩不及格检讨书
2014/10/28 职场文书
高三毕业评语
2014/12/31 职场文书
关于艺术节的开幕致辞
2016/03/04 职场文书
python批量创建变量并赋值操作
2021/06/03 Python
Java中API的使用方法详情
2022/04/06 Java/Android