站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题对象克隆

对象克隆

摘要: 对象克隆

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="425" height="86" align="left" valign="top">



上面是GOF设计模式中对原型模式的图形结构描述,原型模式通过克隆使我们可以得到一个对象的复制版本.其好处就是让我们在需要一个与现有对象类似的实例时,不用一一进行每个成员的赋值,而是直接通过现有的对象复制.并且复制出来的对象是互相独立的.

如上图,当对象进行了这样的赋值以后,两个变量指向了同一个对象实例,也就是说它们都引用是相同的,在这种情况下,其中一个对象的改变都会影响另一个对象的变化.两个对象互不独立.

</td> <td width="259" valign="top"> </td> </tr> </table>




如果想得到互相独立的两个对象就要使用Clone方法,如上图,经Clone后,任何一个对象的改变都不会影响另一个对象。Object1上Object2是两个不同的引用.


如果一个欲实现Clone方法的对象其成员中还包含其它对象,那么那些对象同样要实现Clone方法,而且在上一层的对象中要调用这些Clone方法.以此类推,直到最后一个对象中没有对象值为止(深拷贝).
在JAVA中,要实现Clone方法就要实现Cloneable接口,但是Clone方法不是这个接口的方法,它只是一个标识接口。Clone方法是从Object对象继承下来的protected方法,在实现它的时候要声明为public。在使用Clone的时候还要记得进行类型转换,因为Clone方法返回的是一个Object。
下面是一小段简单的代码,是对对象克隆的实验:
/**
*ImplClone
**/

/**
*
* @author Bill
*/
class ImplClone implements Cloneable{

private String name="";
private Birth birth;
/** Creates a new instance of ImplClone */
public ImplClone() {
this.setName("bill");
this.birth=new Birth();
}

public Object clone(){
ImplClone cloned=new ImplClone();
try {
cloned = (ImplClone) super.clone();
cloned.birth=(Birth)this.birth.clone();
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
cloned.birth=(Birth)birth.clone();
return cloned;
}

public String getName() {
return name;
}

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

public Birth getBirth() {
return birth;
}

public void setBirth(Birth birth) {
this.birth = birth;
}

}

class Birth implements Cloneable{
private String year;
private String month;
private String day;

public Birth(String year,String month,String day){
this.year=year;
this.month=month;
this.day=day;
}

public Birth(){
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}

public String getMonth() {
return month;
}

public void setMonth(String month) {
this.month = month;
}

public String getDay() {
return day;
}

public void setDay(String day) {
this.day = day;
}

public Object clone(){
Birth cloned=new Birth();
try {
cloned = (Birth) super.clone();
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
return cloned;
}
}

/**
*ImplClone-MainClass
**/
public class CloneMain {

/** Creates a new instance of CloneMain */
public CloneMain() {
}

public static void main(String[] args){
ImplClone cloneObj=new ImplClone();

cloneObj.setName("Bill");
cloneObj.setBirth(new Birth("2004","03","01"));

System.out.println(cloneObj.getBirth().getYear()+" :1");

ImplClone cloned=(ImplClone)cloneObj.clone();
cloned.getBirth().setYear("1990"); //改变克隆对象的数据

System.out.println(cloned.getBirth().getYear()+" :2"); //输出原对象
System.out.println(cloned.getBirth().getYear()+" :3"); //输出改变数据后的克隆对象
}
}

C:\java>java CloneMain
2004 :1
1990 :2
1990 :3

</td> </tr> <tr>


↑返回目录
前一篇: Java中方法和变量在继承时的覆盖问题
后一篇: 直接插入排序