博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用C#改写Head First Design Patterns--Iterator迭代器(原创)
阅读量:4135 次
发布时间:2019-05-25

本文共 3384 字,大约阅读时间需要 11 分钟。

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;
            }
        }

    }

}

 

转载地址:http://zvpvi.baihongyu.com/

你可能感兴趣的文章
pytorch
查看>>
pytorch(三)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>
论文翻译:《一个包容性设计的具体例子:聋人导向可访问性》
查看>>
基于“分形”编写的交互应用
查看>>
《融入动画技术的交互应用》主题博文推荐
查看>>
链睿和家乐福合作推出下一代零售业隐私保护技术
查看>>
Unifrax宣布新建SiFAB™生产线
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>