原理介绍
1,浅克隆
浅克隆,只复制当前对象,对该对象内部的引用不能复制。
2,深克隆
深克隆,对对象内部所有对象的引用均复制,每个引用对象创建一个实例并复制实例。
代码:
1,浅克隆
class Student{
String grade;
public Student(){
}
public void setGrade(String a){
this.grade=a;
}
}
class Person2 implements Cloneable{
int age;
String name;
Student grades;
public void setGrades(Student gra){
this.grades=gra;
}
public Person2(int a, String b,Student gra){
age=a;
name=b;
this.grades=gra;
}
public Person2(Person2 p){
this.age=p.age;
this.name=p.name;
}
@Override
public Object clone() {
Person2 per=null;
try {
per=(Person2)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return per;
}
}
public class Body2 {
public static void main(String[] args){
Student stu=new Student();
stu.setGrade("100");
Person2 p1=new Person2(10,"wang",stu);
Person2 p2 = (Person2) p1.clone();
System.out.println(p1.grades==p2.grades);
// 检验子对象的值有没有被修改
System.out.println("*******检验子对象的值有没有被修改********");
stu.setGrade("200");
System.out.println(p2.grades.grade);
System.out.println(p1.grades.grade);
}
}
true
*******检验子对象的值有没有被修改********
200
200
注意:
问:为什么不能直接使用 Object 的 Clone 方法,还要重写 clone() 方法之后才能实现克隆?
答:虽然所有类都是 Object 的子类,但因为 Object 中的 clone() 方法被声明为 protected 访问级别,所以非
java.lang 包下的其他类是不能直接使用的。因此要想实现克隆功能,就必须实现 Cloneable,并重写 clone() 方法才行。
2,深克隆
class Student1 implements Cloneable{
String grade;
public Student1(){
}
public void setGrade(String a){
this.grade=a;
}
public Object clone(){
Student1 stu=null;
try {
stu=(Student1)super.clone();/添加
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
}
class Person3 implements Cloneable{
int age;
String name;
Student1 grades;
public void setGrades(Student1 gra){
this.grades=gra;
}
public Person3(int a, String b,Student1 gra){
age=a;
name=b;
this.grades=gra;
}
public Person3(Person3 p){
this.age=p.age;
this.name=p.name;
this.grades=p.grades;
}
@Override
public Object clone() {
Person3 per=null;
try {
per=(Person3)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/添加
per.grades=(Student1)grades.clone();//深拷贝:就是对引用的对象单独拷贝一次哦。
return per;
}
}
public class Body3 {
public static void main(String[] args){
// TODO Auto-generated method stub
Student1 stu=new Student1();
stu.setGrade("100");
Person3 p1=new Person3(10,"wang",stu);
Person3 p2 = (Person3) p1.clone();
System.out.println(p1.grades==p2.grades);
// 检验子对象的值有没有被修改
System.out.println("*******检验子对象的值有没有被修改********");
stu.setGrade("200");
System.out.println(p2.grades.grade);
System.out.println(p1.grades.grade);
}
}
true
*******检验子对象的值有没有被修改********
200
200
“==” 和equals()的区别
对于基本数据类型(int,float,double,byte,short,long,boolean,char),只能用==
对于基本数据类型的封装类(Boolean,Integer,Long,Float,Double),==比较的是指向对象的地址,比较的是栈内存中存放的对象的引用(地址),equals比较的是值。(这个也好理解,包装类还是一般的类!)
3,复制对象
/**
* 复制map对象
* @explain 将paramsMap中的键值对全部拷贝到resultMap中;
* paramsMap中的内容不会影响到resultMap(深拷贝)
* @param paramsMap
* 被拷贝对象
* @param resultMap
* 拷贝后的对象
*/
public static void mapCopy(Map paramsMap, Map resultMap) {
if (resultMap == null) resultMap = new HashMap();
if (paramsMap == null) return;
Iterator it = paramsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
resultMap.put(key, paramsMap.get(key) != null ? paramsMap.get(key) : "");
}
}
总结:
调用 Object 类中的 clone() 方法默认是浅克隆,浅克隆只能复制值类型,不能复制引用类型,因此更多的场景下我们需要深克隆,深克隆通常的实现方式有两种:序列化方式或把所有的引用类型都实现克隆。
参考链接:
深浅克隆面试题汇总——附详细答案
Java提高篇——对象克隆(复制)
Java中深拷贝(Deep Copy)和浅拷贝(Shallow Copy)学习笔记