java.util.Vector中的$ 1

Sud*_*han 4 java enumeration inner-classes

enumeration e=vector.elements
Run Code Online (Sandbox Code Playgroud)

但是vector类没有实现Enumeration,那么它是如何返回Enumeration Reference的.但是e引用了java.util.vector $ 1.什么是"Vector $ 1"???

ska*_*man 6

Vector$1是一个匿名类.Vector.elements()创建此匿名类的新实例,该实例实现Enumeration接口.

这是源代码Vector.elements()(像往常一样格式错误):

public Enumeration<E> elements() {
return new Enumeration<E>() {
    int count = 0;

    public boolean hasMoreElements() {
    return count < elementCount;
    }

    public E nextElement() {
    synchronized (Vector.this) {
        if (count < elementCount) {
        return (E)elementData[count++];
        }
    }
    throw new NoSuchElementException("Vector Enumeration");
    }
};
}
Run Code Online (Sandbox Code Playgroud)