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();
            }
        }
    }
  • 相关阅读:
    python day03--字符串
    python day02--运算符,编码
    python day01
    数据库:对 null 和notnull,单表操作,多表操作 。
    数据库的介绍
    数据库对表的操作练习。
    数据库基础知识:介绍
    并发编程知识总结,软件开发架构,socket套接字模板,粘包问题,struct解决粘包问题,上传大文件数据,socketserver,关于操作系统的发展史,进程,线程。
    Event事件,进程池与线程池,高性能爬取梨视频,协程,协程的目的,gevent。
    GIL全局解释器锁,多线程的作用,死锁现象,信号量(了解),线程队列。
  • 原文地址:https://www.cnblogs.com/rohelm/p/2384096.html
Copyright © 2011-2022 走看看