java关于 克隆接口cloneable问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java关于 克隆接口cloneable问题相关的知识,希望对你有一定的参考价值。

public class House implements Cloneable
private int id;
private double area;
private java.util.Date whenBuilt;

public House(int id, double area)
this.id = id;
this.area = area;
whenBuilt = new java.util.Date();


public double getId()
return id;


public double getArea()
return area;


public java.util.Date getWhenBuilt()
return whenBuilt;


/** Override the protected clone method defined in the Object
class, and strengthen its accessibility */
public Object clone()
try
return super.clone();

catch (CloneNotSupportedException ex)
return null;



我现在创建House类的一个对象,与它相同的复制品
House house1 = new House(1,1750.50);
House house2 = (House)house1.clone();
运行house1.whenBuilt == house2.whenBuilt 为 true
这说明这是浅复制,麻烦高手帮忙修改程序,使之成为深复制...

对于你的例子中,属性whenBuilt才是对象,因此可以简单的修改一下clone方法即可
/** Override the protected clone method defined in the Object
class, and strengthen its accessibility */
public Object clone()
try
House t = (House)super.clone();
t.whenBuilt = (Date) this.whenBuilt.clone();
return t;

catch (CloneNotSupportedException ex)
return null;


对于复杂的,可以利用串行化进行深度复制,下面的完整实例中的deepClone就是相应的代码(此时,House类需要实现序列化的接口:java.io.Serializable)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class House implements Cloneable, Serializable
private int id;
private double area;
private java.util.Date whenBuilt;

public House(int id, double area)
this.id = id;
this.area = area;
whenBuilt = new java.util.Date();


public double getId()
return id;


public double getArea()
return area;


public java.util.Date getWhenBuilt()
return whenBuilt;


/** Override the protected clone method defined in the Object
class, and strengthen its accessibility */
public Object clone()
try
House t = (House)super.clone();
t.whenBuilt = (Date) this.whenBuilt.clone();
return t;

catch (CloneNotSupportedException ex)
return null;


public Object deepClone()
ByteArrayOutputStream out = new ByteArrayOutputStream();
try
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(this);

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
return oi.readObject();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();

return null;


public static void main(String[] args)
House house1 = new House(1,1750.50);
House house2 = (House)house1.deepClone();
System.out.println(house1.whenBuilt == house2.whenBuilt );

参考技术A public Object clone()
House house1 = new House ();
house1.setAge(this.age);
house1.setName(this.name);

return house1 ;


cloneable 就是深复制。
浅拷贝:仅复制引用,即两个引用指向同一个对象,如:
String aString = "hello";
String bString = aString;
深拷贝:复制对象的内容,Java里通过实现Clonnable并覆盖clone方法实现,具体深度由clone的实现决定,
参考技术B 把whenBuilt clone一下或者再深点。。把所有属性都clone一下。。没实现clone的就算了。。Date是实现了的。。

House cloned = (House)super.clone();
cloned.whenBuilt = (Date)whenBuilt.clone();
return cloned;

以上是关于java关于 克隆接口cloneable问题的主要内容,如果未能解决你的问题,请参考以下文章

Java - 关于 Cloneable 接口 clone 方法

Java对象克隆(Clone)及Cloneable接口Serializable接口的深入探讨

Cloneable 接口是不是一个鸡肋?

Java 接口 Cloneable

一文读懂深克隆与浅克隆的关系

java开发——Cloneable接口clone()方法和深浅拷贝