索引器
索引器(indexer)是c#引入的一个新型的类成员,它使得对象可以像数组那样被方便,直观的引用。索引器非常类似于我们前面讲到的属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用。下面是典型的索引器的设计,我们在这里忽略了具体的实现。
| class myclass { public object this [int index] { get { // 取数据 } set { // 存数据 } } } |
| class myclass { public object get_item(int index) { // 取数据 } public void set_item(int index, object value) { //存数据 } } |
| using system; class bitarray { int[] bits; int length; public bitarray(int length) { if (length < 0) throw new argumentexception(); bits = new int[((length - 1) >> 5) + 1]; this.length = length; } public int length { get { return length; } } public bool this[int index] { get { if (index < 0 index >= length) throw new indexoutofrangeexception(); else return (bits[index >> 5] & 1 << index) != 0; } set { if (index < 0 index >= length) throw new indexoutofrangeexception(); else if(value) bits[index >> 5] = 1 << index; else bits[index >> 5] &= ~(1 << index); } } } class test { static void main() { bitarray bits=new bitarray(10); for(int i=0;i<10;i++) bits[i]=(i%2)==0; console.write(bits[i]+" "); } } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器