程序如下:
import java.util.*;
public class Lianxi09
{
public static void main(String[] args)
{
TreeSet<Student10> ts = new TreeSet<Student10>();
ts.add(new Student10("abc03"));
}
}
class Person10
{
private String name;
Person10(String name)
{
this.name= name;
}
public String getName()
{
return name;
}
public String toString()
{
return "Person:"+name;
}
}
class Student10 extends Person10
{
Student10 (String name)
{
super(name);
}
}
编译时出现
Exception in thread "main" java.lang.ClassCastException: 第十五天.Student10 cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at 第十五天.Lianxi09.main(Lianxi09.java:9)
提问:该如何解决?
|
|