Java 中的重写 Equals - 对实现感到困惑

发布于 2025-01-18 05:57:03 字数 1181 浏览 2 评论 0原文

我一直在学习 MOOC Java 编程课程,他们通常会像这样重写 equals:

  1. 如果 2 个对象的地址相同 = 返回 true。
  2. 如果比较的对象不是与第一个对象相同的类的实例=返回 false。
  3. 将对象转换为与比较每个对象的变量相同的类型
  4. - 返回 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:

  1. If addresses of the 2 objects are the same = return true.
  2. If compared object is not an instance of the same class as the first object = return false.
  3. Convert the object to the same type as the one being compared to
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国际总奸 2025-01-25 05:57:03

因为您仅比较了 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’s Object, and you cannot access its Book members (think about it: at this point you know that the dynamic type of comparedObject is Book; but the Java compiler still sees it with its declared type). To make the compiler understand that the object type is Book, you first need to create an object whose static type is Book, 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.

哽咽笑 2025-01-25 05:57:03

Java 是静态类型的,因此如果变量被声明为 Object,它将不允许您访问 Book 的任何字段或调用任何方法。在这种情况下,很容易证明该对象只能是一个Book,但编译器不会尝试证明它。 (并且,在 更复杂的示例,它不能。)

在最新版本的 Java 中,您可以使用语法的快捷方式:

if (comparedObject instanceof Book book) {
    // do whatever with book
    return this.title.equals(book.title) && this.author.equals(book.author);
}

但在早期版本中版本,您只需向下转换实例:

if (comparedObject instanceof Book) {
    Book book = (Book) comparedObject;
    return this.title.equals(book.title) && this.author.equals(book.author);
}

Java is statically typed, so if the variable is declared as Object it will not let you access any fields or call any methods of Book. In this case, it is easily proven that the object can only be a Book, 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:

if (comparedObject instanceof Book book) {
    // do whatever with book
    return this.title.equals(book.title) && this.author.equals(book.author);
}

But in earlier versions, you simply have to downcast the instance:

if (comparedObject instanceof Book) {
    Book book = (Book) comparedObject;
    return this.title.equals(book.title) && this.author.equals(book.author);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文