本帖最后由 马晓平 于 2013-10-27 23:12 编辑
遇见编译错误了,毕老师的视频里是编译通过的,但是我自己编译就出现错误了自己找了半天没找到也不知道怎么回事请教一下大家
一下是我在eclipse中代码
import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add(new Student("lisio2",22));
ts.add(new Student("lisio3",20));
ts.add(new Student("lisio4",24));
//ts.add(new Student("lisio5",19));
//ts.add(new Student("lisio6",18));
Iterator it=ts.iterator();
while(it.hasNext())
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"...."+stu.getAge());
}
}
}
class Student implements Comparable
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int comparaTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
if(this.age>s.age)
return 1;
if(this.age==s.age)
return 0;
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
提示有一个错误异常
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The type Student must implement the inherited abstract method Comparable.compareTo(Object)
可是毕老师为何是编译成功的呢有点不理解了
|