深拷贝

深拷贝


  1/**
  2 * 使用深拷贝,拷贝才有意义
  3 *
  4 * @author wangy
  5 * @version 1.0
  6 * @date 2020/4/23 / 16:57
  7 */
  8public class DeepCopy implements Cloneable {
  9    private String company;
 10    private Name name;
 11
 12    public DeepCopy(String company, Name name) {
 13        this.company = company;
 14        this.name = name;
 15    }
 16
 17    public DeepCopy(String company, String first, String last) {
 18        this.company = company;
 19        this.name = new Name(first, last);
 20    }
 21
 22    public void changeCompany(String comp) {
 23        this.company = comp;
 24    }
 25
 26    public void changeFirstName(String name) {
 27        this.name.changeFirstName(name);
 28    }
 29
 30    /**
 31     * it's necessary to implement <code>clone()</code>
 32     * when you want to make a copy of an object
 33     *
 34     * @return the copy object
 35     * @throws CloneNotSupportedException
 36     */
 37    @Override
 38    protected DeepCopy clone() throws CloneNotSupportedException {
 39        // 深拷贝所有的可变引用都需要拷贝
 40        DeepCopy clone = (DeepCopy) super.clone();
 41        clone.name = this.name.clone();
 42        return clone;
 43    }
 44
 45    @Override
 46    public String toString() {
 47        return "Item2{" +
 48                "company='" + company + '\'' +
 49                ", name=" + name +
 50                '}';
 51    }
 52
 53    private static class Name implements Cloneable {
 54        private String firstName;
 55        private String lastName;
 56
 57        public Name(String firstName, String lastName) {
 58            this.firstName = firstName;
 59            this.lastName = lastName;
 60        }
 61
 62        private void changeFirstName(String firstName) {
 63            this.firstName = firstName;
 64        }
 65
 66        @Override
 67        public String toString() {
 68            return "Name{" +
 69                    "firstName='" + firstName + '\'' +
 70                    ", lastName='" + lastName + '\'' +
 71                    '}';
 72        }
 73
 74        /**
 75         * 拷贝对象时,对象的可变引用也需要拷贝,知道所有对象都不可变为止
 76         * 实际上,<code>Name</code>类的域都是String,因此拷贝方法到此为止
 77         *
 78         * @return
 79         * @throws CloneNotSupportedException
 80         */
 81        @Override
 82        protected Name clone() throws CloneNotSupportedException {
 83            return (Name) super.clone();
 84        }
 85    }
 86
 87    static class Test {
 88        public static void main(String[] args)
 89                throws CloneNotSupportedException {
 90
 91            DeepCopy i = new DeepCopy(
 92                    "app",
 93                    "steve",
 94                    "jobs");
 95
 96            DeepCopy copy = (DeepCopy) i.clone();
 97            // String 是(final)不可变的
 98            copy.changeCompany("apple");
 99            // 深拷贝时,拷贝对象改变和原对象相互独立
100            copy.changeFirstName("stephen");
101
102            System.out.println(i);
103            System.out.println(copy);
104        }
105    }
106}
107
108/*
109 * Item2{company='app', name=Name{firstName='steve', lastName='jobs'}}
110 * Item2{company='apple', name=Name{firstName='stephen', lastName='jobs'}}
111 */
112/// :~