小弟前日看了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");
    }
};
    } 
请问:return new Enumeration() {}   这个语句是什么意思呢?

解决方案 »

  1.   

    匿名内部类的用法,相当于class XXX implements 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");
        }
    }return new XXX();