//只用作给Sheep充当引用对象 public class Country implements Cloneable, Serializable { //序列化的版本号 private static final long serialVersionUID = 1001L; private String mainland; public Country(String mainland) { this.mainland = mainland; } public String getMainland() { return mainland; } public void setMainland(String mainland) { this.mainland = mainland; } @Override public String toString() { return "Country{" + "mainland='" + mainland + '\'' + '}'; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
public class Sheep implements Cloneable, Serializable { //序列化的版本号 private static final long serialVersionUID = 1000L; private String name; private int age; private String color; private Country country; public Sheep(String name, int age, String color, Country country) { this.name = name; this.age = age; this.color = color; this.country = country; System.out.println("走构造器创建对象"); } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + ", color='" + color + '\'' + ", country=" + country + '}'; } //需要实现Cloneable接口 //实现深克隆方式一 @Override protected Sheep clone() throws CloneNotSupportedException { //对基本数据进行复制,对除string外的引用对象进行地址拷贝 Object clone = super.clone(); Sheep sheep = (Sheep) clone; //调用属性中的引用类型实现的clone()方法,达到深拷贝 sheep.country = (Country) country.clone(); return sheep; } //需要该类和属性中的类实现 Serializable接口 //实现深克隆方式二 public Sheep deepClone() { //创建流对象 ObjectOutputStream oos = null; ObjectInputStream ois = null; ByteArrayInputStream bis = null; ByteArrayOutputStream bos = null; Sheep sheep = null; try { //序列化 bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(this); //反序列化 bis = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bis); sheep = (Sheep) ois.readObject(); } catch (Exception e) { throw new RuntimeException(e); } finally { //关闭所有流 try { if (ois == null) { ois.close(); } if (bis == null) { bis.close(); } if (oos == null) { oos.close(); } if (bos == null) { bos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } return sheep; } }
public class TestClone { public static void main(String[] args) throws Exception{ //走构造克隆对象 Sheep sheep = new Sheep("阿伟", 5, "黄色",new Country("亚洲")); //走克隆对象 Sheep cloneSheep = sheep.deepClone(); /*对比原型与克隆对象的地址(可以深克隆与浅克隆) * 删除方法中 sheep.country = (Country) country.clone();是浅克隆,地址一样 * sheep.country = (Country) country.clone();是深克隆,地址不一样 * */ System.out.println("原型羊的地址"+sheep.hashCode()); System.out.println("克隆羊的地址"+cloneSheep.hashCode()); //对比引用对象country的地址(走的深克隆) System.out.println("原型羊的国家地址"+sheep.getCountry().hashCode()); System.out.println("克隆羊的国家地址"+cloneSheep.getCountry().hashCode()); } }