Object中clone()方法是protected的,是浅拷贝,要使用clone方法,要重写它,只有实现了implements Cloneable才可以调用该方法,否则会抛出CloneNotSupportedException异常。

@Override
    public Object clone(){
        Object o = null;
        try {
            o = super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
//        Person p = (Person) o;
//        p.book = (Book)p.getBook().clone();              //放开这两段即为深拷贝的用法
        return o;
    }

在我们需要复制对象的时候常用的三种方式

public static void main(String[] args){
Book b = new Book("java");
Person p = new Person("wt",b);
Person p1 = new Person(p);
Person p2 = (Person) p.clone();
b.setBookName("js");
p.setName("zjj");
System.out.println("P:"+p);
System.out.println("P1:"+p1);
System.out.println("P2:"+p2);
}

当clone使用浅拷贝(即不放开那两段)时,只拷贝对象不包含对对象引用的对象的拷贝:

P:{name:zjj, book:{bookName:js}}            
P1:{name:wt, book:{bookName:js}}      //不论是p1还是p2,基本类型的数值是直接拷贝值的,但是引用类型的对象却是拷贝地址的
P2:{name:wt, book:{bookName:js}}      //这样一旦p的原始值引用的对象改变,浅拷贝后的对象也会随之变化

当clone使用深拷贝(即放开那两段)时:

P2:{name:wt, book:{bookName:java}}      //深拷贝时,连对象中的对象都进行的是值拷贝,所以原始值的引用对象改变不会影响它

 

完整源代码:

person类

public class Person implements Cloneable {
    private String name;
    private Book book;

    public Person(String name, Book book) {
        this.name = name;
        this.book = book;
    }

    public Person(Person p) {
        this.name = p.name;
        this.book = p.book;
    }

    @Override
    public Object clone(){
        Object o = null;
        try {
            o = super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        Person p = (Person) o;
        p.book = (Book)p.getBook().clone();
        return o;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Override
    public String toString() {
        return "{name:" + name + ", book:" + book + "}";
    }
}

book类

public class Book implements Cloneable{
    private String bookName;
    public Book(String bookName) {
        this.bookName = bookName;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    @Override
    public String toString() {
        return "{bookName:" + bookName + "}";
    }

    @Override
    public Object clone(){
        Object o = null;
        try {
            o = super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return o;
    }
}

clone测试类

public class CloneTest {

    public static void main(String[] args){
        Book b = new Book("java");
        Person p = new Person("wt",b);
        Person p1 = new Person(p);
        Person p2 = (Person) p.clone();
        b.setBookName("js");
        p.setName("zjj");
        System.out.println("P:"+p);
        System.out.println("P1:"+p1);
        System.out.println("P2:"+p2);
    }
}