如何调用枚举的方法
How is it possible to call methods of Enumeration
在枚举API中给出的示例中有以下示例:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
但是当 Enumeration 是一个接口时,如何在向量的元素上调用这些方法?
我的意思是这些方法没有主体(实现)?
Enumeration<E> e = v.elements();
这意味着 elements()
方法 returns 一个 class 实现 Enumeration
或 returns 该实例的 annonymous innerclass
。
这里是Vector
classeselements()
方法的源代码
public Enumeration<E> More ...elements() {
312 return new Enumeration<E>() {
313 int count = 0;
314
315 public boolean More ...hasMoreElements() {
316 return count < elementCount;
317 }
318
319 public E More ...nextElement() {
320 synchronized (Vector.this) {
321 if (count < elementCount) {
322 return elementData(count++);
323 }
324 }
325 throw new NoSuchElementException("Vector Enumeration");
326 }
327 };
328 }
如果您看到它正在返回 return new Enumeration<E>() {
在枚举API中给出的示例中有以下示例:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
但是当 Enumeration 是一个接口时,如何在向量的元素上调用这些方法?
我的意思是这些方法没有主体(实现)?
Enumeration<E> e = v.elements();
这意味着 elements()
方法 returns 一个 class 实现 Enumeration
或 returns 该实例的 annonymous innerclass
。
这里是Vector
classeselements()
方法的源代码
public Enumeration<E> More ...elements() {
312 return new Enumeration<E>() {
313 int count = 0;
314
315 public boolean More ...hasMoreElements() {
316 return count < elementCount;
317 }
318
319 public E More ...nextElement() {
320 synchronized (Vector.this) {
321 if (count < elementCount) {
322 return elementData(count++);
323 }
324 }
325 throw new NoSuchElementException("Vector Enumeration");
326 }
327 };
328 }
如果您看到它正在返回 return new Enumeration<E>() {