First, we need to know is, what is the object of cloning? Object cloning is when the program is running, you may need a new object, to save the current state of the object and the new object and the current object has no association, that I changed the value of the new object properties, and the current object has not changed. This new object, the current object is to clone, or copy.
To complete the clone objects, have the following requirements:
1. The new object with the same value of the current object
2. The new object to the current object reference to a different address
To achieve the object clone, the following steps:
1. Inheritance Cloneable interface class corresponding to the object to be cloned, and the clone method overloads, the public access into
2. The method clone the current object is assigned to the new object, the object to complete cloning.
public class Test implements Cloneable{ public int a; public int b; private int c; private int d; public String aa; private String bb; public TestIn testIn = new TestIn(); public TestIns testIns = new TestIns(); @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } public Test(){} public Test(int a,int b,int c,int d){ this.a=a; this.b=b; this.c=c; this.d=d; } private Test(int c,int d){ this.c=c; this.d=d; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public int getD() { return d; } public void setD(int d) { this.d = d; } private void A(){ System.out.println("哈哈哈"); } public void B(){ System.out.println("fdsa"); } public String getAa() { return aa; } public void setAa(String aa) { this.aa = aa; } public String getBb() { return bb; } public void setBb(String bb) { this.bb = bb; } @Override public String toString() { return "Test [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", aa=" + aa + ", bb=" + bb + "]"; } class TestIn{ public String test_a; public int test_b; } class TestIns{ public String test_a; public int test_b; } }
= New new TEST_1 the Test the Test ();
the Test test_2 = (the Test) test_1.clone (); System.out.println ( "\ R & lt ++++++++++ copy (clone) +++++++++++" ); the System.out .println ( "pre-assignment ---------- ----------" ); System.out.println ( "TEST_1:" + TEST_1); System.out.println ( " test_2: "+ test_2); test_2.setA ( . 4 ); System.out.println ( " base type assignment ---------- ---------- " ); the System.out .println ( "TEST_1:" + TEST_1); System.out.println ( "test_2:" + test_2);
System.out.println ( "addresses refer to the same class is:" + (test_1 == test_2) );
The results of running the above code is:
++++++++++ copy (clone) +++++++++++ before assignment ---------- ---------- TEST_1: the Test [A = 2, B = 0, C = 0, D = 0, AA = null , BB = null ] test_2: the Test [A = 2, B = 0, C = 0, D = 0, AA = null , BB = null ] ---------- assignment basic types ---------- TEST_1: the Test [A = 2, B = 0, C = 0, D = 0, AA = null , = BB null ] test_2: the Test [A =. 4, B = 0, C = 0, D = 0, AA = null , BB = null ] class is referenced with addresses: to false
Thus, we achieve a clone, of course, just for the current Basic types of cloning, cloning we call such an object shallow clone.
And if you want to achieve the basic data types and reference types of cloning, we need a deep clone.
In the test class, you can see, there are two classes of objects, one TestIn class, a class is TestIns, and are initialized, but in the case of a shallow clone, they point to address is the same
System.out.println ( "Class references refer to the same class is TestIn addresses:" + (test_1.testIn == test_2.testIn));
System.out.println ( "class with reference to whether the reference class TestIns address: "+ (test_1.testIns == test_2.testIns)) ;
Its operating results:
Reference to the class whether the class TestIn refer to the same address: to true reference class TestIns classes are referring to the same address: to true
How deep clone it, in fact, achieve deep cloning and realization of the principle shallow clone of similar steps are as follows:
1. The clone corresponding to the object class to inherit Cloneable interface and override the clone method
2. cloning the object if they are, there is a property of the object to be cloned, it is necessary to rewrite the clone method
3. The method clone of the current object assigned to the new object, the object to complete deep cloning.
The following code I rewrote Test of clone method, and make the corresponding class TestIns inherited the Cloneable interface, overloaded clone method
public class Test implements Cloneable{ public int a; public int b; private int c; private int d; public String aa; private String bb; public TestIn testIn = new TestIn(); public TestIns testIns = new TestIns(); @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub Test test = (Test)super.clone(); test.testIns =(TestIns) this.testIns.clone(); return test; } public Test(){} public Test(int a,int b,int c,int d){ this.a=a; this.b=b; this.c=c; this.d=d; } private Test(int c,int d){ this.c=c; this.d=d; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public int getD() { return d; } public void setD(int d) { this.d = d; } private void A(){ System.out.println("哈哈哈"); } public void B(){ System.out.println("fdsa"); } public String getAa() { return aa; } public void setAa(String aa) { this.aa = aa; } public String getBb() { return bb; } public void setBb(String bb) { this.bb = bb; } @Override public String toString() { return "Test [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", aa=" + aa + ", bb=" + bb + "]"; } class TestIn{ public String test_a; public int test_b; } class TestIns implements Cloneable{ public String test_a; public int test_b; @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } } }
System.out.println ( "Class references refer to the same class is TestIn addresses:" + (test_1.testIn == test_2.testIn));
System.out.println ( "class with reference to whether the reference class TestIns address: "+ (test_1.testIns == test_2.testIns)) ;
Operating results are as follows:
Reference to the class whether the class TestIn refer to the same address: to true reference class TestIns classes are referring to the same address: false
Interested parties can go to find out why there are differences cloning shallow and deep cloning clones: https://www.jianshu.com/p/b597f3e8269a
Summary: either deep or shallow clone clone, corresponding to required classes inherit Cloneable interface, and the clone method overloaded, or overwritten; shallow clones, only clone basic types of classes, and a deep clone may be cloned essentially basic types and references to Types of.
public class TestMain{ public static void main(String[] args){ Test test_1 = new Test(); Test test_2 = test_1; System.out.println("++++++++++引用+++++++++++"); System.out.println("----------赋值前----------"); System.out.println("test_1:"+test_1); System.out.println("test_2:"+test_2); test_2.setA(2); System.out.println("----------赋值后----------"); System.out.println ( "TEST_1:" + TEST_1); System.out.println ( "test_2:" + test_2); System.out.println ( "addresses refer to the same class is:" + (TEST_1 == test_2) ); the try { test_2 = (the Test) test_1.clone (); System.out.println ( "\ R & lt ++++++++++ copy (clone) +++++++++++" ); System.out.println ( "- --------- former assignment ---------- " ); System.out.println ( " TEST_1: "+ TEST_1); System.out.println ( " test_2: "+ test_2 ); test_2.setA ( . 4 ); System.out.println ( "base type assignment ---------- ----------" ); System.out.println ( "TEST_1:" + TEST_1); the System.out .println ( "test_2:" + test_2); System.out.println ( "addresses refer to the same class is:" + (TEST_1 == test_2)); System.out.println ( whether "category refer to the same reference class TestIn address: "+ (test_1.testIn == test_2.testIn)); System.out.println ( " class with reference to whether the reference class TestIns addresses: "+ (test_1.testIns == test_2.testIns)); } the catch (CloneNotSupportedException E) { // the TODO Auto-Generated the catch Block E.printStackTrace (); } } }