ClassCastExcep
package GenericDemo;
import java.util.Iterator;
import java.util.TreeSet;
public class GenericDemo1 {
public static void main(String[] args){
TreeSet<Person1> al1=new TreeSet<Person1>();
al1.add(new Person1("sfdf",23));
al1.add(new Person1("adf",24));
al1.add(new Person1("rfdf",25));
TreeSet<Worker1> al2=new TreeSet<Worker1>();
al1.add(new Worker1("tf",23));
al1.add(new Worker1("4df",24));
al1.add(new Worker1("yfdf",25));
TreeSet<Student1> al3=new TreeSet<Student1>();
al1.add(new Student1("sfdf",23));
al1.add(new Student1("adf",24));
al1.add(new Student1("rfdf",25));
Iterator<Worker1> it=al2.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
package GenericDemo;
public class Student1 extends Person1 {
public Student1() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student1 [toString()=" + super.toString() + "]";
}
public Student1(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
}
package GenericDemo;
public class Worker1 extends Person1 {
public Worker1() {
super();
// TODO Auto-generated constructor stub
}
public Worker1(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return super.toString();
}
}
------解决方案--------------------ClassCaseException 类型转换异常,楼主哪里用到了类型转换,仔细看看。
------解决方案--------------------TreeSet<Worker1> al2=new TreeSet<Worker1>();
al1.add(new Worker1("tf",23));
al1.add(new Worker1("4df",24));
al1.add(new Worker1("yfdf",25));
TreeSet<Student1> al3=new TreeSet<Student1>();
al1.add(new Student1("sfdf",23));
al1.add(new Student1("adf",24));
al1.add(new Student1("rfdf",25));
是不是写错了?
------解决方案--------------------是的,粘贴粘的..