C#迭代器的实现

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

namespace iterator
{
    abstract class ITerator//创建抽象迭代接口
    {
        public abstract object Fir();
        public abstract object Sec();
        public abstract bool Judge();
        public abstract object GetCounts();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iterator
{
    abstract class Aggregate
    {
        public abstract ITerator ConcreteIterator();//设置存放的接口,及迭代器
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iterator
{
    class ConcreteAggregate:Aggregate
    {
        private IList<object> items = new List<object>();//创建存放需要迭代的目标
        public int count//设置属性,列表中的元素数量
        {
            get{ return items.Count; }
        }
        public object this[int index]//创建检索目录
        {
            get { return items[index];}
            set { items.Insert(index,value);}
        }

        public override ITerator ConcreteIterator()//重写
        {
            return new ConcreteIterator(this);
        }
        
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iterator
{
    class ConcreteIterator:ITerator
    { 
        private ConcreteAggregate aggregate;//关联存放类
        private int CurrentCount = 0;//定义当前计数初始值
        public ConcreteIterator(ConcreteAggregate aggregate)//传参
        {
            this.aggregate = aggregate;
        }

        public override object Fir()//获取列表中的第一个对象
        {
           return aggregate[0];
        }

        public override object Sec()//重写,这里实现迭代
        {
            object Now = null;
            CurrentCount++;
            if (CurrentCount <aggregate.count)
            { 
            Now=aggregate[CurrentCount];
            }
            return Now;
        }

        public override bool Judge()//判断是否越界
        {
            return CurrentCount < aggregate.count ? true : false;         
        }

        public override object GetCounts()//得到当前迭代得到的值
        {
            return  aggregate[CurrentCount];
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iterator
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteAggregate ca = new ConcreteAggregate();
            ITerator it=new ConcreteIterator(ca);
            ca[0] = "自行车";
            ca[1] = "轿车";
            ca[2] = "货车";
            ca[3] = "客车";
            ca[4] = "出租车";
            ca[5] = "人";
            it.Fir();
            while (it.Judge())
            {
                Console.WriteLine("{0}必须遵守交通规则!", it.GetCounts());
                it.Sec();
            }
            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值