In python we can compare object by attributes by this method:
def __eq__(self, other):
return self.__dict__ == other.__dict__
It allows us to compare objects by their attributes no matter how many attributes they have.
Is something like this possible in Java? I tried to compare objects in this way, but you have to compare them by every attribute. Is it possible to do it similarly to Python eq method above?
public class MyClass {
public MyClass(attr1, attr2, attr3, attr4) {
this.attr1 = attr1;
this.attr2 = attr2;
this.attr3 = attr3;
this.attr4 = attr4;
}
public boolean equals(Object object2) {
return object2 instanceof MyClass &&
attr1.equals(((MyClass)object2).attr1)&&......;
}
}