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

Java中的深拷贝(深复制)和浅拷贝(浅复制)介绍

程序员文章站 2024-03-31 16:48:28
深拷贝(深复制)和浅拷贝(浅复制)是两个比较通用的概念,尤其在c++语言中,若不弄懂,则会在delete的时候出问题,但是我们在这幸好用的是java。虽然java自动管理对...

深拷贝(深复制)和浅拷贝(浅复制)是两个比较通用的概念,尤其在c++语言中,若不弄懂,则会在delete的时候出问题,但是我们在这幸好用的是java。虽然java自动管理对象的回收,但对于深拷贝(深复制)和浅拷贝(浅复制),我们还是要给予足够的重视,因为有时这两个概念往往会给我们带来不小的困惑。

浅拷贝是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。深拷贝不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。举例来说更加清楚:对象a1中包含对b1的引用,b1中包含对c1的引用。浅拷贝a1得到a2,a2 中依然包含对b1的引用,b1中依然包含对c1的引用。深拷贝则是对浅拷贝的递归,深拷贝a1得到a2,a2中包含对b2(b1的copy)的引用,b2 中包含对c2(c1的copy)的引用。

若不对clone()方法进行改写,则调用此方法得到的对象即为浅拷贝,下面我们着重谈一下深拷贝。

运行下面的程序,看一看浅拷贝:

class professor0 implements cloneable { 
  string name; 
  int age; 
 
  professor0(string name, int age) { 
    this.name = name; 
    this.age = age; 
  } 
 
  public object clone() throws clonenotsupportedexception { 
    return super.clone(); 
  } 
} 
 
class student0 implements cloneable { 
  string name;// 常量对象。 
  int age; 
  professor0 p;// 学生1和学生2的引用值都是一样的。 
 
  student0(string name, int age, professor0 p) { 
    this.name = name; 
    this.age = age; 
    this.p = p; 
  } 
 
  public object clone() { 
    student0 o = null; 
    try { 
      o = (student0) super.clone(); 
    } catch (clonenotsupportedexception e) { 
      system.out.println(e.tostring()); 
    } 
 
    return o; 
  } 
} 
 
public class shallowcopy { 
  public static void main(string[] args) { 
    professor0 p = new professor0("wangwu", 50); 
    student0 s1 = new student0("zhangsan", 18, p); 
    student0 s2 = (student0) s1.clone(); 
    s2.p.name = "lisi"; 
    s2.p.age = 30; 
    s2.name = "z"; 
    s2.age = 45; 
    system.out.println("学生s1的姓名:" + s1.name + "\n学生s1教授的姓名:" + s1.p.name + "," + "\n学生s1教授的年纪" + s1.p.age);// 学生1的教授 
  } 
}

s2变了,但s1也变了,证明s1的p和s2的p指向的是同一个对象。这在我们有的实际需求中,却不是这样,因而我们需要深拷贝:

class professor implements cloneable { 
  string name; 
  int age; 
 
  professor(string name, int age) { 
    this.name = name; 
    this.age = age; 
  } 
 
  public object clone() { 
    object o = null; 
    try { 
      o = super.clone(); 
    } catch (clonenotsupportedexception e) { 
      system.out.println(e.tostring()); 
    } 
    return o; 
  } 
} 
 
class student implements cloneable { 
  string name; 
  int age; 
  professor p; 
 
  student(string name, int age, professor p) { 
    this.name = name; 
    this.age = age; 
    this.p = p; 
  } 
 
  public object clone() { 
    student o = null; 
    try { 
      o = (student) super.clone(); 
    } catch (clonenotsupportedexception e) { 
      system.out.println(e.tostring()); 
    } 
    o.p = (professor) p.clone(); 
    return o; 
  } 
} 
 
public class deepcopy { 
  public static void main(string args[]) { 
    long t1 = system.currenttimemillis(); 
    professor p = new professor("wangwu", 50); 
    student s1 = new student("zhangsan", 18, p); 
    student s2 = (student) s1.clone(); 
    s2.p.name = "lisi"; 
    s2.p.age = 30; 
    system.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age);// 学生1的教授不改变。 
    long t2 = system.currenttimemillis(); 
    system.out.println(t2-t1); 
  } 
}

当然我们还有一种深拷贝方法,就是将对象串行化:

import java.io.*; 
//serialization is time-consuming 
class professor2 implements serializable { 
  /** 
   * 
   */
  private static final long serialversionuid = 1l; 
  string name; 
  int age; 
 
  professor2(string name, int age) { 
    this.name = name; 
    this.age = age; 
  } 
} 
 
class student2 implements serializable { 
  /** 
   * 
   */
  private static final long serialversionuid = 1l; 
  string name;// 常量对象。 
  int age; 
  professor2 p;// 学生1和学生2的引用值都是一样的。 
 
  student2(string name, int age, professor2 p) { 
    this.name = name; 
    this.age = age; 
    this.p = p; 
  } 
 
  public object deepclone() throws ioexception, optionaldataexception, 
      classnotfoundexception { 
    // 将对象写到流里 
    bytearrayoutputstream bo = new bytearrayoutputstream(); 
    objectoutputstream oo = new objectoutputstream(bo); 
    oo.writeobject(this); 
    // 从流里读出来 
    bytearrayinputstream bi = new bytearrayinputstream(bo.tobytearray()); 
    objectinputstream oi = new objectinputstream(bi); 
    return (oi.readobject()); 
  } 
 
} 
 
public class deepcopy2 { 
 
  /** 
   * @param args 
   */
  public static void main(string[] args) throws optionaldataexception, 
      ioexception, classnotfoundexception { 
    long t1 = system.currenttimemillis(); 
    professor2 p = new professor2("wangwu", 50); 
    student2 s1 = new student2("zhangsan", 18, p); 
    student2 s2 = (student2) s1.deepclone(); 
    s2.p.name = "lisi"; 
    s2.p.age = 30; 
    system.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // 学生1的教授不改变。 
    long t2 = system.currenttimemillis(); 
    system.out.println(t2-t1); 
  } 
 
}

但是串行化却很耗时,在一些框架中,我们便可以感受到,它们往往将对象进行串行化后进行传递,耗时较多。