代码学习JAVA迭代子模式的使用
看下面的代码学习JAVA迭代子模式(Iterator)的使用方法 public abstract class Aggregate
{
public abstract Iterator createIterator();
}
public interface Iterator
{
void first();
void next();
boolean isDone();
Object currentItem();
}
public class ConcreteAggregate extends Aggregate
{
private Object[] objs={"Monk Tang","Mo...全部
看下面的代码学习JAVA迭代子模式(Iterator)的使用方法 public abstract class Aggregate
{
public abstract Iterator createIterator();
}
public interface Iterator
{
void first();
void next();
boolean isDone();
Object currentItem();
}
public class ConcreteAggregate extends Aggregate
{
private Object[] objs={"Monk Tang","Monkey","Pigsy","Sandy","Horse"};
public Iterator createIterator()
{
return new ConcreteIterator();
}
private class ConcreteIterator implements Iterator
{
private int currentIndex=0;
public void first()
{
currentIndex=0;
}
public void next()
{
if(currentIndex=objs。
length);
}
public Object currentItem()
{
return objs[currentIndex];
}
}
}
public class Client
{
private Iterator it;
private Aggregate agg=new ConcreteAggregate()
public void operation()
{
it=agg。
createIterator();
while(!it。isDone())
{
System。out。println(it。currentItem()。toString());
it。next();
}。
收起