- class Student implements Comparable//该接口强制让学生具备比较性。
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int compareTo(Object obj)
- {
- //return 0;
-
- if(!(obj instanceof Student))
- throw new RuntimeException("不是学生对象");
- Student s = (Student)obj;
- //System.out.println(this.name+"....compareto....."+s.name);
- if(this.age>s.age)
- return 1;
- if(this.age==s.age)
- {
- return this.name.compareTo(s.name);
- }
- return -1;
- /**/
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
复制代码- class MyCompare implements Comparator
- {
- public int compare(Object o1,Object o2)
- {
- Student s1 = (Student)o1;
- Student s2 = (Student)o2;
- int num = s1.getName().compareTo(s2.getName());
- if(num==0)
- {
- return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
- /*
- if(s1.getAge()>s2.getAge())
- return 1;
- if(s1.getAge()==s2.getAge())
- return 0;
- return -1;
- */
- }
-
- return num;
- }
- }
复制代码 这两种比较器有什么区别? |