public class CloneTest2
{
public static void main(String[] args)
{//from N o w J a v a . c o m
Employee emp1 = new Employee("M", "A");
emp1.setSalary(40000.0);
emp1.address = new Address("1300 N. Main Street","New York", "CA", "99999");
Employee emp2 = (Employee)emp1.clone();
System.out.println("**** after cloning ****\n");
printEmployee(emp1);
printEmployee(emp2);
emp2.setLastName("S");
emp2.address = new Address("2503 N. 6th Street","L.A.", "CA", "93722");
/*来自
时代Java公众号*/
System.out.println(
"**** after changing emp2 ****\n");
printEmployee(emp1);
printEmployee(emp2);
}
private static void printEmployee(Employee e)
{
System.out.println(e.getFirstName() + " " + e.getLastName());
System.out.println(e.address.getAddress());
System.out.println("Salary: " + e.getSalary());
System.out.println();
}
}
class Employee implements Cloneable
{
private String lastName;
private String firstName;
private Double salary;
public Address address;
public Employee(String lastName, String firstName)
{
this.lastName = lastName;
this.firstName = firstName;
this.address = new Address();
}
public String getLastName()
{
return this.lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getFirstName()
{
return this.firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public Double getSalary()
{
return this.salary;
}
public void setSalary(Double salary)
{
this.salary = salary;
}
public Object clone()
{
Employee emp;
try
{
emp = (Employee) super.clone();
emp.address = (Address)address.clone();
}
catch (CloneNotSupportedException e)
{
return null; // will never happen
}
return emp;
}
public String toString()
{
return this.getClass().getName() + "["
+ this.firstName + " "
+ this.lastName + ", "
+ this.salary + "]";
}
}
class Address implements Cloneable
{
private String street;
private String city;
private String state;
private String zipCode;
public Address()
{
this.street = "";
this.city = "";
this.state = "";
this.zipCode = "";
}
public Address(String street, String city,
String state, String zipCode)
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。