[英]call method of child from instance of type Parent
I have to make the following equals method: 我必须制作以下equals方法:
public boolean equals(ParentClass pa) {
if (!(pa instanceof ChildClass))
return false;
boolean sameID = ID.equals(pa.getID());
return super.equals(pa) && sameID;
}
The class ChildClass is a child of the class ParentClass, and this would be it's equals method. ChildClass类是ParentClass类的子级,这就是equals方法。 The method getId() is only in the ChildClass, but I need to know how to get the method pa.getID() to work, is there any way to do a cast here? 方法getId()仅在ChildClass中,但是我需要知道如何使方法pa.getID()起作用,是否可以在此处进行强制转换? since i get an error, the compiler can't be sure if pa is an instance of Child or not. 由于出现错误,编译器无法确定pa是否为Child的实例。
This is the erro that i get: 这是我得到的错误:
Error:(19, 73) java: cannot find symbol
symbol: method getID()
location: variable pa of type ParentClass
I need the method to be of type ParentClass, because the function should return false when an object of type ParentClass instead of an error. 我需要方法的类型为ParentClass,因为当对象的类型为ParentClass而不是错误时,该函数应返回false。
Any help would be appreciated. 任何帮助,将不胜感激。
After you check the type of object and ensure that it's instance of ChildClass
just convert it to ChildClass
type and use it: 在检查对象的类型并确保它是ChildClass
的实例之后,只需将其转换为ChildClass
类型并使用它即可:
public boolean equals(ParentClass pa) {
if (!(pa instanceof ChildClass))
return false;
ChildClass child = (ChildClass) pa;
boolean sameID = ID.equals(child.getID());
return super.equals(child) && sameID;
}
There's a common pattern that's useful to follow when overriding equals
. 在覆盖equals
时,有一个通用的模式非常有用。 I'll provide a version with the corrections here, and outline what those corrections were below: 我将在此处提供包含更正的版本,并概述以下这些更正:
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (!super.equals(o)
|| o == null
|| getClass() != o.getClass()) {
return false;
}
ChildClass other = (ChildClass) o;
return this.getID().equals(other.getID());
}
First, the stuff you can generally copy-paste between implementations of equals
: 首先,通常可以在equals
实现之间复制粘贴内容:
Always @Override
the equals
method - this will help identify issues where you're accidentally overloading it (same method name, different argument types). 始终@Override
equals
方法-这将帮助您识别意外重载它的问题(相同的方法名称,不同的参数类型)。 Overloading equals
is a bad practice that will cause problems. 重载等equals
一种不好的做法,会引起问题。
Start with ==
check - if this passes, no need to waste CPU cycles doing any more logic. 从==
检查开始-如果通过,则无需浪费CPU周期做更多的逻辑。 Though this is really just a minor optimization, it's a useful best practice. 尽管这实际上只是次要的优化,但这是有用的最佳实践。
If you are extending a class other than Object
, check for super
equality. 如果要扩展Object
以外的其他类,请检查super
相等性。
Check to make sure the classes are the same . 检查以确保类相同 。 Note that this is not an instanceof
check. 请注意,这不是检查的instanceof
。
equals
must be symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. 请记住, equals
必须对称:对于任何非空参考值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才返回true。 instanceof
, you'll likely run into case where parent.equals(child) == true
but child.equals(parent) == false
- that's bad. 如果使用instanceof
,很可能会遇到parent.equals(child) == true
但child.equals(parent) == false
-这很糟糕。 instanceof
is ok, but checking class equality is safer and more correct most of the time. 在某些情况下, instanceof
可以,但是在大多数情况下检查类的相等性是更安全和更正确的。 Now, the class specific stuff: 现在,该类特定的东西:
Object
parameter to your class type, and compare the ones you care about for equality. 将Object
参数转换为您的类类型,然后比较您关心的那些参数是否相等。
this
should match the calls on other
- in your case, I changed both to call getID()
method. 需要注意的是在调用this
应符合在调用other
-在你的情况,我都修改了调用getID()
方法。 Alternatively, both can refer to the private member ID
- but they should be consistent. 另外,两者都可以引用私有成员ID
但它们应保持一致。 Finally, don't forget that equals
and hashCode
are a pair - you should always implement them together . 最后,不要忘记equals
和hashCode
是一对-您应该始终一起实现它们 。 (I know you didn't specifically ask about it, but I'm pointing it out as it's a common error people make when implementing equals
) (我知道您没有专门询问它,但我指出这一点是因为这是人们在实现equals
犯的错误)
I hope this helps you and anyone else with problems implementing equals
. 希望这对您和其他在实现equals
方面遇到问题的人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.