谁能告诉我内部类的目的是什么?同样在设计迭代器模式时,我们是否必须使用内部类?使用内部类会更好吗?
您不需要将内部类用于迭代器模式:
import java.util.*;
public class BitSetIterator implements Iterator<Boolean> {
private final BitSet bitset;
private int index;
public BitSetIterator(BitSet bitset) {
this.bitset = bitset;
}
public boolean hasNext() {
return index < bitset.length();
}
public Boolean next() {
if (index >= bitset.length()) {
throw new NoSuchElementException();
}
boolean b = bitset.get(index++);
return new Boolean(b);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句