现在的位置: 首页 > 综合 > 正文

让类属性带索引

2012年04月30日 ⁄ 综合 ⁄ 共 2832字 ⁄ 字号 评论关闭
今年看到一种实现索引的方法,后来调试一下才知道这是对对象索引的,不是我想要的对属性索引的。

using System;

public class m
{
 
static void Main()
 
{
  point p
=new point();
  p[
0]=13;
  Console.WriteLine(
"y[0]=" + p[0].ToString());
 }

}


class point
{
 
int[] m_y=new int[10];
 
public int this[int nIndex]
 
{
  
get return m_y[nIndex]; }
  
set { m_y[nIndex] = value; }
 }

}

这里的this不以改为point,也不能去掉。正是MSDN中说的this的第三种用法。
这种索引,一个类只能有一个,显然不够我用,并且用得也是奇奇怪怪,要显示y的值,要用p来表达。
最理想的是这种模式:

using System;

public class m
{
 
static void Main()
 
{
  point p
=new point();
  p.y[
0]=13;
  Console.WriteLine(
"y[0]=" + p.y[0].ToString());
 }

}


class point
{
 
int[] m_y=new int[10];
 
public int y[int nIndex]
 
{
  
get return m_y[nIndex]; }
  
set { m_y[nIndex] = value; }
 }

}

它只是把第一种模式中的this改为y,并且希望要支持多个索引。但这个代码,编译器生成一箩筐错误:
p.cs(16,14): error CS0650:
        语法错误,错误的数组声明符。要声明托管数组,秩说明符应位于变量标识符之前
        。要声明固定大小缓冲区字段,应在字段类型之前使用 fixed 关键字。
p.cs(16,15): error CS0270:
        不能在变量声明中指定数组大小(请尝试使用“new”表达式初始化)
p.cs(16,19): error CS1525: 无效的表达式项“int”
p.cs(16,26): error CS1002: 应输入 ;
p.cs(17,2): error CS1519: 类、结构或接口成员声明中的标记“{”无效
p.cs(18,7): error CS1519: 类、结构或接口成员声明中的标记“{”无效
p.cs(18,20): error CS0270:
        不能在变量声明中指定数组大小(请尝试使用“new”表达式初始化)
p.cs(18,27): error CS1519: 类、结构或接口成员声明中的标记“;”无效
p.cs(19,3): error CS0116: 命名空间并不直接包含诸如字段或方法之类的成员
p.cs(19,21): error CS1518: 应输入 class、delegate、enum、interface 或 struct
p.cs(19,30): error CS1022: 应输入类型、命名空间定义或文件尾

但别人根据类索引器这个原理,做出一可以任意多个索引的功能,只是太麻烦了点:

using System;
using System.Collections;

public class m
{
 
static void Main() 
 
{
  MyClass myObject 
= new MyClass(); 
  myObject.Data[
10= 10
  Console.Write(
"Data[0]=" + myObject.Data[10].ToString());
 }

}

public class MyClass 

 
private ArrayList _list = new ArrayList(); 
 
 
public class MyData 
 

  
private MyClass _owner = null
  
internal MyData(MyClass owner) 
  

   
this._owner = owner; 
  }
 
  
public object this[int index] 
  

   
get 
   

    
return this._owner._list[index]; 
   }
 
   
set 
   

    
this._owner._list[index] = value; 
   }
 
  }
 
 }
 
 
 
private MyData _data = null
 
public MyData Data 
 

  
get 
  

   
if(this._data == null
    
this._data = new MyData(this); 
   
return this._data; 
  }
 
 }
 
}

我把它改后变成这样:

using System;

public class m
{
 
static void Main()
 
{
  point p
=new point();
  p.x[
0]=11;
  Console.WriteLine(
"x[0]=" + p.x[0].ToString());
 }

}


class point
{
 
static int[] m_x=new int[10];
 
public class ax
 
{
  
public int this[int nIndex]
  
{
   
get return m_x[nIndex]; }
   
set { m_x[nIndex] = value; }
  }

 }


 
public ax x=new ax();
}

实现属性索引的另一种方法:

using System;
using System.Collections;

public class m
{
 
static void Main()
 
{
  point p
=new point();
  p.x
=new ArrayList();
  p.x.Add(
"12");
  Console.WriteLine(
"x[0]=" + p.x[0].ToString());
 }

}


class point
{
 
private ArrayList m_x;

 
public ArrayList x
 
{
  
get return m_x; }
  
set { m_x = value; }
 }

}

我曾经在第9行写错:point.x=new ArrayList();
编译器给的出错结果为:
p.cs(9,3): error CS0120: 非静态的字段、方法或属性“point.x.get”要求对象引用
p.cs(21,3): (与前一个错误相关的符号位置)
我一直理解不过来。因此耽误了好长时间。

这种虽然实现了索引,但还是不够理想,它引入了ArrayList对象,主要缺点是代码不够简洁。

抱歉!评论已关闭.