Student class:
package clone clone;/*To clone, this excuse must be implemented: Cloneable, to mark this object, Cloneable: This class implements the Cloneable interface to indicate that the Object.clone() method can legally copy the instance of this class by field. This interface is a tag interface, telling us that the class implementing this interface can realize the copying of objects. */public class Student implements Cloneable {private String name;private int age;public Student() {}public Student(String name, int age) {super();this.name = name;this.age = age;}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;}// Rewrite the cloning method subcolumn to call @Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}}Test cloning student class:
package clone clone;/* *protected void finalize(): This method is called by the object's garbage collector when the garbage collector determines that no more references to the object exist. Used for garbage recycling, but it is uncertain when it will be recycled. *protected Object clone(): Creates and returns a copy of this object. *A: Rewrite this method * * Cloneable: This class implements the Cloneable interface to indicate that the Object.clone() method can legally copy the instance of this class by field. * This interface is a tag interface, telling us that the class implementing this interface can realize the copying of objects. */public class StudentClone {public static void main(String[] args) throws CloneNotSupportedException {//Create student object Student s = new Student();s.setName("Qingxia Lin");s.setAge(27);//Clone student object Object obj = s.clone();Student s2 = (Student)obj;System.out.println("------------");System.out.println(s.getName()+"----"+s.getAge());System.out.println(s2.getName()+"---"+s2.getAge());//Previous practices Student s3 = s;System.out.println(s3.getName()+"---"+s3.getAge());System.out.println("--------------");//In fact, there are differences s3.setName("Liu Yi");s3.setAge(30);System.out.println(s.getName()+"-- -"+s.getAge());System.out.println(s2.getName()+"---"+s2.getAge());System.out.println(s3.getName()+"---"+s3.getAge());System.out.println("----------");//Change the cloned one? s2.setName("Clone changed name");s2.setAge(66);System.out.println(s.getName()+"---"+s.getAge());System.out.println(s2.getName()+"---"+s2.getAge());System.out.println(s3.getName()+"---"+s3.getAge());System.out.println(s3.getName()+"---"+s3.getAge());//In other words, assign an object's reference to a reference. When the reference is sent to something, the object pointed to by the same reference will change, but the cloned object will not change. // Change some of its properties to the cloned object, it will not affect the change of the value of other objects}}The above is all the content that the editor brings to you about the difference between cloning close() and assignment reference in Java. I hope it will be helpful to everyone and support Wulin.com more~