avatar
s*y
1
我记得== 只适用于数值比较
对于Object, 要用 a.equal(b)来进行比较
那么对于下面的的code, "==" 应该返回不等; "e . equal ( f )"应该返回相等
可运行的结果却是两个都不相等
== shows e and f are not equal
e . equal ( f ) shows they are not equal
这是什么原因? 谢谢
avatar
B*h
2
o1 == o2 : only when o1 and o2 are the same object.
o1.equals(o2): implemented as {return o2 ==null ? false : o1 == o2;}by defau
lt in Object class.

【在 s******y 的大作中提到】
: 我记得== 只适用于数值比较
: 对于Object, 要用 a.equal(b)来进行比较
: 那么对于下面的的code, "==" 应该返回不等; "e . equal ( f )"应该返回相等
: 可运行的结果却是两个都不相等
: == shows e and f are not equal
: e . equal ( f ) shows they are not equal
: 这是什么原因? 谢谢

avatar
s*y
3
e.equal(f) 在这个case里, 依你说, 是应该等还是不等呢?

defau

【在 B*********h 的大作中提到】
: o1 == o2 : only when o1 and o2 are the same object.
: o1.equals(o2): implemented as {return o2 ==null ? false : o1 == o2;}by defau
: lt in Object class.

avatar
B*h
4
were you reading my post?

【在 s******y 的大作中提到】
: e.equal(f) 在这个case里, 依你说, 是应该等还是不等呢?
:
: defau

avatar
m*y
5
你要在Officer class里override那个equals方法
只有你自己知道应该怎么判断相等的标准
否则by default用==比较 就是不相等

【在 s******y 的大作中提到】
: 我记得== 只适用于数值比较
: 对于Object, 要用 a.equal(b)来进行比较
: 那么对于下面的的code, "==" 应该返回不等; "e . equal ( f )"应该返回相等
: 可运行的结果却是两个都不相等
: == shows e and f are not equal
: e . equal ( f ) shows they are not equal
: 这是什么原因? 谢谢

avatar
s*y
6
Of course, but still a little confused, from what you said
e.equal(f) still should be true.
But why did the code return false instead

【在 B*********h 的大作中提到】
: were you reading my post?
avatar
B*h
7
how are e and f THE same object?
e = new Officer(...);
f = new Officer(...);
different objects.

【在 s******y 的大作中提到】
: Of course, but still a little confused, from what you said
: e.equal(f) still should be true.
: But why did the code return false instead

avatar
m*y
8
As BulletTooth said,
the default impl of equals is
public boolean equals(Object obj) {
return (this == obj);
}
If u don't override, u will always get "false"...

【在 s******y 的大作中提到】
: Of course, but still a little confused, from what you said
: e.equal(f) still should be true.
: But why did the code return false instead

avatar
s*y
9
I see. Thanks you guys


【在 m******y 的大作中提到】
: As BulletTooth said,
: the default impl of equals is
: public boolean equals(Object obj) {
: return (this == obj);
: }
: If u don't override, u will always get "false"...

avatar
s*y
10
thanks a lot.

【在 B*********h 的大作中提到】
: how are e and f THE same object?
: e = new Officer(...);
: f = new Officer(...);
: different objects.

avatar
q*g
11
one remider: always overide hashCode() method when overide() equals method

【在 s******y 的大作中提到】
: thanks a lot.
avatar
r*l
12
Look at the contractual relation between equals() and hash().

【在 s******y 的大作中提到】
: 我记得== 只适用于数值比较
: 对于Object, 要用 a.equal(b)来进行比较
: 那么对于下面的的code, "==" 应该返回不等; "e . equal ( f )"应该返回相等
: 可运行的结果却是两个都不相等
: == shows e and f are not equal
: e . equal ( f ) shows they are not equal
: 这是什么原因? 谢谢

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。