Java 中的重写 Equals - 对实现感到困惑
我一直在学习 MOOC Java 编程课程,他们通常会像这样重写 equals:
- 如果 2 个对象的地址相同 = 返回 true。
- 如果比较的对象不是与第一个对象相同的类的实例=返回 false。
- 将对象转换为与比较每个对象的变量相同的类型
- - 返回 true 或 false
我只是对步骤 2 和 3 有点困惑。
- 步骤 2 检查第二个对象是否是同一对象的实例type 作为其比较对象...
如果不是,该方法将忽略代码的其余部分并返回 false
如果是,我们将其转换为该类。 所以在这一点上我很困惑为什么我们需要将一个已经是同一个类的实例的对象转换为该类?如果Java识别它是相同的类型,为什么你要这样做?需要转换吗?
*示例如下:
public boolean equals(Object comparedObject) {
// if the variables are located in the same place, they're the same
if (this == comparedObject) {
return true;
}
// if comparedObject is not of type Book, the objects aren't the same **
if (!(comparedObject instanceof Book)) {
return false;
}
// convert the object to a Book object
Book comparedBook = (Book) comparedObject;**
// if the instance variables of the objects are the same, so are the objects
if (this.name.equals(comparedBook.name) &&
this.published == comparedBook.published &&
this.content.equals(comparedBook.content)) {
return true;
}
// otherwise, the objects aren't the same
return false;
}
I've been doing the MOOC Java programming course and they typically override equals like this:
- If addresses of the 2 objects are the same = return true.
- If compared object is not an instance of the same class as the first object = return false.
- Convert the object to the same type as the one being compared to
- Compare the variables of each object - returns true or false
I'm just a bit confused about step 2 and 3.
- Step 2 checks if the 2nd object is an instance of the same type as the one its compared to...
If it isn't, the method ignores the rest of the code and returns false
If it is, we convert it to that Class. So at this point i'm confused about why we need to convert an object that is already an instance of the same class - to that class? If Java recognises it is of the same type, why do you need to convert it?
*Example below:
public boolean equals(Object comparedObject) {
// if the variables are located in the same place, they're the same
if (this == comparedObject) {
return true;
}
// if comparedObject is not of type Book, the objects aren't the same **
if (!(comparedObject instanceof Book)) {
return false;
}
// convert the object to a Book object
Book comparedBook = (Book) comparedObject;**
// if the instance variables of the objects are the same, so are the objects
if (this.name.equals(comparedBook.name) &&
this.published == comparedBook.published &&
this.content.equals(comparedBook.content)) {
return true;
}
// otherwise, the objects aren't the same
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您仅比较了 dynamic 类型; static
比较的类型
是不变的:它是object
,您无法访问其book> book
成员(考虑一下:此时 you 知道,的动态类型
是book
;要使编译器了解对象类型是book
,您首先需要创建一个 static 类型的对象, 是book ,为此,您需要一个演员。
静态类型信息是Java 编译器所看到的,并且是编译器用来检查您的代码是否静态正确的内容。
Because you’ve only compared the dynamic type; the static type of
comparedObject
is unchanged: it’sObject
, and you cannot access itsBook
members (think about it: at this point you know that the dynamic type ofcomparedObject
isBook
; but the Java compiler still sees it with its declared type). To make the compiler understand that the object type isBook
, you first need to create an object whose static type isBook
, and to do that you need a cast.The static type information is what the Java compiler sees, and which is what the compiler uses to check whether your code is statically correct.
Java 是静态类型的,因此如果变量被声明为
Object
,它将不允许您访问Book
的任何字段或调用任何方法。在这种情况下,很容易证明该对象只能是一个Book
,但编译器不会尝试证明它。 (并且,在 更复杂的示例,它不能。)在最新版本的 Java 中,您可以使用语法的快捷方式:
但在早期版本中版本,您只需向下转换实例:
Java is statically typed, so if the variable is declared as
Object
it will not let you access any fields or call any methods ofBook
. In this case, it is easily proven that the object can only be aBook
, but the compiler does not make any attempt to prove it. (And, in a more complicated example, it couldn't.)In the newest versions of Java, there is a shortcut you can take with the syntax:
But in earlier versions, you simply have to downcast the instance: