Java 深复制 浅复制

阅读更多

Java当中的copy和clone的疑惑主要是对于是否引用地址发生了根本的改变,网上有很多地方有讲解的,再次不再多说,为了便于你的理解,可以参考此链接地址的:Clone & Copy.

个人觉得就是copy就是浅复制,如果把copy的对象当做一个容器,那么只是复制了这个容器,容器里面的东西还是原来。只不过java的基本类型的特殊性,导致如果对象的属性都是基本类型时,貌似连基本属性也复制了。

clone就是深复制,就是连容器和容器里面的属性,如果属性是复杂类型的话,也全部复制,导致了生成的对象从里带外都是新的。

在这里是指做一个代码的验证。

Demo 类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.vo.Emotion;
import com.vo.Society;
import com.vo.User;

public class Dom4JDemo {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws DocumentException 
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 * @throws CloneNotSupportedException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException, CloneNotSupportedException{
		User user1 = new User();
		user1.setName("kenny");
		Emotion emotion1 = new Emotion();
		emotion1.setName("emotion1");
		user1.setEmotion(emotion1);
		List list1 = new ArrayList();
		Society s1 = new Society();
		s1.setName("society1");
		list1.add(s1);
		user1.setSocieties(list1);
	    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
	    ObjectOutputStream out = new ObjectOutputStream(byteout);
	    out.writeObject(user1);
	    ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
	    ObjectInputStream in = new ObjectInputStream(bytein);
	   // User newUser = (User)in.readObject();// 深复制
	    User newUser = user1.clone();//也是深复制,但是非常的繁琐,如果以后user新增了新的复杂类型,还得修改clone方法。
	    System.out.println("Before change,oldUser's emotion name=" + user1.getEmotion().getName());
	    System.out.println("Before change,newUser's emotion name=" + newUser.getEmotion().getName());
	    newUser.getEmotion().setName("emotion2");// change newUser's emotion name
	    System.out.println("After change,oldUser's emotion name=" + user1.getEmotion().getName());
	    System.out.println("After change,newUser's emotion name=" + newUser.getEmotion().getName());
	    
	    System.out.println("Before change,oldUser's society name=" + user1.getSocieties().get(0).getName());
	    System.out.println("Before change,newUser's society name=" + newUser.getSocieties().get(0).getName());
	    newUser.getSocieties().get(0).setName("society2");
	    System.out.println("After change,oldUser's society name=" + user1.getSocieties().get(0).getName());
	    System.out.println("After change,newUser's society name=" + newUser.getSocieties().get(0).getName());
	    
	    User user2 = user1.clone();
	    System.out.print(user2);
	    
	}
 }

 com.vo.Emotion 类

package com.vo;

import java.io.Serializable;

/**
 * @author:kenny dong
 */
public class Emotion implements Serializable,Cloneable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Emotion clone() throws CloneNotSupportedException {
        return (Emotion) super.clone();
	}
}

 com.vo.society 类

package com.vo;

import java.io.Serializable;

/**
 * @author:kenny dong
 */
public class Society implements Serializable,Cloneable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Society clone() throws CloneNotSupportedException {
        return (Society) super.clone();
	}
}

 com.vo.User 类

package com.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author:kenny dong
 */
public class User implements Serializable,Cloneable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private Emotion emotion;
	private List societies;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Emotion getEmotion() {
		return emotion;
	}
	public void setEmotion(Emotion emotion) {
		this.emotion = emotion;
	}
	public List getSocieties() {
		return societies;
	}
	public void setSocieties(List societies) {
		this.societies = societies;
	}
	//这里面仅仅clone自己是不行的,还要手动把复杂对象挨个clone一遍才行。
	public User clone() throws CloneNotSupportedException {
		User user = (User) super.clone();
		if(emotion != null){
			user.setEmotion(emotion.clone());
		}
		List copyLists = new ArrayList();
		if(societies != null){
			for(Society s : societies){
				copyLists.add(s.clone());
			}
			user.setSocieties(copyLists);
		}
        return user;
	}
}

 

 

你可能感兴趣的:(java,copy,clone,深复制,浅复制)