英文:
Any possible to make two instantiations be equal in java?
问题
我需要实现这个结果:
public class question {
public static void main(String[] arg) {
A a1 = new A();
A a2 = new A(2);
assert a2.equals(a1);
}
}
所以我尝试过:
class A {
A() {
return;
}
A(int x) {
return;
}
}
public class d22 {
public static void main(String[] arg) {
A a1 = new A();
A a2 = new A(2);
assert a2.equals(a1);
}
}
但显然这并不起作用,有没有办法实现上面的代码?谢谢。
英文:
I need to achieve this result:
public class question{
public static void main(String[]arg){
A a1=new A();
A a2=new A(2);
assert a2.equals(a1);
}
}
So I have tried:
class A{
A(){
return;
}
A(int x){
return;
}
}
public class d22 {
public static void main(String[]arg){
A a1=new A();
A a2=new A(2);
assert a2.equals(a1);
}
}
But obviously it's not working, is there any way to achieve the code upper? Thx.
答案1
得分: 0
equals
方法并没有什么特别神奇的地方 - 它只是在 java.lang.Object
中定义的一个普通方法。
你可以对其进行重写。
这个奇怪的想法可以生效:
class A {
@Override public boolean equals(Object other) {
return other instanceof A;
}
}
这意味着任何一个 A 的实例都与另一个实例相等。
英文:
There's nothing particularly magical about equals
- it's a plain jane method defined in java.lang.Object.
You can override it.
This weird idea would work:
class A {
@Override public boolean equals(Object other) {
return other instanceof A;
}
}
would mean any A is equal to any other.
答案2
得分: 0
实际上,由关键字"new"创建的Java对象分配在堆中。因此,如果你想比较a1和a2,结果将会是false,因为默认的equals方法是比较对象的内存地址。如果你想比较两个对象,你应该重写equals方法以实现你的目标。
英文:
actually,the java object which is created by the key word new is allocated in the heap.So the result is false if you want compare a1 with a2,because the default equals method is to compare the object memory address.If you want compare two object ,you should rewrite the equals method to achieve your goal.
答案3
得分: 0
All classes in Java extend the default Object class, which contains the methods equals()
and toString()
. The equals()
method is used to compare an instance of a particular class with another variable. You can override this method to define how the equals
function should behave.
class A {
@Override
public boolean equals(Object other) {
if (!(other instanceof A)) return false; // other is not the same type as this class
if (other == this) return true; // both variables point to the same object
return true; // you can compare attributes of the other class here
}
}
By default, the equals()
method will return true if you are comparing two variables that point to the same object. This means that in your case, you will obtain false when calling equals
, as your two variables point to different objects created by the new
operator, and you have not overridden the equals()
method to provide different behavior than the default one.
英文:
All classes in Java extends default Object class which contains method's equals()
and toString()
. Equals()
method is used to compare instance of that particular class against another variable. You can override this method to define how equals should behave.
class A {
@Override
public boolean equals(Object other) {
if (!other instance of A) return false; // other is not same type as this class
if (other == this) return true; // both variables point to same object
return true; // you can compare other class attributes here
}
}
By default equals will return true if you are comparing two variables that point to same object. That means in your case that you obtain false when calling equals
since your two variables point to different object created by new
operator and you have not overriden equals()
method to get different behavior than default one.
专注分享java语言的经验与见解,让所有开发者获益!
评论