class Student implements Comparable
{
private String name;
private int age;
Student(String name , int age )
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public void sop(Object obj)
{
System.out.println(obj);
}
}
class MyCompare implements Comparator
{
public int compare(Object o1 , Object o2 )
{
if(!(o1 instanceof Student)||!(o2 instanceof Student))
throw new RuntimeException("不是学生对象!");
Student s1 = (Student)o1;
Student s2 = (Student)o2;
return s1.getName().compareTo(s2.getName());
}
}
在这段小代码中,s1.getName().compareTo(s2.getName()),这句我不理解?compareTo()这个
方法是怎么用的?还有这句return this.name.compareTo(stu.name); ,this.name返回的
就是String型的name值啊,能直接调用compareTo方法吗?请高手赐教,拜托拜托啊! |