欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实例分析java对象中浅克隆和深克隆

程序员文章站 2024-02-26 16:35:58
引言: 在object基类中,有一个方法叫clone,产生一个前期对象的克隆,克隆对象是原对象的拷贝,由于引用类型的存在,有深克隆和浅克隆之分,若克隆对象中存在引用类...

引言:

在object基类中,有一个方法叫clone,产生一个前期对象的克隆,克隆对象是原对象的拷贝,由于引用类型的存在,有深克隆和浅克隆之分,若克隆对象中存在引用类型的属性,深克隆会将此属性完全拷贝一份,而浅克隆仅仅是拷贝一份此属性的引用。首先看一下容易犯的几个小问题

clone方法是object类的,并不是cloneable接口的,cloneable只是一个标记接口,标记接口是用用户标记实现该接口的类具有某种该接口标记的功能,常见的标记接口有三个:serializable、cloneable、randomaccess,没有实现cloneable接口,那么调用clone方法就会爆出clonenotsupportedexception异常。

object类中的clone方法是protected修饰的,这就表明我们在子类中不重写此方法,就在子类外无法访问,因为这个protected权限是仅仅能在object所在的包和子类能访问的,这也验证了子类重写父类方法权限修饰符可以变大但不能变小的说法。

protected native object clone() throws clonenotsupportedexception;

重写clone方法,内部仅仅是调用了父类的clone方法,其实是为了扩大访问权限,当然你可以把protected改为public,以后再继承就不用重写了。当然只是浅克隆的clone函数,深克隆就需要修改了。

@override  
protected object clone() throws clonenotsupportedexception {    
 return super.clone();
}

属性是string的情况,string也是一个类,那string引用类型吗?string的表现有的像基本类型,归根到底就是因为string不可改变,克隆之后俩个引用指向同一个string,但当修改其中的一个,改的不是string的值,却是新生成一个字符串,让被修改的引用指向新的字符串。外表看起来就像基本类型一样。

浅克隆:

浅克隆就是引用类型的属性无法完全复制,类user中包含成绩属性mark,mark是由chinese和math等等组成的,浅克隆失败的例子

class mark{
  private int chinese;
  private int math;
  public mark(int chinese, int math) {
    this.chinese = chinese;
    this.math = math;
  }
 
  public void setchinese(int chinese) {
    this.chinese = chinese;
  }
 
  public void setmath(int math) {
    this.math = math;
  }
 
  @override
  public string tostring() {
    return "mark{" +
        "chinese=" + chinese +
        ", math=" + math +
        '}';
  }
}
public class user implements cloneable{
  private string name;
  private int age;
  private mark mark;
 
  public user(string name, int age,mark mark) {
    this.name = name;
    this.age = age;
    this.mark = mark;
  }
 
  @override
  public string tostring() {
    return "user{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", mark=" + mark +
        '}';
  }
 
  @override
  protected object clone() throws clonenotsupportedexception {
    return super.clone();
  }
 
  public static void main(string[] args) throws clonenotsupportedexception {
    mark mark = new mark(100,99);
    user user = new user("user",22,mark);
    user userclone = (user) user.clone();
    system.out.println("原user:"+user);
    system.out.println("克隆的user:"+userclone);
    //修改引用类型的mark属性
    user.mark.setmath(60);
    system.out.println("修改后的原user:"+user);
    system.out.println("修改后的克隆user:"+userclone);
  }
}

输出结果为:   

原user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
克隆的user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
修改后的原user:user{name='user', age=22, mark=mark{chinese=100, math=60}}
修改后的克隆user:user{name='user', age=22, mark=mark{chinese=100, math=60}}

很清楚的看到user的mark更改后,被克隆的user也修改了。而要想不被影响,就需要深克隆了。

深克隆:

方式一:clone函数的嵌套调用

既然引用类型无法被完全克隆,那将引用类型也实现cloneable接口重写clone方法,在user类中的clone方法调用属性的克隆方法,也就是方法的嵌套调用

class mark implements cloneable{
  private int chinese;
  private int math;
  public mark(int chinese, int math) {
    this.chinese = chinese;
    this.math = math;
  }
  public void setchinese(int chinese) {
    this.chinese = chinese;
  }
  public void setmath(int math) {
    this.math = math;
  }
  @override
  protected object clone() throws clonenotsupportedexception {
    return super.clone();
  }
  @override
  public string tostring() {
    return "mark{" +
        "chinese=" + chinese +
        ", math=" + math +
        '}';
  }
}
public class user implements cloneable{
  private string name;
  private int age;
  private mark mark;
 
  public user(string name, int age,mark mark) {
    this.name = name;
    this.age = age;
    this.mark = mark;
  }
 
  @override
  public string tostring() {
    return "user{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", mark=" + mark +
        '}';
  }
 
  @override
  protected object clone() throws clonenotsupportedexception {
    user user = (user) super.clone();
    user.mark = (mark) this.mark.clone();
    return user;
  }
 
  public static void main(string[] args) throws clonenotsupportedexception {
    mark mark = new mark(100,99);
    user user = new user("user",22,mark);
    user userclone = (user) user.clone();
    system.out.println("原user:"+user);
    system.out.println("克隆的user:"+userclone);
    //修改引用类型的mark属性
    user.mark.setmath(60);
    system.out.println("修改后的原user:"+user);
    system.out.println("修改后的克隆user:"+userclone);
  }
}

输出结果为: 

原user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
克隆的user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
修改后的原user:user{name='user', age=22, mark=mark{chinese=100, math=60}}
修改后的克隆user:user{name='user', age=22, mark=mark{chinese=100, math=99}}

方式二:序列化

上一种方法已经足够满足我们的需要,但是如果类之间的关系很多,或者是有的属性是数组呢,数组可无法实现cloneable接口(我们可以在clone方法中手动复制数组),但是每次都得手写clone方法,很麻烦,而序列化方式只需要给每个类都实现一个serializable接口,也是标记接口,最后同序列化和反序列化操作达到克隆的目的(包括数组的复制)。序列化和反序列化的知识请参照下一篇

import java.io.*;
class mark implements serializable {
  private int chinese;
  private int math;
  public mark(int chinese, int math) {
    this.chinese = chinese;
    this.math = math;
}
  public void setchinese(int chinese) {
    this.chinese = chinese;
  }
  public void setmath(int math) {
    this.math = math;
  }
  @override
  public string tostring() {
    return "mark{" +
        "chinese=" + chinese +
        ", math=" + math +
        '}';
  }
}
public class user implements serializable{
  private string name;
  private int age;
  private mark mark;
 
  public user(string name, int age,mark mark) {
    this.name = name;
    this.age = age;
    this.mark = mark;
  }
 
  @override
  public string tostring() {
    return "user{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", mark=" + mark +
        '}';
  }
  public static void main(string[] args) throws ioexception, classnotfoundexception {
    mark mark = new mark(100,99);
    user user = new user("user",22,mark);
 
    bytearrayoutputstream bo = new bytearrayoutputstream();
    objectoutputstream oo = new objectoutputstream(bo);
    oo.writeobject(user);//序列化
    bytearrayinputstream bi = new bytearrayinputstream(bo.tobytearray());
    objectinputstream oi = new objectinputstream(bi);
    user userclone = (user) oi.readobject();//反序列化
 
    system.out.println("原user:"+user);
    system.out.println("克隆的user:"+userclone);
    user.mark.setmath(59);
    system.out.println("修改后的原user:"+user);
    system.out.println("修改后的克隆user:"+userclone);
  }
}

输出结果:

原user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
克隆的user:user{name='user', age=22, mark=mark{chinese=100, math=99}}
修改后的原user:user{name='user', age=22, mark=mark{chinese=100, math=60}}
修改后的克隆user:user{name='user', age=22, mark=mark{chinese=100, math=99}}

带数组属性的克隆

import java.io.*;
import java.util.arrays;
 
public class user implements serializable{
  private string name;
  private int age;
  private int[] arr;
 
  public user(string name, int age, int[] arr) {
    this.name = name;
    this.age = age;
    this.arr = arr;
  }
  @override
  public string tostring() {
    return "user{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", arr=" + arrays.tostring(arr) +
        '}';
  }
  public static void main(string[] args) throws ioexception, classnotfoundexception {
    int[] arr = {1,2,3,4,5,6};
    user user = new user("user",22,arr);
 
    bytearrayoutputstream bo = new bytearrayoutputstream();
    objectoutputstream oo = new objectoutputstream(bo);
    oo.writeobject(user);//序列化
    bytearrayinputstream bi = new bytearrayinputstream(bo.tobytearray());
    objectinputstream oi = new objectinputstream(bi);
    user userclone = (user) oi.readobject();//反序列化
 
    system.out.println("原user:"+user);
    system.out.println("克隆的user:"+userclone);
    user.arr[1] = 9;
    system.out.println("修改后的原user:"+user);
    system.out.println("修改后的克隆user:"+userclone);
  }
}