如果有一个StudentUnion
类定义了相应的比较器,并且当它成为Student
类的成员对象时,它希望以相同的因素在集合中排序(按学生会排序),那么如何避免重新编码另一个比较器几乎和StudentUnionComparator
? 这种情况有没有最好的设计?
public class Student{
....
private StudentUnion;
....
}
public class StudentUnionComparator implements Comparator<StudentUnion>{
public int compare(StudentUnion s1, StudentUnion s2){
....
return result // 1 or 0;
}
}