用C#改写Head First Design PatternsIterator迭代器(原创)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Iterator
{
    public class Item
    {
        string name;
        double price;
       
        public Item(string n, double p)
        {
            this.name = n;
            this.price = p;
        }

        public string getName()
        {
            return name;
        }

        public double getPrice()
        {
            return price;
        }

    }

    //使用ArrayList
    public class BookItems
    {
        ArrayList books;

        public BookItems() {

            books = new ArrayList();

            add("JAVA大全", 199);
            add("InfoPath大全", 29);
            add("Asp大全", 33);

       
        }

        public void add(string n, double p)
        {
            Item i = new Item(n, p);

            books.Add(i);
        }

        public ArrayList getItems()
        {
            return books;
        }


      
    }


    //使用范型
    public class CDItems
    {
        List<Item> CDs;

        public CDItems()
        {

            CDs = new List<Item>();

            add("郑秀文专辑", 69);
            add("郑伊健专辑", 59);
            add("刘德华专辑", 53);
        }

        public List<Item> Items
        {
            get
            {
                return CDs;
            }
            set
            {

            }
        }

        public void add(string n, double p)
        {
            Item i = new Item(n, p);

            CDs.Add(i);
        }

        public List<Item> getItems()
        {
            return CDs;
        }

    }

    //使用数组
    public class MobileItems
    {
        Item[] Mobiles;
        static int MAX_ITEMS = 3;
        int index = 0;

        public MobileItems()
        {

            Mobiles = new Item[MAX_ITEMS];

            add("诺基亚", 3369);
            add("三星", 2259);
            add("LG", 1153);
        }

        public void add(string n, double p)
        {
            Item i = new Item(n, p);
            if (index < MAX_ITEMS)
            {
                Mobiles[index] = i;
                index++;
            }
        }

        public Item[] getItems()
        {
            return Mobiles;
        }


        //#region IEnumerable 成员

        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //    return new ArrayIEnumerator(Mobiles);
        //}

        //#endregion
    }


    public class ArrayIEnumerator : IEnumerator
    {
        Item[] items;
        int index = 0;

        public ArrayIEnumerator(Item[] i)
        {
            this.items = i;
        }

        #region IEnumerator 成员

        object IEnumerator.Current
        {
            get { return items[index]; }
        }

        bool IEnumerator.MoveNext()
        {
            if (index > items.Length || items[index] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        void IEnumerator.Reset()
        {
            index = 0;
        }

        #endregion
    }


}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Iterator
{
    public class Point
    {
        public int X;
        public int Y;

        public Point(int i, int j)
        {
            this.X = i;
            this.Y = j;
        }
   
    }

    class Program
    {
        static void Main(string[] args)
        {

            BookItems b = new BookItems();
            CDItems c = new CDItems();
            MobileItems m = new MobileItems();

            //C#自动实现了迭代器

            foreach (Item i in b.getItems())
            {
                Console.WriteLine(i.getName() + " " + i.getPrice());
            }

            foreach (Item i in c.Items)
            {
                Console.WriteLine(i.getName() + " " + i.getPrice());
            }

            foreach (Item i in m.getItems())
            {
                Console.WriteLine(i.getName() + " " + i.getPrice());
            }


            //以下是范型委托的例子

            // To find the first Point structure for which X times Y
            // is greater than 100000, pass the array and a delegate
            // that represents the ProductGT10 method to the Shared
            // Find method of the Array class.


            Point[] points = { new Point(100, 200),
            new Point(150, 250), new Point(250, 375),
            new Point(275, 395), new Point(295, 450) };

            Point[] all = Array.FindAll(points, ProductGT10);


            List<Point> all1 = new List<Point>();
            all1.Add(new Point(100, 200));
            all1.Add(new Point(150, 250));
            all1.Add(new Point(250, 375));
            all1.Add(new Point(275, 395));
            all1.Add(new Point(295, 450));

            all1 = all1.FindAll(ProductGT10);

            Console.WriteLine("以下是范型委托的例子FindAll");

            foreach (Point p in all)
            {
                Console.WriteLine(p.X.ToString() + " " + p.Y.ToString());
            }


            System.Console.ReadLine();

           
        }

        private static bool ProductGT10(Point p)
        {
            if (p.X * p.Y > 100000)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}


关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1519223.html