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