zoukankan      html  css  js  c++  java
  • 自定义集合类

            我们可以通过实现System.Collections.IEnumerable

    System.Collections.IEnumerator接口写出自己需要的集合类。

    下面写两种设计实现自定义Library集合类的方法:


     
    using System;
    using System.Collections;

    namespace Library
    自定义集合类 - 脆弱的rohelm - 脆弱的rohelm(C入门历程)
     
    {
        class Library : IEnumerable//实现接口IEnumerabler
        {
            protected class Enumerator : IEnumerator//实现接口IEnumerator
            {
                private string exceptionInfo =
                    "索引无效,MoveNext当前指向索引为空";
                private int index = -1;//指向集合顶部对象的前一位
                private ArrayList books = new ArrayList();//制定一个存放书名的数据成员
                public Enumerator(ArrayList books)
                {
                    foreach (string book in books)
                    {
                        this.books.Add(book);
                    }
                    Reset();
                }
                public bool MoveNext()//实现方法MoveNext
                {
                    return  (++index < books.Count);    
                }
                public void Reset()//实现方法Reset
                {
                    index = -1;
                }
                public object Current//实现属性Current
                {
                    get
                    {
                        if (0 <= index && index<books.Count)
                            return books[index];//返回当前元素
                        else
                            throw new IndexOutOfRangeException(exceptionInfo);
                    }
                }
            }
            protected ArrayList books;
            public Library()
            {
                books = new ArrayList();
            }
            public void Add(string book)
            {
                books.Add(book);
            }
            public void Remove(string book)
            {
                books.Remove(book);
            }
            public IEnumerator GetEnumerator()//实现方法 GetEnumerator
            {
                return new Enumerator(books);
            }
        }
        class Test
        {
            static void Main()
            {
                Library lib = new Library();
                lib.Add("《广告》");
                lib.Add("《传播学》");
                lib.Add("《传媒研究》");
                lib.Add("《市场调研》");
                Console.WriteLine("说含有的课本包括:");
                IEnumerator e = lib.GetEnumerator();
                while (e.MoveNext())
                {
                    Console.WriteLine(e.Current);
                }
                Console.ReadKey();
            }
        }
    }
    ////////////////////////////////method of second///////////////////////////////////
    using System;
    using System.Collections;

    namespace Library2
    {
        class Library:IEnumerable,IEnumerator
        {
            private int index = -1;
            protected ArrayList books;
            private string exceptionInfo = "索引无效,MoveNext当前指向索引为空";
            public Library()
            {
                books = new ArrayList();
            }
            //实现IEnumerator接口的方法
            public bool MoveNext()
            {
                return (++index < books.Count);
            }
            public void Reset()
            {
                index = -1;
            }
            public object  Current
            {
                get
                {
                    if (0 <= index && index < books.Count)
                    {
                        return books[index];
                    }
                    else
                        throw new IndexOutOfRangeException(exceptionInfo);
                }
            }
            //实现IEnumerable接口方法
            public IEnumerator GetEnumerator()
            {
                Reset();
                return this;
            }
             //  添加Library集合类方法
            public void Add(string book)
            {
                books.Add(book);
            }
            public void Remove(string book)
            {
                books.Remove(book);
            }
        }
        class Test
        {
            static void Main()
            {
                Library lib = new Library();
                lib.Add("《广告》");
                lib.Add("《传播学》");
                lib.Add("《传媒研究》");
                lib.Add("《市场调研》");
                Console.WriteLine("说含有的课本包括:");
                foreach (string book in lib)
                {
                    Console.WriteLine(book);
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    React 框架 | 深入剖析 Scheduler 原理 广东靓仔
    Spool and Print [转]
    Excel表中的文本格式与数字格式转换方法(转)
    什么叫网关的精解(超经典)(zt)
    如何在公司局域网内部上网使用代理服务器突破网关(zt)
    百鸟之王
    两种修改网卡物理地址的秘笈
    MAC地址作用以及原理(ZT)
    js 通过后台接口返回的URL地址下载文件并保存到本地(已在项目中使用,保存音视频文件)
    DataTable中数据记录的统计
  • 原文地址:https://www.cnblogs.com/rohelm/p/2384096.html
Copyright © 2011-2022 走看看