[行为型设计模式:游标模式]
来源:互联网 发布:mac cocoapods 升级 编辑:程序博客网 时间:2024/06/03 17:28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
int Current { get; }//原代码是OBJECT类型
bool MoveNext();
void Reset();
}
public class MyCollection : IEnumerable
{
int[] items;
public MyCollection()
{
items = new int[5] { 3, 5, 5, 8, 10 };
}
public IEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
public class MyEnumerator : IEnumerator
{
int nindex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nindex = -1;
}
public bool MoveNext()
{
nindex++;
return (nindex < collection.items.GetLength(0));
}
public int Current
{
get
{
return (collection.items[nindex]);
}
}
public void Reset()
{
nindex = 0;
}
}
}
public class APPFORITERATOR
{
public string ITERATOR()
{
MyCollection col = new MyCollection();
string strReturn = string.Empty;
//foreach (int i in col)
//{
// strReturn = strReturn + i.ToString() + ",";
//}
///下面的WHILE循环等价与上面的屏蔽代码
IEnumerator ico = col.GetEnumerator();
while (ico.MoveNext())
{
int i = (int)ico.Current;
strReturn = strReturn + i.ToString() + ",";
}
return strReturn;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
int Current { get; }//原代码是OBJECT类型
bool MoveNext();
void Reset();
}
public class MyCollection : IEnumerable
{
int[] items;
public MyCollection()
{
items = new int[5] { 3, 5, 5, 8, 10 };
}
public IEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
public class MyEnumerator : IEnumerator
{
int nindex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nindex = -1;
}
public bool MoveNext()
{
nindex++;
return (nindex < collection.items.GetLength(0));
}
public int Current
{
get
{
return (collection.items[nindex]);
}
}
public void Reset()
{
nindex = 0;
}
}
}
public class APPFORITERATOR
{
public string ITERATOR()
{
MyCollection col = new MyCollection();
string strReturn = string.Empty;
//foreach (int i in col)
//{
// strReturn = strReturn + i.ToString() + ",";
//}
///下面的WHILE循环等价与上面的屏蔽代码
IEnumerator ico = col.GetEnumerator();
while (ico.MoveNext())
{
int i = (int)ico.Current;
strReturn = strReturn + i.ToString() + ",";
}
return strReturn;
}
}
}
- [行为型设计模式:游标模式]
- 设计模式-行为型模式
- [设计模式]--行为型模式
- 设计模式:行为型模式
- 设计模式--行为型模式
- 设计模式--行为型模式
- 设计模式----行为型模式
- 设计模式 -- 行为型模式
- 行为型设计模式
- 行为型设计模式
- 设计模式-行为型
- 行为型设计模式
- 设计模式 - 行为型模式 - 命令模式
- 设计模式 - 行为型模式 - 迭代器模式
- 设计模式 - 行为型模式 - 备忘录模式
- 设计模式 - 行为型模式 - 观察者模式
- 设计模式 - 行为型模式 - 状态模式
- 设计模式 - 行为型模式 - 策略模式
- 有关Java 异常处理中的知识总结
- log4简单配置说明
- MacOS常用命令大全
- log4j配置说明
- log4j用法
- [行为型设计模式:游标模式]
- java反射机制和泛型原理区别
- Javascript继承机制的设计思想
- Myeclipse配置servlet和jsp模板代码
- PHP获取表单里各项值总结
- HTTP响应头信息
- z资治通鉴,卷六十六,孙权如何劝吕蒙好好学习
- java令牌生成器
- TCP/IP Illustrated vol1 笔记 2MSL Wait State