[英]How x.clone().equals(x) is True
I have tried this implementation but i got false for the class x 我已经尝试过这种实现方式,但对于x班却错了
x.clone().equals(x)
Class X : X级:
public class X implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected Object clone()throws CloneNotSupportedException {
return super.clone();
}
}
Main class : 主类:
public class ObjectCloneCopy {
public static void main(String[] args) throws CloneNotSupportedException {
X x = new X();
System.out.println("x.clone().equals(x) - " + x.clone().equals(x));
}
}
Is it mandatory to overload the hashcode() and equals() to get this True ? 是否必须重载hashcode()和equals()以获得此True?
Without overriding these methods how this statement gives true? 在不覆盖这些方法的情况下,此语句如何正确?
X x1 = x;
x1.equals(x)
Explain how that could be true, i have seen in this link 解释这可能是真的,我在此链接中已经看到
You need to override equals()
and hashCode()
method in your X
class. 您需要在
X
类中重写equals()
和hashCode()
方法。
Else you can't get the correct result from x.clone().equals(x)
否则您无法从
x.clone().equals(x)
获得正确的结果
Object#clone
returns independent of clonning object, so two independent object may not be equals. Object#clone
返回独立于克隆对象的对象,因此两个独立对象可能不相等。
As per documentation - 根据文档-
Object#clone -
Creates and returns a copy of this object.
创建并返回此对象的副本。 The precise meaning of "copy" may depend on the class of the object.
“复制”的确切含义可能取决于对象的类别。 .
。 The general intent is that -
总体意图是-
x.clone() != x // true
x.clone().getClass() == x.getClass() // true
and 和
x.clone().equals(x) // will be true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling super.clone.
按照约定,应该通过调用super.clone获得返回的对象。 If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
如果一个类及其所有超类(对象除外)都遵守此约定,则x.clone()。getClass()== x.getClass()就是这种情况。
Need to override `equals()` and `hashCode()` method in the class `X`.
If not you can't get the correct true for x.clone().equals(x)
如果不是,您将无法获得
x.clone().equals(x)
的正确true
For 对于
X x1 = new X()
x1.equals(x)
Since not overriding equals() 由于不覆盖equals()
x1 and x are considered as same object of X class so it return true
Without overriding it will check the equals() in Object class 在不覆盖的情况下,它将检查Object类中的equals()
public boolean equals(Object obj) {
return (this == obj);
}
Try first to equals()
and hashCode()
and make thier result based on class attribute value so that when you override , the cloned instance should return the same value of hashCode()
and the equals code should return true. 首先尝试对
equals()
和hashCode()
进行尝试,并根据类属性值得出结果,以便在重写时,克隆的实例应返回相同的hashCode()
值,而equals代码应返回true。 check this it may help you . 检查这可能对您有帮助。
First Question : x.clone().equals(x)
It will return false. 第一个问题 :
x.clone().equals(x)
它将返回false。
We need override equals & hashcode.Because of the following reason we need to do. 我们需要覆盖等于&哈希码。由于以下原因,我们需要这样做。
1) Clone will create new instance of x. 1)克隆将创建x的新实例。 Reference of two instances are different since both are different reference.
两个实例的引用不同,因为两者都是不同的引用。
2) If equals & hashcode methods are not overridden, Super class Object# equals will be invoked. 2)如果不覆盖equals和hashcode方法,则将调用超类Object# equals 。 This will check the memory location of the object.
这将检查对象的内存位置。 As per our earlier point both have different address, so it will false.
根据我们先前的观点,两者都有不同的地址,因此它将为假。
Second Question : X x1 = x x1.equals(x)
return true. 第二个问题:
X x1 = x x1.equals(x)
返回true。
1) x1,x are same object and same location. 1)x1,x是相同的对象和相同的位置。
2) So even override equals & hashcode methods are not overridden, it will check the memory location and will return true. 2)因此即使重写equals&hashcode方法也不会被覆盖,它将检查内存位置并返回true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.