前言

数据内容完全一样,但实例不同,比如:Bean拷贝。这里涉及到深拷贝、浅拷贝。

  • 浅拷贝:仅支持8种基本数据类型、String。
  • 深拷贝:各种集合、对象等,都可拷贝。

代码示例

package com.prototype.greatestsage;

import java.util.Date;

public class Monkey {
    protected int height;
    protected int weight;
    protected Date birthday;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
package com.prototype.greatestsage;

import java.io.Serializable;

public class GoldRingedStaff implements Serializable {
    private float height = 100;
    private float diameter = 10;

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public float getDiameter() {
        return diameter;
    }

    public void setDiameter(float diameter) {
        this.diameter = diameter;
    }

    public void grow() {
        this.height *= 2;
        this.diameter *= 2;
    }

    public void shrink() {
        this.height /= 2;
        this.diameter /= 2;
    }
}
package com.prototype.greatestsage;

import java.io.*;
import java.util.Date;

public class TheGreatestSage extends Monkey implements Cloneable, Serializable {
    private GoldRingedStaff staff;

    public TheGreatestSage() {
        this.staff = new GoldRingedStaff();
        this.birthday = new Date();
        this.height = 150;
        this.weight = 30;
        //**克隆是不走构造方法的,字节流复制**
        System.out.println("-------------------------------");
    }

    @Override
    protected Object clone() {
        //深度克隆
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            TheGreatestSage copy = (TheGreatestSage) ois.readObject();
            copy.setBirthday(new Date());
            return copy;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                ois.close();
                bos.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //浅克隆 8种基本数据类型+String
        //return super.clone();
        return null;
    }

    public GoldRingedStaff getStaff() {
        return staff;
    }

    public void setStaff(GoldRingedStaff staff) {
        this.staff = staff;
    }

    public void change() {
        TheGreatestSage copySage = (TheGreatestSage) clone();
        System.out.println("大圣本尊的生日是:" + this.getBirthday().getTime());
        System.out.println("克隆大圣的生日是:" + copySage.getBirthday().getTime());
        System.out.println("大圣本尊和克隆大象是否为同一个对象:" + (this == copySage));
        System.out.println("大圣本尊持有的金箍棒跟克隆大圣持有的金箍棒是否为同一个对象:" + (this.getStaff() == copySage.getStaff()));
    }
}

package com.prototype.greatestsage;

public class UnitTest {
    public static void main(String[] args) {

        TheGreatestSage sage = new TheGreatestSage();
        sage.change();
    }
}
-------------------------------
大圣本尊的生日是:1597647772421
克隆大圣的生日是:1597647772591
大圣本尊和克隆大象是否为同一个对象:false
大圣本尊持有的金箍棒跟克隆大圣持有的金箍棒是否为同一个对象:false