public class Main {
public static void main(String[] args) {
Person person1 = new Person();
person1.setFirstName("F");
person1.setLastName("L");
Person person2 = (Person) person1.clone();
Person person3 = (Person) person2.clone();
System.out.println("Person 1: " + person1.getFirstName() + " " + person1.getLastName());
System.out.println("Person 2: " + person2.getFirstName() + " " + person2.getLastName());
System.out.println("Person 3: " + person3.getFirstName() + " " + person3.getLastName());
}
}
class Person implements Cloneable {
private String firstName;
private String lastName;
public Object clone() {
Person obj = new Person();
obj.setFirstName(this.firstName);
obj.setLastName(this.lastName);
return obj;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
/*
Person 1: F L
Person 2: F L
Person 3: F L
*/
|