只要你的C类实现了 I 接口,你就可以用I i = new C() 来指向C的实例

解决方案 »

  1.   

    也就是说,I 和 C 的引用都可以指像一个C的实例
      

  2.   

    Vector的Enumeration方法返回一个实现了Enumeration接口的匿名类
    Vector部分源代码如下    public Enumeration elements() {
    return new Enumeration() {
        int count = 0;     public boolean hasMoreElements() {
    return count < elementCount;
        }     public Object nextElement() {
    synchronized (Vector.this) {
        if (count < elementCount) {
    return elementData[count++];
        }
    }
    throw new NoSuchElementException("Vector Enumeration");
        }
    };    public Enumeration elements() {
    return new Enumeration() {
        int count = 0;     public boolean hasMoreElements() {
    return count < elementCount;
        }     public Object nextElement() {
    synchronized (Vector.this) {
        if (count < elementCount) {
    return elementData[count++];
        }
    }
    throw new NoSuchElementException("Vector Enumeration");
        }
    };    public Enumeration elements() {
    return new Enumeration() {
        int count = 0;     public boolean hasMoreElements() {
    return count < elementCount;
        }     public Object nextElement() {
    synchronized (Vector.this) {
        if (count < elementCount) {
    return elementData[count++];
        }
    }
    throw new NoSuchElementException("Vector Enumeration");
        }
    };Interface是Java把世界的实现和描述分离的有力工具!
      

  3.   

    public Enumeration elements() {
    return new Enumeration() 
    }Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实例化吗?在哪定义?
      

  4.   

    public Enumeration elements() {
    return new Enumeration()

    ...........
    }return new Enumeration()这里的Enumeration()不是构造函数,它就是一个接口(一个新的对象),是elements()的返回值,在后面实现了该接口,所以接口的函数就可以用了。
      

  5.   

    >public Enumeration elements() {
    > return new Enumeration() 
    >}
    >
    >Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实
    >例化吗?在哪定义
    不好意思,我忘记告诉你那是个匿名类了
    new Enumeration() 
             {//do something
                 int ....
                 public void find()....
                 .......
    }
    这相当于
    class XXX implements Enumeration
    {
      //........
    }
    new XXX
    也可以用extends class的
    IE一变慢,我不小心连贴了三次