collection作为一种容器 被定义为接口,但是这个接口里怎么会有具体的方法,接口里不是只有方法的声明 ,没有定义吗 是因为系统给的接口特殊?

解决方案 »

  1.   

    你哪里看出来collection有方法的?
      

  2.   

    LZ你看到的是Collection还是Collections??????????
      

  3.   

    lz眼花了,把Collections看成Collection了...
      

  4.   

    楼主看错了..你看的是Collections的方法...
      

  5.   

    java.util.Collection  里面的方法可以直接使用呀 是定义过的 下面的程序里的add remove不都是collection的方法?
    import java.util.*;public class BasicContainer {
        public static void main(String[] args) {
            Collection c = new HashSet();
            c.add("hello");
            c.add(new Name("f1","l1"));
            c.add(new Integer(100));
            c.remove("hello"); 
            c.remove(new Integer(100));
            System.out.println
                      (c.remove(new Name("f1","l1")));
            System.out.println(c);
        }
    }class Name implements Comparable {
        private String firstName,lastName;
        public Name(String firstName, String lastName) {
            this.firstName = firstName; this.lastName = lastName;
        }
        public String getFirstName() {  return firstName;   }
        public String getLastName() {   return lastName;   }
        public String toString() {  return firstName + " " + lastName;  }
        

    }
      

  6.   

    这是HashSet的方法 这叫多态
      

  7.   

    Collection c = new HashSet(); 
    这里使用的明明是HashSet的方法。
    接口只有方法约定和静态常量,没有方法体。
    如果楼主想进一步证实,请打开jdk安装目录下的src.zip,找到java.util.Collection.java这个文件,看看到底有没有方法体。
      

  8.   

     才看到 collection是有子类的 呵呵 方法都是在子类中重载了吧。。感谢大家