比较两者的正确方法是什么Pair<?,?>
?
我使用此代码,但是我不确定100%吗?
也许您知道另一个更好的解决方案?
public boolean equals(Object other) {
if (other instanceof Pair<?,?>) {
Pair<?, ?> otherPair = (Pair<?, ?>) other;
return equalsPart(this._first, otherPair._first) && equalsPart(this._second, otherPair._second);
}
return false;
}
public boolean equalsPart(Class<?> one, Class<?> two) {
return ((one== two || (one!= null && two != null && one.equals(two))));
}
public boolean equals(Object other) {
if(this == other)
return true;
if (!(other instanceof Pair)) {
return false;
}
Pair<?, ?> otherPair = (Pair<?, ?>) other;
return Equality.equals(this.first,otherPair._first) &&
Equality.equals(this._second, otherPair._second);
}
class Equality{
public static boolean equals(Object first, Object second){
return first == null ? second == null : first.equals(second);
}
}
您会发现许多库中都遵循这种成语equals()
。前提条件检查和快速故障排除方法equals()
。
已经创建了一个新的实用程序Equality
,可用于进一步的类中,从而提高了可读性并提供了适当的null
检查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句