却未实现equals方法呢?
public class AlphaComp implements Comparator {
public int compare(Object o1, Object o2) {
// Assume it's used only for Strings...
String s1 = ((String)o1).toLowerCase();
String s2 = ((String)o2).toLowerCase();
return s1.compareTo(s2);
}
public static void main(String[] args) {
按理说一个非抽象类实现了一个接口不应该实现这个接口全部的方法嘛?可是在think in java实现Comparator
答案:10 悬赏:70 手机版
解决时间 2021-03-27 12:18
- 提问者网友:战魂
- 2021-03-26 22:23
最佳答案
- 五星知识达人网友:洎扰庸人
- 2020-09-05 10:08
真是很细心,这个问题从来没有想过
Ojbect类中的equals
public boolean equals(Object obj) {
return (this == obj);
}现在问题变成了为什么Comparator要有
boolean equals(Object obj);方法
请一定要记住实现了Comparator方法,最好不要覆盖equals方法
下面英文的大概意思是你覆盖了Comparator的equals方法最好是不要把这个类的相关逻辑掺和进来,因为一个普通的类覆盖了equals方法都会检查某些属性是否相等,但是比较器就应该比较比较器本身是否相等
In common java objects, we will decide two objects are equal by checking its one or more than one instance variables.
Form javaDoc Comparator
return true -> only if the specified object is also a comparator and it imposes the same ordering as this comparator.Means we need to check for its business logic (This is what really makes two comparators objects equal)
Also from JavaDoc
Note that it is always *safe not* to override Object.equals(Object).Since the POJO class can have its own equals() which would check for some of its instance variables(Which makes it really unique). If we implement comparator interface in the same POJO class and provide the equals () for comparator then we cannot check equality for POJO class, vice versa. That is the reason it mentioned it's not to override equals().
As explained earlier this is to 'just to expand on the general contract of the method'. So that we would treat comparator equals() is different from POJO equals() .
下面是引用自另一篇文章:
The equals( ) method, shown here, tests whether an object equals the invoking comparator:
boolean equals(Object obj)obj is the object to be tested for equality. The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false.
Overriding equals( ) is unnecessary, and most simple comparators will not do so.
如果覆盖了Comparator是比较他们都是比较器,并且是使用相同的比较策略,所以一般不要覆盖
再说了比较器本身的用途就是用来比较,某些模型不应该实现这个接口,为什么Comparator要麻烦的写出这个方法,是要告诉我们正确覆盖这个equals方法的方式,但是由于Object中已经有这个这个方法的实现了,所以一般实现Comparator是不需要覆盖这个equals方法的,覆盖也是没有意义的,所以thinking in java的作者就正确地没有覆盖这个可以不需要覆盖的方法
Ojbect类中的equals
public boolean equals(Object obj) {
return (this == obj);
}现在问题变成了为什么Comparator要有
boolean equals(Object obj);方法
请一定要记住实现了Comparator方法,最好不要覆盖equals方法
下面英文的大概意思是你覆盖了Comparator的equals方法最好是不要把这个类的相关逻辑掺和进来,因为一个普通的类覆盖了equals方法都会检查某些属性是否相等,但是比较器就应该比较比较器本身是否相等
In common java objects, we will decide two objects are equal by checking its one or more than one instance variables.
Form javaDoc Comparator
return true -> only if the specified object is also a comparator and it imposes the same ordering as this comparator.Means we need to check for its business logic (This is what really makes two comparators objects equal)
Also from JavaDoc
Note that it is always *safe not* to override Object.equals(Object).Since the POJO class can have its own equals() which would check for some of its instance variables(Which makes it really unique). If we implement comparator interface in the same POJO class and provide the equals () for comparator then we cannot check equality for POJO class, vice versa. That is the reason it mentioned it's not to override equals().
As explained earlier this is to 'just to expand on the general contract of the method'. So that we would treat comparator equals() is different from POJO equals() .
下面是引用自另一篇文章:
The equals( ) method, shown here, tests whether an object equals the invoking comparator:
boolean equals(Object obj)obj is the object to be tested for equality. The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false.
Overriding equals( ) is unnecessary, and most simple comparators will not do so.
如果覆盖了Comparator是比较他们都是比较器,并且是使用相同的比较策略,所以一般不要覆盖
再说了比较器本身的用途就是用来比较,某些模型不应该实现这个接口,为什么Comparator要麻烦的写出这个方法,是要告诉我们正确覆盖这个equals方法的方式,但是由于Object中已经有这个这个方法的实现了,所以一般实现Comparator是不需要覆盖这个equals方法的,覆盖也是没有意义的,所以thinking in java的作者就正确地没有覆盖这个可以不需要覆盖的方法
全部回答
- 1楼网友:你可爱的野爹
- 2019-11-03 00:17
在Object里实现了
- 2楼网友:傲气稳了全场
- 2020-08-10 08:13
因为Object里面有个默认的equals实现
- 3楼网友:污到你湿
- 2020-11-15 14:51
1楼是对的,因为已经有equals方法了,所以算作实现了接口中的方法
- 4楼网友:由着我着迷
- 2019-08-21 11:24
事实上所有对象都继承了Object这个基础类,
而object本身就有equals方法
所以相当于每个对象都有equals方法,
所以如果你看到类里面有equals方法,一般都是override的
- 5楼网友:从此江山别
- 2020-07-29 12:08
你好!
事实上所有对象都继承了Object这个基础类,
而object本身就有equals方法
所以相当于每个对象都有equals方法,
所以如果你看到类里面有equa缉供光佳叱簧癸伪含镰ls方法,一般都是override的
仅代表个人观点,不喜勿喷,谢谢。
- 6楼网友:青灯有味
- 2020-04-22 06:30
去新浪看看 哪里有免费领卡和测试帐号的地方 不过抢得人很多 你最好到深夜比如3 .4点上去抢 或早点起 或者直接申请个新浪账号能直接领、、
- 7楼网友:鸽屿
- 2019-08-14 19:27
正好我需要
- 8楼网友:何以畏孤独
- 2020-05-03 18:33
看到这样的问题真是笑喷。“免费领”还收取“材料费300”哪还是免费吗?没发现自己说话都有语病前后矛盾。以后别相信什么免费的。
- 9楼网友:我住北渡口
- 2019-06-05 19:05
B解析Simon,Kitty说她昨天把一本漫画书落在你家里了。把某物落在某地,用leave,昨天发生的,用一般过去式。选B。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯