浅拷贝:指的是你的类本身被拷贝,而没有拷贝类本身属性中的类。

深拷贝:指的是包含类本身和属性类在内的所有类的拷贝。

简单点说:就是浅拷贝的两个对象中的属性还会指向同一个类,而深拷贝则全部单独了。也就是说深拷贝把关联关系也拷贝了。可以简单理解为深拷贝中的数据类型中含有类类型的变量,因为java中传递参数的形式是以传值方式进行的。以八种基本类型而言,传递的是具体值。而以引用类型,传递的是对象的引用。

本文主要举一下深拷贝的例子,代码如下:

package clonedemo;
public class Student implements Cloneable...{
private String name ;
private int age ;
teacher tea ;
Student(String name , int age,teacher tea)...{
this.name = name ;
this.age = age ;
this.tea = tea ;
}
Student()...{
}
void showInfo(String stu)...{
System.out.println(stu+ " : name ="+this.name) ;
System.out.println(stu+" : age ="+this.age) ;
System.out.println(stu+" : name ="+this.tea.getName()) ;
System.out.println(stu+" : age ="+this.tea.getAge()) ;
}
public Object clone()...{
//Object obj = null ;
Student obj = null ;
try ...{
obj = (Student)super.clone() ;
} catch (CloneNotSupportedException e) ...{
// TODO: handle exception
e.printStackTrace() ;
}
obj.tea = (teacher)tea.clone() ;
return obj ;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public teacher getTea() ...{
return tea;
}
public void setTea(teacher tea) ...{
this.tea = tea;
}
}[SPAN]

若一个类要具有clone功能,那么必须继承Cloneable接口(该接口是一个标记接口,没有任何抽象方法,或许是为了某种标识吧),而且必须重写clone方法(这是一个Object类中的一个protected方法,可见性是包可见和子类可见,这样做的目的是包外的类不能访问该方法,但每一个类都会继承自Object类。因此我们得出一结论,如果在进行系统架构时,可以在根类中定义子类中要用到的方法,不能确定子类是否要使用的可定义为protected类型,子类若想具有某种功能,只需要对该protected方法进行重写即可),然后在该方法中调用父类中的clone方法,在JDK的源码中我们可能看下如下定义:

protected native Object clone() throws CloneNotSupportedException;

在该方法中使用了native关键字(简单地讲,一个native Method就是一个java调用非java代码的接口。一个Native Method是这样一个java的方法:该方法的实现由非java语言实现,比如C。这个特征并非java所特有,很多其它的编程语言都有这一机制,比如在C++中,你可以用extern "C"告知C++编译器去调用一个C的函数.)而且没有方法实现,如果源类没有继承自Cloneable接口,那么编译器会抛出CloneNotSupportedException异常。

我们再看Student.java中用到的teacher.java:

package clonedemo;
public class teacher implements Cloneable...{
private int age ;
private String name ;
teacher(String name,int age)...{
this.age = age ;
this.name = name ;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public Object clone()...{
Object obj = null ;
try ...{
obj = super.clone() ;
} catch (CloneNotSupportedException e) ...{
// TODO: handle exception
e.printStackTrace() ;
}
return obj ;
}
}

在该类中也会继承Cloneable接口,并且重写Clone方法,因为在Student类中会引用到teacher类,那么在clone Student类时,传递的是teacher类的引用,因此我们在Student类中显示的指定对其中的teacher对象进行clone,否则clone 的是teacher对象的引用,对clone后的对象进行操作,也会影响到源对象的。

看客户端:

package clonedemo;
public class client ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stu
teacher tea = new teacher("hello",66) ;
Student stu1 = new Student("jacky",33,tea) ;
Student stu2 = (Student)stu1.clone() ;
stu2.showInfo("stu2") ;
System.out.println("-------------------") ;
stu2.getTea().setName("world") ;
stu2.getTea().setAge(88) ;
stu1.showInfo("stu1") ;
//System.out.println("-------------------") ;
//stu2.showInfo("stu2") ;
}
}

若对stu2进行更改后,stu1中的属性并没有改变,这就是深拷贝后的结果。\