public static void main(String[] args) {
Professor professor = new Professor("beijing"?? 12.0);
Student s1 = new Student("zhangsan"?? 18?? professor);
Student s2 = (Student) s1.clone();
System.out.println(s1);
System.out.println(s2);
System.out.println("**************************************");
s2.name = "lisi";
s2.age = 20;
s2.professor.setAddress("shanghai");
s2.professor.setSalary(230.0);
System.out.println(s1);
System.out.println(s2);
}
}
Student [name=zhangsan?? age=18?? Professor=Professor [address=beijing?? salary=12.0]]
Student [name=zhangsan?? age=18?? Professor=Professor [address=beijing?? salary=12.0]]
**************************************
Student [name=zhangsan?? age=18?? Professor=Professor [address=shanghai?? salary=230.0]]
Student [name=lisi?? age=20?? Professor=Professor [address=shanghai?? salary=230.0]]
??????????
public class Professor2 implements Cloneable {
private String address;
private double salary;
public Professor2(String address?? double salary) {
this.address = address;
this.salary = salary;
}
public void setAddress(String address) {
this.address = address;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Professor2 [address=" + address + "?? salary=" + salary + "]";
}
@Override
public Object clone() {
Professor2 o = null;
try {
o = (Professor2) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}