本帖最后由 回首、后_街 于 2013-5-7 22:46 编辑
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Person> p = new ArrayList<Person>();
p.add(new Person("java01"));
p.add(new Person("java02"));
p.add(new Person("java03"));
p.add(new Person("java04"));
arrnewPerson(p);
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new Student("java01----11"));
stu.add(new Student("java。。。。2"));
arrnewPerson(stu);
}
public static void arrnewPerson(ArrayList<? super Student> al){
for(Iterator<? super Student> it = al.iterator(); it.hasNext();){
System.out.println("....."+((Student)it.next()).show());
}
}
}
运行该程序出现类型转换异常...帮忙问一下。。。
Exception in thread "main" java.lang.ClassCastException: Person cannot be cast to Student
at Test.arrnewPerson(Test.java:30)
at Test.main(Test.java:18)
|