java clone 深度克隆 浅度克隆_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java clone 深度克隆 浅度克隆

java clone 深度克隆 浅度克隆

 2013/11/21 18:28:12  a151555  程序员俱乐部  我要评论(0)
  • 摘要:一,在持久层操作时,有时需要操纵hibernatesession内实例,但是又不想影响到原实例,这时克隆就必不可少.复制一份一样的实例.进行传值.克隆Object的方法.protectednativeObjectclone()throwsCloneNotSupportedException;CloneNotSupportedException表示你必须为你要克隆的类实现Cloneable接口publicinterfaceCloneable例子一,User类packagecom.ming
  • 标签:Java
,在持久层操作时,有时需要操纵hibernate session内实例,但是又不想影响到原实例,这时克隆就必不可少.复制一份一样的实例.进行传值.

   克隆Object的方法.
  
class="java"> protected native Object clone() throws CloneNotSupportedException;

  
   CloneNotSupportedException表示你必须为你要克隆的类实现Cloneable接口
  
public interface Cloneable 

  例子一,User类
package com.ming.test.three;

public class User implements Cloneable{
	private String username;
	private Integer age;
	private int sex;
	private Car car;
	public User(String username,Integer age,int sex,Car car){
		this.username = username;
		this.age = age;
		this.sex = sex;
		this.car = car;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		 // TODO Auto-generated method stub  
        User u = (User)super.clone();  
        //u.setUsername(new String(username));//在这里你可以进行重新生成注入进去  
        //u.setAge(new Integer(age));//在这里进行改良  
        //----如果是自定义类型  
        //u.setCar((Car)car.clone());//car 是你当前克隆对象的属性,而且Car必须实现Cloneable接口  
        return u; 
	}
	
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

Car 类
package com.ming.test.three;

import java.io.Serializable;

/**
 * @author user
 *
 */
public class Car implements Cloneable{

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
}

   测试类,Test
package com.ming.test.three;
package com.ming.test.three;

public class Test {
	public static void main(String[] args) throws CloneNotSupportedException {
		User u = new User("jacky",11,1,new Car());
		User u1 = (User)u.clone();//克隆一份
		System.out.println(u==u1);//false 表示重新生成了一份
		System.out.println(u.getUsername()==u1.getUsername());//true 表示引用类型 只是copy了栈的引用为重新复制 这为浅克隆
		System.out.println(u.getCar()==u1.getCar());//true
	}
}


    深克隆的实现方式,对于引用类型的复制,必须要深度克隆,一下有俩种方式实现。
1.在你需要clone的类中

   
@Override
	protected Object clone() throws CloneNotSupportedException {
		 // TODO Auto-generated method stub  
        User u = (User)super.clone();  
        u.setUsername(new String(username));//在这里你可以进行重新生成注入进去  
        u.setAge(new Integer(age));//在这里进行改良  
  
        //----如果是自定义类型  
        u.setCar(car.clone());//car 是你当前克隆对象的属性,而且Car必须实现Cloneable接口  
        return u; 
	}


2.实现 implements Serializable
 
   User类
package com.ming.test.three;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class User implements Cloneable, Serializable{
	
	/**	 */
	private static final long serialVersionUID = 8574879580125565714L;
	
	private String username;
	private Integer age;
	private int sex;
	private Car car;
	public User(String username,Integer age,int sex,Car car){
		this.username = username;
		this.age = age;
		this.sex = sex;
		this.car = car;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		 // TODO Auto-generated method stub  
        User u = (User)super.clone();  
       // u.setUsername(new String(username));//在这里你可以进行重新生成注入进去  
        //u.setAge(new Integer(age));//在这里进行改良  
        //----如果是自定义类型  
       // u.setCar((Car)car.clone());//car 是你当前克隆对象的属性,而且Car必须实现Cloneable接口  
        return u; 
	}
	
	public Object deepCopy() throws IOException, ClassNotFoundException{  
	     ByteArrayOutputStream bos = new ByteArrayOutputStream();  
	     ObjectOutputStream oos;
	     ObjectInputStream ois = null;
		 oos = new ObjectOutputStream(bos);
		 oos.writeObject(this);
		 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
		 ois = new ObjectInputStream(bis);
	    return ois.readObject();  
	}  
	
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

    Car类
 
package com.ming.test.three;

import java.io.Serializable;

/**
 * @author user
 *
 */
public class Car implements Cloneable, Serializable{

	/**	 */
	private static final long serialVersionUID = -740464901844332572L;

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
}

Test类
package com.ming.test.three;

public class Test {
	public static void main(String[] args) throws CloneNotSupportedException {
		//-----深度克隆
		Car  car = new Car();
		User u2 = new User("ming",111,11,car);
		try {
			User u3 = (User) u2.deepCopy();
			System.out.println(u3==u2);//false
			System.out.println(u3.getCar()==u2.getCar());//false
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
发表评论
用户名: 匿名