在没有覆盖的情况下从Equals方法返回false [英] Returning false from Equals methods without Overriding
问题描述
编写 TestEquals类
,以便 Puzzle3
类的主要方法打印 false
。
Write TestEquals class
so that main method of Puzzle3
class prints false
.
注意:你不能覆盖TestEquals类的equals方法。
Note: You cannot override equals method of TestEquals class.
public class Puzzle3 {
public static void main(String[] args) {
TestEquals testEquals = new TestEquals();
System.out.println(testEquals.equals(testEquals));
}
}
我找不到实现此目的的方法,请分享您的意见
I didn't find a way to achieve this, please share your comments
推荐答案
您可能无法覆盖equals方法,但没有理由不能重载equals方法。
You may not override the equals method, but there is no reason you cannot overload the equals method.
方法Object.equals有原型:
The method Object.equals has prototype:
public boolean equals(Object o) { ... }
要覆盖此方法,您必须创建一个具有相同方法的方法TestEquals中的原型。但是,您的问题语句表明您不允许覆盖此方法。没问题,重载方法是完成任务的有效方法。只需将以下方法定义添加到TestEquals:
To override this method you have to create a method with the same prototype in TestEquals. However your problem statement indicates that you are not allowed to override this method. No problem, overloading the method is a valid approach to accomplish your task. Simply add the following method definition to TestEquals:
public boolean equals(TestEquals o) { return false; }
你已经完成了。
这篇关于在没有覆盖的情况下从Equals方法返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!