关于Java中的equals()方法,以下哪些是正确的?

huangapple go评论98阅读模式
英文:

Which of the following are correct regarding an equals() method in java?

问题

以下是翻译好的内容:

这是我练习考试中的一个让我困惑的问题。

可能的答案

  1. 在名为C的类中编写的equals()方法必须具有一个声明为类型为C的参数,以便覆盖Object类中的equals()方法。

  2. equals()方法必须具有一个声明为类型为Object的参数,以便覆盖Object类中的equals()方法。

  3. 在名为C的类中编写的equals()方法必须将其参数强制转换为Object,即使其参数声明为类型为C,否则将无法正确比较当前对象的字段与参数对象的字段。

  4. 设计良好的equals()方法应检查并处理当前对象可能为null的情况。

  5. 子类中的equals()方法不能直接引用超类中的私有字段进行比较,因此应调用超类的equals()方法来执行此操作。

答案是 #2 和 #5。我不明白为什么 #1 是错误的,而 #2 是正确的。难道不需要将一个类型C与另一个类型C 进行比较吗?为什么 #5 是正确的?我根本不理解这个。子类不能访问超类的私有字段吗?

英文:

This is a question on one of my practice exams that has me stumped.

Potential Answers

  1. An equals() method written in a class named C must have a parameter that is declared to be of type C, in order to override the equals() method in the Object class.

  2. An equals() method must have a parameter that is declared to be of type Object, in order to override the equals() method in the Object class.

  3. An equals() method written in a class named C must cast its parameter to Object, even if its parameter is declared to be of type C, otherwise it will not be able to properly compare the fields of current object to those of the parameter object.

  4. A well-designed equals() method should check for and handle the case where its current object may be null

  5. An equals() method in a subclass cannot refer directly to private fields in a superclass in order to compare them, so it should invoke the superclass equals() method to do this

The answer is #2 & #5. I don't understand why #1 is wrong and #2 is correct. Don't you have to compare one type C with another type C? And why is #5 correct. I don't understand this one at all. Can't subclasses access the private fields of a superclass?

答案1

得分: 2

我不明白为什么 #1 是错误的

因为:

  1. Object::equals 被定义为 public boolean equals(Object),并且

  2. public boolean equals(SomeType) 并未覆盖 public boolean equals(Object)。实际上这是一个重载而不是覆盖。它们是不同的操作。


子类不能访问超类的私有字段吗?

一般情况下,不可以。请查阅您的讲座笔记。

但有一些例外情况,涉及子类被声明在超类内部。


从技术上讲,答案 #5 只对了一半。

  • 如果我们保持在纯粹的 Java 范围内,子类无法访问超类中的私有字段,这是正确的。Java 访问规则禁止了这种情况。(但参见上面的内容。)

  • 调用 super.equals(other) 可能是一个解决方案,事实上,通常它是正确的解决方案。

  • 但错误的是暗示 super.equals(other) 是唯一的解决方案。

其他可能的解决方案可能包括:

  • 在测试相等性时忽略超类字段,
  • 如果存在的话,使用公共的 getter 方法来访问超类的私有字段,
  • 通过违反抽象的反射来访问私有字段。

在这些替代方案中,第一个方案很少适用,第三个方案则是一个非常糟糕的想法。但是这些替代方案意味着答案 #5(如写的那样)在技术上是不正确的。

英文:

> I don't understand why #1 is wrong

Because:

  1. Object::equals is defined as public boolean equals(Object), and

  2. public boolean equals(SomeType) does not override public boolean equals(Object). This is actually an overload not an override. They are different operations.


> Can't subclasses access the private fields of a superclass?

In general, no they can't. Check your lecture notes.

There are some exceptions that involve the subclass being declared inside the superclass.


Technically, answer #5 is only half right.

  • It is correct that the subclass cannot access private fields in the superclass, assuming we stay within the bounds of pure Java. The Java access rules forbid this. (But see above.)

  • It is correct that calling super.equals(other) could be a solution. And indeed, it usually is the correct solution.

  • It is incorrect to imply that calling super.equals(other) is the only solution.

Other possible solutions may include:

  • ignoring the superclass fields when testing for equality,
  • accessing the private fields of the superclass using public getters ... if they exist, or
  • accessing the private fields by abstraction-breaking reflection.

Of the alternatives, the first one is rarely applicable and the third is a really bad idea. But these alternatives mean that Answer #5 (as written) is technically incorrect.

答案2

得分: 1

如果这是您的实际考试:您的课程材料没有涵盖这部分内容吗?

至于第一点:方法签名是public boolean equals(Object obj),所以这是您需要重写的内容。使用其他签名将不会是重写,而是“完全不同的方法”。

至于第五点:这就是private的意思。这些字段对于该类是私有的,并且仅限于该类访问。如果子类需要访问它们,那么它们必须是protected,而不是private。

英文:

If this is your practical exam: did your course material not cover this?

As for #1: the method signature is public boolean equals(Object obj), so that's what you have to override. Any other signature and it's not an override, it's "a completely different method".

As for #5: That's what private means. The fields are private to that class and only that class. If a subclass should have access to them, then they'd have to be protected, not private.

答案3

得分: 0

考虑以下 C 类的 equals 方法。

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    C c = (C) o;
    return id == c.id;
}

1 和 5 选项的解释:

  1. 如果你声明参数的类型为 C,那么它将不会是 overriding(覆盖)。

    错误:

> 方法没有覆盖其超类中的方法

> 表示方法声明旨在覆盖超类型中的方法声明。如果一个方法带有这个注解类型,编译器需要生成一个错误消息,除非满足以下至少一个条件:
该方法确实覆盖或实现了在超类型中声明的方法。
该方法具有与 Object 中任何公共方法的重写等效的签名。

  1. 超类无法访问类的私有字段,因此它们需要调用超类的 equals 方法来比较它们。
英文:

Consider the following equals method for C class.

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        C c= (C) o;
        return id == c.id;
    }

Explanation for 1 & 5 options:

  1. If you declare the parameter of type C, then it will not be overriding.

    Error:

> Method does not override method from its superclass

> Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
The method does override or implement a method declared in a supertype.
The method has a signature that is override-equivalent to that of any public method declared in Object.

  1. superclass cannot access private fields of a class, so they need to call the superclass equals method to compare them.

huangapple
  • 本文由 发表于 2020年5月29日 09:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/62077096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定