Comparing two objects
How can you determine whether two objects are equal or not? Basically, this is defined by objects (refer to the How it works… section of this recipe). Obviously, two equal objects will be of the same class (so the same type) and have the same value(s).
How to do it...
In the comparing_objects
program, we define a class Person
, override ==
and hashcode
, and test the equality of some objects, as shown in the following code:
void main() { var p1 = new Person("Jane Wilkins", "485-56-7861", DateTime.parse("1973-05-08")); var p2 = new Person("Barack Obama", "432-94-1282", DateTime.parse("1961-08-04")); var p3 = p1; var p4 = new Person("Jane Wilkins", "485-56-7861", DateTime.parse("1973-05-08")); // with == and hashCode from Object: // (comment out == and hashCode in class Person) print(p2==p1); //false: p1 and p2 are different print(p3==p1); //true: p3 and p1 are the same object print(p4==p1); //false: p4 and p1 are different objects print(identical(p1...