zoukankan      html  css  js  c++  java
  • 索引器

    索引器是C#引入的一个新的类型成员。估且不说它到底是什么,我们先来看下面一个向量集合类的代码:

    namespace XiaomiExample
    {
        
    public class VectorCollection
        
    {
            
    //存放Vector实例
            private List<Vector> _vectors;

            
    public List<Vector> Vectors
            
    {
                
    get
                
    {
                    
    if (null == _vectors)
                    
    {
                        _vectors 
    = new List<Vector>();
                    }

                    
    return _vectors;
                }

            }


            
    /// <summary>
            
    /// 向集合中添加一个向量
            
    /// </summary>
            
    /// <param name="value"></param>

            public void Add(Vector value)
            
    {
                Vectors.Add(value);
            }

            
    /// <summary>
            
    /// 从集合中去除一个向量
            
    /// </summary>
            
    /// <param name="index"></param>

            public void RemoveAt(int index)
            
    {
                Vectors.RemoveAt(index);
            }


            
    /// <summary>
            
    /// 返回集合中所有的向量
            
    /// </summary>
            
    /// <returns></returns>

            public override string ToString()
            
    {
                
    if (null == _vectors || _vectors.Count == 0return "The VectorCollection is Empty!<br />";
                
    //The Vectors is not null
                string result="";
                
    foreach (Vector v in _vectors)
                
    {
                    result 
    += v.ToString() + "<br />";
                }

                
    return result;
            }

        }

    }

    因为使用到了List<T>,请大家添加System.Collections和System.Collections.Generic命名空间。关于Vector类,大家可以参看http://www.cnblogs.com/xiaomi7732/archive/2007/08/15/857301.html文章最下面的源代码。
    首先,我们定义了一List<Vector>对象_vectors来存放Vector的实例,并通过public的Vectors属性将_vectors暴露。然后,简单的做了Add和RemoveAt方法,然后,重写了ToString()方法,用以输出全部的Vector。相应的测试代码片段如下:

    //a、b、c、da是四个Vector的实例
    VectorCollection vectors = new VectorCollection();
    vectors.Add(a);
    vectors.Add(b);
    vectors.Add(c);
    vectors.Add(da);
    vectors.RemoveAt(
    3);//移除第4条向量
    Response.Write("<br />" + vectors.ToString());

    看到这里,你可能觉得这样的代码并没有什么奇怪的。而且,结果也很正常的输出了4-1条向量。但是,如果要求接着再输出第三要记录,要如何写代码?呵呵,恐怕代码会是这样子的:

    Response.Write("<br />" + vectors.Vectors[2].ToString());

    重新看一下这一句代码,是不是觉得怪怪的?vectors就是向量集,为什么要取它的Vectors[2]才能输出第三条向量呢?但是,根据当前的代码分析,这也没有什么奇怪的:实质上,所有的向量都是存储在_vectors这个List<Vector>里的,而它又是通过Vectors属性暴露出来的。无论如何,这样子的代码给人感觉很不爽,于是,有人发明的一个东东,叫“索引器”^_^,它有点儿像属性,又可以让我们像访问字典一样的访问对象。这样说可能有点儿抽象,还是来看具体的例子好了。
    首先,我们明确一下要达到的最终目标,希望把上面那一句改成如下形式:

    Response.Write("<br />" + vectors[2].ToString());

    嗯,好看多了。但是,要实现这样的目标,需要对VectorCollection作一个小小的修改,即加上索引器,我们添加如下代码到VectorCollection类:

    public Vector this[int index]
    {
        
    get
        
    {
            
    return _vectors[index];
        }

        
    set
        
    {
            _vectors[index] 
    = value;
        }

    }

    索引器的定义是不是很像一个带了有参数的[]的属性?
    总结一下,索引器就如同一个属性,但是,这个属性是带有参数的,并且这个参数还是一个索引(但不仅限于数值类型)。它可以让我们像访问Dictionary一样去访问一个集合型对象。
    最后,为了完整起见,附上VectorCollection类的完整源代码。呵呵,这个代码可不完善哦。


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.Collections.Generic;

    namespace XiaomiExample
    {
        
    public class VectorCollection
        
    {
            
    //存放Vector实例
            private List<Vector> _vectors;

            
    public List<Vector> Vectors
            
    {
                
    get
                
    {
                    
    if (null == _vectors)
                    
    {
                        _vectors 
    = new List<Vector>();
                    }

                    
    return _vectors;
                }

            }


            
    /// <summary>
            
    /// 返回或者设置第index条向量
            
    /// </summary>
            
    /// <param name="index">索引</param>
            
    /// <returns></returns>

            public Vector this[int index]
            
    {
                
    get
                
    {
                    
    return _vectors[index];
                }

                
    set
                
    {
                    _vectors[index] 
    = value;
                }

            }


            
    /// <summary>
            
    /// 向集合中添加一个向量
            
    /// </summary>
            
    /// <param name="value"></param>

            public void Add(Vector value)
            
    {
                Vectors.Add(value);
            }

            
    /// <summary>
            
    /// 从集合中去除一个向量
            
    /// </summary>
            
    /// <param name="index"></param>

            public void RemoveAt(int index)
            
    {
                Vectors.RemoveAt(index);
            }


            
    /// <summary>
            
    /// 返回集合中所有的向量
            
    /// </summary>
            
    /// <returns></returns>

            public override string ToString()
            
    {
                
    if (null == _vectors || _vectors.Count == 0return "The VectorCollection is Empty!<br />";
                
    //The Vectors is not null
                string result="";
                
    foreach (Vector v in _vectors)
                
    {
                    result 
    += v.ToString() + "<br />";
                }

                
    return result;
            }

        }

    }
  • 相关阅读:
    Navicat将表转为模型
    RestTemplate Hashmap变为LinkedHashMap源码解读
    IDEA无法编译源码,IDEA查看源码出现/* compiled code */
    grep,egrep,正则表达式
    特殊权限
    更新系统硬件信息----光驱
    复制其他文件的权限做为自己的权限
    umask
    生成随机口令
    让新增用户默认拥有文件
  • 原文地址:https://www.cnblogs.com/lipengjiushiwo/p/2514101.html
Copyright © 2011-2022 走看看