淺複製
被複制對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用仍然指向原來的對象。換言之,淺複製僅僅複製所考慮的對象,而不復制它所引用的對象。
深複製
被複制對象的所有變量都含有與原來的對象相同的值,除去那些引用其他對象的變量。那些引用其他對象的變量將指向被複制過的新對象,而不再是原有的那些被引用的對象。換言之,深複製把要複製的對象所引用的對象都複製了一遍(遞歸下去)。
Java中對象的克隆
Object的clone()方法
clone方法將對象複製了一份並返回給調用者。一般而言,clone()方法滿足:
1)對任何的對象x,都有x.clone() !=x
克隆對象與原對象不是同一個對象
2)對任何的對象x,都有x.clone().getClass()= =x.getClass()
克隆對象與原對象的類型一樣
3)如果對象x的equals()方法定義恰當,那麼x.clone().equals(x)應該成立。
2)在派生類中覆蓋基類的clone()方法,並聲明爲public(Object類中的clone()方法爲protected的)
3)在派生類的clone()方法中,調用super.clone()
4)在派生類中實現Cloneable接口
淺複製
public class Student implements Cloneable {
private int age;
private String name;
private Book book;
public int getAge() {
return age;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
Object object = super.clone();
return object;
}
}
public class Book {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Book book = new Book();
book.setName("english");
student1.setName("zhangsan");
student1.setAge(22);
student1.setBook(book);
Student student2 = (Student) student1.clone();
System.out.println(student1.clone().equals(student1));// false
System.out.println(student1.clone() == student1);// false
System.out.println(student1.clone().getClass() == student2.getClass());// true
System.out.println(student2.getName());// zhangsan
student1.setName("lisi");
student1.getBook().setName("chinese");
System.out.println(student2.getName());// zhangsan
System.out.println(student2.getBook().getName());// chinese
}
}
深複製
public class Fruit implements Cloneable {
public String name;
public Apple apple;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Apple getApple() {
return apple;
}
public void setApple(Apple apple) {
this.apple = apple;
}
@Override
public Object clone() throws CloneNotSupportedException {
<strong>Fruit fruit1 = (Fruit) super.clone();
fruit1.setApple((Apple) fruit1.getApple().clone());
return fruit1;</strong>
}
}
public class Apple implements Cloneable {
public String Name = "";
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
<strong>return super.clone();</strong>
}
}
public class Test {
public static void main(String[] args) throws Exception {
Fruit fruit1 = new Fruit();
Apple apple = new Apple();
fruit1.setName("apple");
apple.setName("fusi");
fruit1.setApple(apple);
Fruit fruit2 = (Fruit) fruit1.clone();
System.out.println(fruit1.clone().equals(fruit1));// false
System.out.println(fruit1.clone() == fruit1);// false
System.out.println(fruit1.clone().getClass() == fruit2.getClass());// true
System.out.println(fruit2.getName());// apple
fruit1.setName("banana");
fruit1.getApple().setName("shandong");
System.out.println(fruit2.getName());// apple
System.out.println(fruit2.getApple().getName());// fusi
}
}
public class People implements Serializable {
public String name;
public Girl girl;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Girl getGirl() {
return girl;
}
public void setGirl(Girl girl) {
this.girl = girl;
}
public Object deepCopy() throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
}
public class Girl implements Serializable {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) throws Exception {
People people1 = new People();
Girl girl = new Girl();
people1.setName("zhangsan");
girl.setName("meimei");
people1.setGirl(girl);
People people2 = (People) people1.deepCopy();
System.out.println(people1.deepCopy().equals(people1));// false
System.out.println(people1.deepCopy() == people1);// false
System.out.println(people1.deepCopy().getClass() == people2.getClass());// true
System.out.println(people2.getName());// zhangsan
people1.setName("lisi");
people1.getGirl().setName("lili");
System.out.println(people2.getName());// zhangsan
System.out.println(people2.getGirl().getName());// meimei
}
}