枚举接口(interface)有方法hashMoreElements与引用变量(Enumv)一起使用,它们如何使用 因为它们没有实现? 我的意思是它是一个接口(interface)方法,那么如何调用它 - Enumv.hasMoreElements() 它没有实现。
Vector v = new Vector();
//v contains list of elements - Suppose
Enumeration Enumv = v.elements();
while(Enumv.hasMoreElements()) {
System.out.println(Enumv.nextElement());
}
这怎么可能?
最佳答案
对于您的代码,
Enumeration Enumv = v.elements();
这就是 Vector
返回给您的内容。
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");
}
};
}
如您所见,Vector 返回一个已实现的 Enumeration
类(对开发人员来说是匿名的)。
so how can it be called - Enumv.hasMoreElements() it does not have a implementation .
已实现(如上所示)。
关于java - 枚举方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3769990/