接口源码:
publicinterfaceEnumeration<E>{
boolean hasMoreElements();
E nextElement();
}
由此可见,接口定义了两个函数:
1:是否有更多元素
2:获取下一个元素
附上自己做的demo:
package com.wang.interfaceTest;
import java.util.Enumeration;
/**
* 此类用于测试枚举接口
* @author Administrator
*
*/
publicclassEnumerationTestimplementsEnumeration<String>{
privateint count;
privateint length;
privateString[] arr;
publicEnumerationTest(int count,int length,String[] arr){
this.count = count;
this.length = length;
this.arr = arr;
}
@Override
publicboolean hasMoreElements(){
return(count<length);
}
@Override
publicString nextElement(){
returnthis.arr[this.count++];
}
publicstaticvoid main(String[] args){
String[] myArr =newString[]{"哈哈","呵呵","嘿嘿","嘎嘎","哇哇","咩咩"};
EnumerationTest et =newEnumerationTest(0, myArr.length, myArr);
while(et.hasMoreElements()){
System.out.println(et.nextElement());
}
}
}
运行结果:
哈哈
呵呵
嘿嘿
嘎嘎
哇哇
咩咩