比较器可以比较两个不同类型的对象吗? [英] Can Comparator compare two objects of different type?

查看:108
本文介绍了比较器可以比较两个不同类型的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Person和Employee类的两个对象。
这两个类都有共同的属性年龄。
并且我已经将这两个类的几个对象添加到Arraylist中,现在我想两个对象编写一个Comparator并将其传递给Collections类的sort方法。
并且希望根据年龄对列表进行排序。
我尝试这样做只是为了更清楚地了解Java中可比和比较器的使用。

I have two objects of classes Person and Employee. Both classes have common attribute age. And I have added few objects of both theses classes to an Arraylist and now I want two write one Comparator and pass it to sort method of Collections class. And want the list to be sorted on the basis of age. I am trying this just for getting more clarity on use of Comparable and Comparator in Java.

编辑:
原因为什么我问这个问题是我不清楚比较器和可比性。
我在某处读到,如果Class实现Comparable,则无法将其与其他类的对象进行比较(由于Class强制转换异常)。

Reason Why I was asking this question was that I was not clear about Comparator and Comparable. I read somewhere that If Class implements Comparable then it can not be compared against objects of other classes (Because of Class cast exception).

现在,如果我创建关系,然后就不需要实现Comparator(除非我要根据这些类的名称或其他公共属性进行排序)。

Now if I create relation ship in Employee and Person then there is no need to implement Comparator (Unless I want to do sorting on the basis of name or other common attribute of these classes).

再次,我问这个问题以使比较器和可比对象更加清晰。

Once Again, I have asked this question to get more clarity on Comparator and Comparable.

到现在,我一直能够理解的是,如果我想进行排序

Till now, What I have been able to understand is that If I want to do sorting on more than one parameters then I should implement Comparator and pass it to Collections.srot().

或者如果我对要排序的对象的类没有控制权,则应该实现Comparator并将其传递给Collections.srot()。 ,然后自定义排序我应该实现Comparator。
(如果我错了或者我错过了什么,请纠正我)

Or If I don't have control over the Class of object being sorted, then custom sorting I should implement Comparator. (Correct me if I am wrong or I am missing something)

编辑

我想现在我理解了Comparable和Comparator的用法。
为了确保我有正确的概念,这是我的理解:
如果我想比较两个不同类的对象(并且没有关系),则应该使用原始比较器并重写compare方法。
有人实施Comparator而不是Comparable的另一个原因是
具有排序的灵活性。
我从自然排序中了解的是CompareTo方法中Class实现的逻辑。

I think now I understand the use of Comparable and Comparator. To make sure I got the right concepts, here is what I understand: If I want to compare two objects of different classes (and having no relation ) then I should use raw comparator and override the compare method. Another reason why somebody would implement Comparator instead of Comparable is to have flexibility in sorting. And what I understand from natural sorting is the logic implemented by Class in CompareTo method.

希望我对所有这些观点都是正确的。

Hope I am correct with all these points.

推荐答案

您可以定义一个名为Ageable的接口,而Employee和Person可以实现此接口。您还可以定义一个具有相同效果的抽象类(并且Employee和Person将从该抽象类继承)。请注意,由于这个原因,您也可以使用普通的类,您可以执行所需的任何操作。

You can define an interface called Ageable and Employee and Person can implement this interface. Also you can define an abstract class (and Employee and Person will be inherited from the abstract class) with the same effect. Note that you can also use a normal class for this reason, you can do whatever you need.

此外,如果没有理由说一个雇员不是人员(例如,机器人在办公室工作),则可以从人员继承员工。

Also, if there is no reason to say that an Employee is not a Person (for instance robots are working at the office) then Employee can be inherited from Person.

这篇关于比较器可以比较两个不同类型的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆