clone淺拷貝:
public class Student implements Cloneable{
int id;
String name;
Pro pro;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public Student(int id, String name, Pro pro) {
super();
this.id = id;
this.name = name;
this.pro = pro;
}
public Pro getPro() {
return pro;
}
public void setPro(Pro pro) {
this.pro = pro;
}
public void setName(String name) {
this.name = name;
}
public Object clone()
{
Student student=null;
try {
student=(Student) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
}
public class Pro {
int id;
String name;
public Pro(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Test
public void testStudent()
{
Pro pro=new Pro(1,"w1");
Student s1=new Student(1,"wuqiang",pro);
Student s2=(Student) s1.clone();
s2.getPro().setId(2);
s2.getPro().setName("w2");
System.out.println(s1.getPro().getId()+" "+s1.getPro().getName());
}