C#迭代器的实现
2016-12-05 10:58
260 查看
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 items = new List();//创建存放需要迭代的目标
public int count//设置属性,列表中的元素数量
{
get{ return items.Count; }
}
public object this[int index]//创建检索目录
{
get { return items[index];}
set { items.Insert(index,value);}
}
}
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;
}
}
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();
}
}
}
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 items = new List();//创建存放需要迭代的目标
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();
}
}
}
相关文章推荐
- C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器
- C#学习之用迭代器实现枚举器
- C#迭代器的实现
- C#学习笔记(三)—–C#高级特性:实现迭代器的捷径
- 【C#】如何实现一个迭代器
- C# 迭代器实现枚举器
- C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器
- C#中迭代器的概念和两种实现方式
- C# in depth (第六章 实现迭代器的捷径)
- C#中foreach语句的迭代器实现机制
- 二、C++迭代器的两种实现方式 (Range for和C#、Java中的foreach)
- C#学习之用迭代器实现枚举器
- C#中foreach语句的迭代器实现机制
- c# Yield return之简单方法实现迭代器和集合初始化器
- C# Iterator迭代器的实现方式
- C#中实现迭代器
- C#用yield实现简单的迭代器
- 了解 C# foreach 内部语句和使用 yield 实现的自定义迭代器
- C#中实现迭代器
- 数据结构与算法(C#实现)系列---树(二)