接口用来描述类的功能,而不指明具体的实现方法。但是Iterator作为一个接口继承与collection中的set,它的方法包含hasnext()next remove,方法又能实现,那么不是与原定义矛盾了吗? 

解决方案 »

  1.   

    难道楼主可以用
    Iterator it=new Iterator()
    这样new出它的对象吗???
      

  2.   

    但是
    Collection c = new HashSet();
    Iterator i = c.iterator();
    i.hasnext(...);
    不是调用了它的方法了吗
      

  3.   

    对啊  没错啊  Iterator接口只有一个没有实现的方法iterator()
    Iterator作为一个接口继承与collection中的set????   这话什么意思...
      

  4.   

    我是强调Iterator是set派生出的接口而已
      

  5.   

    错了吧  Iterator接口是java.lang包里的接口  怎么是set派生的呢?你的这段代码
    Collection c = new HashSet(); 
    Iterator i = c.iterator(); 也不是说set派生Iterator接口啊   而是Collection实现了Iterator接口   
      

  6.   

    不好意思  我弄错了  我刚才说的那个java.lang包里的是Iterable接口  然后它有一个方法是iterator
    而你说的Iterator接口是java.util包下面的  有3个方法hasNext(),next(),remove() 这下应该清楚了吧  呵呵
      

  7.   

    Collection c = new HashSet(); 
    Iterator i = c.iterator(); 
    i.hasnext(...); ========================
    Collection是一个接口,HashSet是Collection的实现类(非抽象类,否则就new操作非法)!
    当调用c.iterator()时,实际是去HashSet类中找到这个方法并执行,该方法的返回值是一个迭代器对象!
    接着的i.hasnext()并不是调用Iterator中的方法,则是调用了由Collection声明、由HashSet类new出的对象c调用HashSet类中的hasnext()方法!!!其实这些只是涉及到了RTTI(运行期类型检查)的知识!自己好好看看书吧!!