什么是索引指示器


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...

面试题 相关文章推荐
PHP笔试题
Feb 22 面试题
String这个类型的class为何定义成final?
Nov 13 面试题
一套带答案的C++笔试题
Jan 10 面试题
extern在函数声明中是什么意思
Jan 19 面试题
SQL SERVER面试资料
Mar 30 面试题
用友笔试题目
Oct 25 面试题
什么是WEB控件?使用WEB控件有哪些优势?
Jan 21 面试题
面向对象概念面试题(.NET)
Nov 04 面试题
什么是Connection-oriented Protocol/Connectionless Protocol面向连接的协议/无连接协议
Sep 06 面试题
什么是符号链接,什么是硬链接?符号链接与硬链接的区别是什么?
May 03 面试题
解决方案设计综合面试题
Aug 31 面试题
一套Delphi的笔试题二
May 11 面试题
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中的unicode和utf8编码
2015/06/10 PHP
PHP常见数组函数用法小结
2016/03/21 PHP
PHP对象实例化单例方法
2017/01/19 PHP
复制Input内容的js代码_支持所有浏览器,修正了Firefox3.5以上的问题
2010/06/21 Javascript
javascript事件冒泡详解和捕获、阻止方法
2014/04/12 Javascript
使用jquery选择器如何获取父级元素、同级元素、子元素
2014/05/14 Javascript
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
js实现简易的单数字随机抽奖(0-9)
2020/03/19 Javascript
JS实现网页上随滚动条滚动的层效果代码
2015/11/04 Javascript
jQuery实现两列等高并自适应高度
2016/12/22 Javascript
详解为Bootstrap Modal添加拖拽的方法
2018/01/05 Javascript
JS获取月的第几周和年的第几周实例代码
2018/12/05 Javascript
vue中渲染对象中属性时显示未定义的解决
2020/07/31 Javascript
OpenLayers3实现地图显示功能
2020/09/25 Javascript
[56:45]DOTA2上海特级锦标赛D组小组赛#1 EG VS COL第一局
2016/02/28 DOTA
[05:24]TI9采访——教练
2019/08/24 DOTA
Python如何实现守护进程的方法示例
2017/02/08 Python
Python 实现数据库更新脚本的生成方法
2017/07/09 Python
Python自动化运维_文件内容差异对比分析
2017/12/13 Python
[原创]python爬虫(入门教程、视频教程)
2018/01/08 Python
python链接oracle数据库以及数据库的增删改查实例
2018/01/30 Python
python实现NB-IoT模块远程控制
2018/06/20 Python
python使用matplotlib绘制热图
2018/11/07 Python
深入浅析python 协程与go协程的区别
2019/05/09 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
python高斯分布概率密度函数的使用详解
2019/07/10 Python
在linux系统下安装python librtmp包的实现方法
2019/07/22 Python
Python基于Tkinter编写crc校验工具
2020/05/06 Python
CSS3.0实现霓虹灯按钮动画特效的示例代码
2021/01/12 HTML / CSS
Fanatics官网:运动服装、球衣、运动装备
2020/10/12 全球购物
理工学院学生自我鉴定
2014/02/23 职场文书
歌颂党的演讲稿
2014/09/10 职场文书
毕业生自荐信范文
2015/03/05 职场文书
2015年小学一年级班主任工作总结
2015/05/21 职场文书
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2021/06/21 Java/Android
如何解决php-fpm启动不了问题
2021/11/17 PHP