About the Java clone

Original link: https://blog.csdn.net/zhaoheng314/article/details/81985880

Java implementation clones need to follow these rules:

  1. We must implement the interface Cloneable
  2. The implement Cloneable should override clone (), which modifier is overridden to public.
public class CloneTest {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("张三");
        stu.setAge(10);
        Classes classes = new Classes();
        classes.setClassId(101);
        classes.setClassName("一班");
        stu.setClasses(classes);
        try {
            System.out.println("浅克隆测试------"); //克隆
            Student stu2 =(Student) stu.clone (); 
            System.out.println ( "two objects are the same:" + (STU == STU2)); 
            System.out.println ( "name properties of both objects are the same:" + (stu.getName () == stu2.getName ())); 
            System.out.println ( "classes properties of both objects are the same:" + (stu.getClasses () == stu2.getClasses ())); 
            System.out.println ( "shallow clone, Stu" + STU); 
            System.out.println ( "shallow clone, STU2" + STU); 
            System.out.println ( "modify Object properties cloning" ); 
            stu2.setName ( "John Doe"); // modify the name 
            stu2.setAge (20); // modify Age
            . stu2.getClasses () setclassid (102); // Modify number of classes 
            stu2.getClasses () setClassName ( "class two"); // modify class name 
            System.out.println ( "modified cloning object properties, Stu" + STU); 
            System.out.println ( "modified cloning object properties, STU2" + STU2); 
        } the catch (CloneNotSupportedException E) { 
            e.printStackTrace (); 
        } 
    } 


} 

@Data 
class the Classes {
     Private  int classId;
     // basic types 
    Private String className; // reference type // omitted gettters and setters

    @Override
    public String toString() {
        return "Classes [classId=" + classId + ", className=" + className + "]";
    }
//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
}

@Data
class Student implements Cloneable {
    private String name;//引用类型
    private int age;//基本类型
    private Classes classes;//引用类型 //Here gettters and setters omitted 

    @Override 
    public String toString () {
         return "Student [name =" + name + ", Age =" + + Age ", classes =" + + classes "]" ; 
    } 

    @Override 
    public Object clone ( ) throws CloneNotSupportedException { 
        Student STU = (Student) Super .clone ();
 //         the Classes = CLA (the Classes) classes.clone ();
 //         stu.setClasses (CLA); 
        return STU; 
    } 
} 
result:
shallow clones tested - -----
whether two objects are the same: to false
name attribute two objects are the same: true
classes properties of both objects are the same: true
Shallow clone, Stu Student [name = Joe Smith, age = 10, classes = Classes [classId = 101, className = class]]
shallow clone, Stu2 Student [name = Joe Smith, age = 10, classes = Classes [classId = 101, className = class]]
modified cloning object attributes
modified cloning object properties, Stu Student [name = Joe Smith, age = 10, classes = classes [classId = 102, className = double shift]]
to modify the properties of the clone, Stu2 Student [name = John Doe, age = 20, classes = classes [classId = 102, className = second class]]

Results observed by Debug and run the test class properties obtained, can be found in the original object and clone stu stu2 not the same object, but the name attribute classes and properties of two objects are the same object, and the original object and clone stu properties stu2 values are equal, which also verified both "Java clone mechanism class instance attributes one by one copy." name attribute classes and instances of the same type as a reference, and cloned name attribute classes and properties of the original clone stu2 stu objects are the same object, an example of the class does not implement Cloneable clone interface is achieved through the transmission reference.

After modifying individual properties clone stu2 can be found: the original object stu properties of classes to follow the classes of property changes stu2 clone, but the name and age properties do not follow the changes, which is why?

age property not follow the changes easier to understand, because the age attribute value is just passed int type, base type clone-reference does not exist.

The same is by-reference, why not follow the attribute name change, you do not have a special name attribute? The name attribute of type String, String type does not implement Conleable ah, very confused.

Recalls Statement under the String class, public final class String implements ..., final keyword is not very suspicious of it? And then think about the role of the final keyword, one can not change, the second is prohibited command rearrangement. Immutable, unchangeable, what is not thought of? It is precisely because String immutable characteristics, clone stu2 did not change the value of the name attribute, but to modify the reference to the name attribute. This is why the name attribute does not follow the changes in the clone changes, String immutable like to make changes to the deep shallow clone clone.

public class CloneTest {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("张三");
        stu.setAge(10);
        Classes classes = new Classes();
        classes.setClassId(101);
        classes.setClassName("一班");
        stu.setClasses(classes);
        try {
            System.out.println("浅克隆测试------"); //克隆
            Student stu2 =(Student) stu.clone (); 
            System.out.println ( "two objects are the same:" + (STU == STU2)); 
            System.out.println ( "name properties of both objects are the same:" + (stu.getName () == stu2.getName ())); 
            System.out.println ( "classes properties of both objects are the same:" + (stu.getClasses () == stu2.getClasses ())); 
            System.out.println ( "shallow clone, Stu" + STU); 
            System.out.println ( "shallow clone, STU2" + STU); 
            System.out.println ( "modify Object properties cloning" ); 
            stu2.setName ( "John Doe"); // modify the name 
            stu2.setAge (20); // modify Age
            . stu2.getClasses () setclassid (102); // Modify number of classes 
            stu2.getClasses () setClassName ( "class two"); // modify class name 
            System.out.println ( "modified cloning object properties, Stu" + STU); 
            System.out.println ( "modified cloning object properties, STU2" + STU2); 
        } the catch (CloneNotSupportedException E) { 
            e.printStackTrace (); 
        } 
    } 


} 

@Data 
class the Classes the implements the Cloneable {
     Private  int classId;
     // basic types of 
    Private String className; // reference type//这里省略gettters and setters

    @Override
    public String toString() {
        return "Classes [classId=" + classId + ", className=" + className + "]";
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

@Data
class Student implements Cloneable {
    private String name;//引用类型
    private int age;//基本类型
    private Classes classes;//Reference type // here gettters and setters omitted 

    @Override 
    public String toString () {
         return "Student [name =" + name + ", Age =" + + Age ", classes =" + + classes "]" ; 
    } 

    @Override 
    public Object clone () throws CloneNotSupportedException { 
        Student STU = (Student) Super .clone (); 
        the Classes CLA = (the Classes) classes.clone (); 
        stu.setClasses (CLA); 
        return STU; 
    } 
} 

deep clones tested --- ---
two objects are the same: false
name properties of two objects are the same: to true
classes properties of two objects are the same: false
Deep clone , Stu Student [name = Joe Smith, age = 10, classes = Classes [classId = 101, className = class]]
deep clone , Stu2 Student [name = Joe Smith, age = 10, classes = Classes [classId = 101, className = class]]
modified cloning object attributes
modified cloning object properties, Stu Student [name = Joe Smith, age = 10, classes = classes [classId = 101, className = class]]
to modify the properties of the clone, Stu2 Student [name = John Doe, age = 20, classes = classes [classId = 102, className = second class]]

Debug observation results obtained by the properties and run the test class cloned stu original object classes and attribute clone stu2 is not the same object. After modifying the properties of each clone stu2 it can be found: the properties of the original object stu not follow changes in the attribute change stu2 clone.

By Java clone of learning and testing mechanisms, we come to a conclusion: If you want to refer to the type of property to an instance of a class's implementation of deep clone, the type of property required for the class reference implementation Cloneable interface and override the clone ().

Guess you like

Origin www.cnblogs.com/bokai/p/11584258.html