Home  >  Article  >  Java  >  Example tutorial of object cloning (Clone) in Java

Example tutorial of object cloning (Clone) in Java

零下一度
零下一度Original
2017-07-19 10:00:011485browse

Part II<br>

When talking about object cloning, we have to talk about why objects should be cloned. All objects in Java are stored in the heap, and the heap is shared globally. In other words, if different methods of the same Java program can get a reference to an object, the referrer can modify the internal data of the object at will (provided that the internal data of the object is exposed through the get/set method) . Sometimes, the code we write wants the caller to get only one copy of the object (that is, an object with exactly the same content, but there are two such objects in the memory). Is there any way to do this? Of course it's a clone.

Part III

First of all, we are programmers, and of course we communicate in the language of our programmers.

import java.util.Date;
public class User implements Cloneable {
	private String username;
	private String password;
	private Date birthdate;
	public User(String username, String password, Date birthdate) {
		this.username = username;
		this.password = password;
		this.birthdate = birthdate;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	@Override
	public int hashCode() {
		// 省略equals的实现(可用eclipse自动生成)
	}
	@Override
	public boolean equals(Object obj) {
		// 省略equals的实现(可用eclipse自动生成)
	}

The above code builds a User class and implements the java.lang.Cloneable interface. As the name suggests, Cloneable means that this class can be cloned.

Let’s first take a look at what the java.lang.Cloneable interface has.

	// 省略一大堆get/set方法
}
/*
 * @(#)Cloneable.java	1.17 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.lang;
/**
 * A class implements the Cloneable interface to
 * indicate to the {@link java.lang.Object#clone()} method that it
 * is legal for that method to make a

 * field-for-field copy of instances of that class.
 *
 * Invoking Object's clone method on an instance that does not implement the
 * Cloneable interface results in the exception

 * CloneNotSupportedException being thrown.
 *
 * By convention, classes that implement this interface should override
 * Object.clone (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this

 * method.
 *
 * Note that this interface does not contain the clone method.
 * Therefore, it is not possible to clone an object merely by virtue of the
 * fact that it implements this interface.  Even if the clone method is invoked
 * reflectively, there is no guarantee that it will succeed.
 *
 * @author  unascribed
 * @version 1.17, 11/17/05
 * @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0

Don’t be surprised, yes, except for a lot of chicken intestines, this interface does not define any methods sign. In other words, we want to clone an object, but it doesn't provide me with a method. So what to do? Don't be afraid, we still have the almighty Object class. Don't forget that he is the ancestor of all classes (a god-like existence), so you should go and greet him whenever you have nothing to do.

 */
public interface Cloneable {
}
/*
 * @(#)Object.java	1.73 06/03/30
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.lang;
/**
 * Class Object is the root of the class hierarchy.
 * Every class has Object as a superclass. All objects,
 * including arrays, implement the methods of this class.
 *
 * @author  unascribed
 * @version 1.73, 03/30/06
 * @see     java.lang.Class
 * @since   JDK1.0
 */
public class Object {
    <br>
   // 省略N多的代码
    /**
     * Creates and returns a copy of this object.  The precise meaning
     * of "copy" may depend on the class of the object. The general
     * intent is that, for any object x, the expression:
     *
*
     * x.clone() != x
     * will be true, and that the expression:
     *
*
rrree

     * x.clone().getClass() == x.getClass()
     * will be true, but these are not absolute requirements.
     * While it is typically the case that:
     *
*

     * x.clone().equals(x)
     * will be true, this is not an absolute requirement.
     *
     * By convention, the returned object should be obtained by calling
     * super.clone.  If a class and all of its superclasses (except
     * Object) obey this convention, it will be the case that
     * x.clone().getClass() == x.getClass().
     *
     * By convention, the object returned by this method should be independent
     * of this object (which is being cloned).  To achieve this independence,
     * it may be necessary to modify one or more fields of the object returned

     * by super.clone before returning it.  Typically, this means
     * copying any mutable objects that comprise the internal "deep structure"
     * of the object being cloned and replacing the references to these
     * objects with references to the copies.  If a class contains only
     * primitive fields or references to immutable objects, then it is usually
     * the case that no fields in the object returned by super.clone
     * need to be modified.
     *
     * The method clone for class Object performs a
     * specific cloning operation. First, if the class of this object does
     * not implement the interface Cloneable, then a

     * CloneNotSupportedException is thrown. Note that all arrays
     * are considered to implement the interface Cloneable.
     * Otherwise, this method creates a new instance of the class of this
     * object and initializes all its fields with exactly the contents of
     * the corresponding fields of this object, as if by assignment; the
     * contents of the fields are not themselves cloned. Thus, this method
     * performs a "shallow copy" of this object, not a "deep copy" operation.
     *
     * The class Object does not itself implement the interface
     * Cloneable, so calling the clone method on an object
     * whose class is Object will result in throwing an
     * exception at run time.
     *
     * @return     a clone of this instance.

Haha, another big bunch of chicken intestines, don’t think I’m here To make up for the word count, these are technical articles written by Sun Java developers. Read more and talk less.

Yes, it is a native method. It is indeed a profound thing, but we still have to take advantage of it. Moreover, his method is protected, and he is clearly asking us to take advantage.

Continue to look at the test code below.

     * @exception  CloneNotSupportedException  if the object's class does not
     *               support the Cloneable interface. Subclasses
     *               that override the clone method can also
     *               throw this exception to indicate that an instance cannot
     *               be cloned.
     * @see java.lang.Cloneable
     */
    protected native Object clone() throws CloneNotSupportedException;
}
import java.util.Date;
import org.junit.Test;
public class TestCase {
<br>
	@Test
	public void testUserClone() throws CloneNotSupportedException {
		User u1 = new User("Kent", "123456", new Date());

This clone() method is really awesome. It clones our object in one go. The execution results are also in line with our expectations. The addresses of u1 and u3 are different but the content is the same.

Part IV

Through the above example, we can see that there are actually two steps to clone an object:

1. Let this class implement the java.lang.Cloneable interface;

2. Override (override) the clone() method of the Object class.

But, is it really that simple? Look at the code below again.

		User u2 = u1;
		User u3 = (User) u1.clone();
<br>
		System.out.println(u1 == u2);		// true
		System.out.println(u1.equals(u2));	// true
<br>
		System.out.println(u1 == u3);		// false
		System.out.println(u1.equals(u3));	// true
	}
<br>
}
public class Administrator implements Cloneable {
	private User user;
	private Boolean editable;
	public Administrator(User user, Boolean editable) {
		this.user = user;
		this.editable = editable;
	}
<br>
	@Override

The above defines an Administrator class, which holds an object of the User class. Next, let's take a look at the effect of cloning the Administrator object.

	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	@Override
	public int hashCode() {
		// 老规矩
	}
	@Override
	public boolean equals(Object obj) {
		// 老规矩
	}
	// 老规矩
}
import java.util.Date;
import org.junit.Test;
public class TestCase {
	@Test
	public void testAdministratorClone() throws CloneNotSupportedException {

Hehehe! Something went wrong. Java is so easy to master!

Here we can introduce two professional terms: shallow clone and deep clone.

The so-called shallow cloning, as the name suggests, is a very superficial clone. If we want to clone the Administrator object, we only clone itself and the reference addresses of all the objects it contains.

And deep cloning is not shallow cloning. Clone all objects except itself, including all object instances contained in itself. As for the level of deep cloning, it is determined by specific needs. There is also a saying of "N-level cloning".

However, all primitive type data, whether shallow clone or deep clone, will be cloned from the original value. After all, they are not objects and are not stored in the heap. Note: Basic data types do not include their corresponding wrapper classes.

If we want the object to be deeply cloned, we can modify the Administrator class like this.

		Administrator a1 = new Administrator(new User("Kent", "123456", new Date()), true);
		Administrator a2 = a1;
		Administrator a3 = (Administrator) a1.clone();
<br>
		System.out.println(a1 == a2);			// true
		System.out.println(a1.equals(a2));		// true

Since Boolean will cache the value, we do not need to clone the Boolean object. And the Boolean class does not implement the java.lang.Cloneable interface.

Part V

1. 让该类实现java.lang.Cloneable接口;

2. 确认持有的对象是否实现java.lang.Cloneable接口并提供clone()方法;

3. 重写(override)Object类的clone()方法,并且在方法内部调用持有对象的clone()方法;

4. ……

5. 多麻烦啊,调来调去的,如果有N多个持有的对象,那就要写N多的方法,突然改变了类的结构,还要重新修改clone()方法。

难道就没有更好的办法吗?

Part VI

接下来要重点介绍一下使用java.lang.Serializable来实现对象的深度克隆。

首先,我们编写一个工具类并提供cloneTo()方法。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public abstract class BeanUtil {
	@SuppressWarnings("unchecked")
	public static  T cloneTo(T src) throws RuntimeException {
		ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		ObjectInputStream in = null;
		T dist = null;
		try {
			out = new ObjectOutputStream(memoryBuffer);
			out.writeObject(src);
			out.flush();
			in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
			dist = (T) in.readObject();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (out != null)
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			if (in != null)
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
		}
		return dist;
	}
}

看不懂,没关系,直接拿去用就可以了。嘻嘻。

接下来我们测试一下是否能通过这个工具来实现深度克隆。

又是这个可爱的TestCase,可怜的每次都要动他……

import java.util.Date;
import org.junit.Test;
public class TestCase {
<br>
	@Test
	public void testCloneTo() {
		Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
		Administrator dist = BeanUtil.cloneTo(src);
<br>
		System.out.println(src == dist);			// false
		System.out.println(src.equals(dist));		// true
<br>
		System.out.println(src.getUser() == dist.getUser());		//false ! Well done!
		System.out.println(src.getUser().equals(dist.getUser()));	//true
	}
<br>
}

好了,无论你的对象有多么的复杂,只要这些对象都能够实现java.lang.Serializable接口,就可以进行克隆,而且这种克隆的机制是JVM完成的,不需要修改实体类的代码,方便多了。

The above is the detailed content of Example tutorial of object cloning (Clone) in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn