class="java"> import java.util.Enumeration; import java.util.Vector; class Cat2{ private int catNumber; Cat2(int i){ catNumber = i; } void print(){ System.out.println("Cat number " + catNumber); } } class Dog2{ private int dogNumber; Dog2(int i){ dogNumber = i; } void print(){ System.out.println("Dog number " + dogNumber); } } public class EnumerationTest { @SuppressWarnings("unchecked") public static void main(String args[]){ @SuppressWarnings("rawtypes") Vector cats = new Vector(); //集合是使用 for(int i=0;i<7;i++){ cats.addElement(new Cat2(i)); //添加元素的方法 } cats.addElement(new Dog2(7)); @SuppressWarnings("rawtypes") Enumeration e = cats.elements(); //枚举的前提 while(e.hasMoreElements()){ //枚举元素是判断 ((Cat2)e.nextElement()).print(); //获得元素的方法 } } } //运行时会有异常发生: //Exception in thread "main" java.lang.ClassCastException: //Dog2 cannot be cast to Cat2 //at EnumerationTest.main(EnumerationTest.java:36) //有兴趣的可以思考下原因?其实此问题实在是简单···