-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
44 lines (37 loc) · 1.24 KB
/
Person.java
File metadata and controls
44 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Objects;
public class Person implements Cloneable, Comparable<Person> {
private String name;
private Address address;
public Person(String name, Address address) {
super();
this.name = name;
this.address = address;
}
@Override
public Person clone() {
try {
Person clonedPerson = (Person) super.clone();
// Perform deep copy for reference-type fields
clonedPerson.name = new String(this.name);
clonedPerson.address = this.address.clone();
return clonedPerson;
} catch (CloneNotSupportedException e) {
// Should never happen since Person implements Cloneable
throw new InternalError(e);
}
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Person person = (Person) other;
return Objects.equals(name, person.name) && Objects.equals(address, person.address);
}
@Override
public int compareTo(Person p) {
return this.name.compareTo(p.name);
}
public String toString() {
return name + " resides at " + address;
}
}