[英]Regarding Equals in java with Objects and Classes
Lets say in java I have a Class A and a Class B which extends A.让我们说在java中我有一个A类和一个扩展A的B类。
Note: classes A and B don't have an equals method in them!注意:A 类和 B 类中没有equals 方法!
I do:我愿意:
A a = new B();
B b = new B();
a.equals(b);
It comes out false.结果是假的。 I thought when we did equals it dynamically bounded to a and b which would make it true.
我认为当我们做 equals 时,它会动态地绑定到 a 和 b 上,这将使它成为真的。 Or is it because at compile time it looks at a and assumes equals would be equals(A) and since its a B for B b = new B();
或者是因为在编译时它查看 a 并假设 equals 将是 equals(A) 并且因为它的 a B for B b = new B(); it says false?
说是假的? Or am i wrong on both fronts?
或者我在两个方面都错了?
As you are not overriding equals
then it will use Object
's method由于您没有覆盖
equals
那么它将使用Object
的方法
If you have a look at the source code for Object如果您查看 Object 的源代码
@param obj the reference object with which to compare.
public boolean equals(Object obj) {
return (this == obj);
}
Obviously a
has a different refernce to b
thus false
显然
a
对b
有不同的引用,因此为false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.