设为首页 加入收藏

TOP

自定义集合实体类(二)
2015-07-24 05:50:27 来源: 作者: 【 】 浏览:13
Tags:定义 集合 实体
承ICollection //获取一个值,该值指示 System.Collections.Generic.ICollection 是否为只读。 public bool IsReadOnly { get { return true; } } //确定 System.Collections.Generic.ICollection 是否包含特定值。 public bool Contains(T item) { return this.list.Contains(item); } //从特定的 System.Array 索引处开始,将 System.Collections.Generic.ICollection 的元素复制到一个 public void CopyTo(T[] array, int arrayIndex) { this.list.CopyTo(array, arrayIndex); } #endregion #region 支持foreach语句遍历 #region 实现IEnumerable 接口 System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable .GetEnumerator() { return (System.Collections.Generic.IEnumerator )this; } #region 实现IEnumerable接口,因为IEnumerable 继承IEnumerable System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return (System.Collections.IEnumerator)this; } #endregion #endregion #region 实现IEnumerator 接口 //获取集合中的当前元素。 T System.Collections.Generic.IEnumerator .Current { get { try { return this.list[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } #region 实现IEnumerator接口,因为IEnumerator 继承IEnumerator //获取集合中的当前元素。 object System.Collections.IEnumerator.Current { get { try { return this.list[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } //将枚举数推进到集合的下一个元素。 bool System.Collections.IEnumerator.MoveNext() { this.position++; return (this.position < this.list.Count); } //将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。 void System.Collections.IEnumerator.Reset() { this.position = -1; } #endregion #region 实现IDisposable接口,因为IEnumerator 继承IDisposable public void Dispose() { this.position = -1; } #endregion #endregion #endregion } }

具体实体集合类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableToCollection.实体;

namespace 集合.集合.灵活性好的集合
{
    public class PersonBaseCollection:BaseCollection
  
   
    {        
    }
}

  

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableToCollection.实体;
using TableToCollection.集合.灵活性差的集合;
using 集合.集合.灵活性好的集合;
using 集合.灵活性差的ORMapping.ORMapping;

namespace 集合
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 灵活性差的集合
            //#region 灵活性差的集合
            //PersonCollection_bad personCollection = new PersonCollection_bad();
            ////集合添加元素
            //for (int i = 0; i < 10; i++)
            //{
            //    Person p = new Person() { strID=i.ToString(),strName="zhang",intAge=i};
            //    personCollection.Add(p);
            //}

            //Console.WriteLine("元素个数:"+personCollection.Count);

            ////输出集合
            //Console.WriteLine("使用foreach输出");
            //foreach (var item in personCollection)
            //{
            //    Console.WriteLine(((Person)item).strID + ((Person)item).strName + ((Person)item).intAge);
            //}
            //Console.WriteLine("使用for输出");
            //for (int i = 0; i < personCollection.Count; i++)
            //{
            //    Console.WriteLine(((Person)personCollection[i]).strID + ((Person)personCollection[i]).strName + ((Person)personCollection[i]).intAge);
            //}
            //#endregion
            #endregion
            
            #region 灵活性好的集合
            PersonBaseCollection personBaseCollection = new PersonBaseCollection();

            DoAdd(personBaseCollection);
            DoForPrint(personBaseCollection);

            DoInsert(0,personBaseCollection);
            DoForEachPrint(personBaseCollection);

            personBaseCollection.RemoveAt(1);
            DoForEachPrint(personBaseCollection);

            personBaseCollection.Clear();
            DoForEachPrint(personBaseCollection);            
            #endregion
            
            
            Console.ReadKey();
        }

        static void DoAdd(PersonBaseCollection personBaseCollection)
        {
            personBaseCollection.Add(new Person() { strID = "1", strName = "qs1(Add)", intAge = 21 });

            Person[] persons = { new Person() { strID = "2", strName = "qs2(AddRange)", intAge = 22 }, new Person() { strID = "3", strName = "qs3(AddRange)", intAge = 23 } };
            personBaseCollection.AddRange(persons);

        }

        static void DoInsert(int index, PersonBaseCollection personBaseCollection)
        {
            personBaseCollection.Insert(index, new Person() { strID = "4", strName = "qs4(Insert)", intAge = 24 });
        }

        static void DoForPrint(PersonBaseCollection personBaseCollection)
        {            
            Console.WriteLine("For语句输出:");
            if (personBaseCollection.Count == 0)
            {
                Console.WriteLine("集合里面没有元素!");
                return;
            }
            for (int i = 0; i < personBaseCollection.Count; i++)
            {
                Console.WriteLine(personBaseCollection[i].strID + "    " + personBaseCollection[i].strName + "    " + personBaseCollection[i].intAge);
            }
        }

        static void DoForEachPrint(PersonBaseCollection personBaseCollection) {
            Console.WriteLine("ForEach语句输出:");
            if (personBaseCollection.Count == 0)
            {
                Console.WriteLine("集合里面没有元素!");
                return;
            }
            foreach (var item in personBaseCollection)
            {
                Console.WriteLine(item.strID +"    "+ item.strName +"    "+ item.intAge);
            }
        }
    }
}

总结

上面说的内容中还有一块没有说,就是迭代器设计模式,其实上面的程序中已经运用了,只不过相应的接口是微软给定义好的,为什么微软给定义好了的呢?因为微软自己也要用(.net framework提供的具体集合类)。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2391Ombrophobic Bovines(二.. 下一篇POJ 2349 Arctic Network 最小生..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: