正在加载图片...
package java. util.Iterator; public interface Iterator boolean hasNexto; I Object nexto throws NoSuch Element Exception void remove throws NoSuch ElementException 古色 public static Iterator walkThrough (final Objectl objs) return new Iterator private int pos=0; public boolean hasNextor return(pos<objs length) public Object nextot if(pos>=objs length) throw new NoSuch Element Exception 0; turn obispos++l; public void removed..Anonymous Classes (匿名类) • 匿名类是一种特殊的局部类 • 匿名类通常作为抽象类或接口的实现类使用 • 匿名类不能定义自己的构造函数,只能使用 父类或默认的构造函数 • 匿名类对象的创建的同时也提供了对接口或 抽象类的实现 public static Iterator walkThrough(finalObject[] objs){ return new Iterator(){ private int pos=0; public boolean hasNext(){ return (pos<objs.length); } public Object next(){ if(pos>=objs.length) throw new NoSuchElementException(); return objs[pos++]; } public void remove(){…} } } package java.util.Iterator; public interface Iterator{ boolean hasNext(); Object next() throws NoSuchElementException; void remove() throws NoSuchElementException; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有