Student Class Student:
Package cloning clone;/* to clone must implement this excuse: cloneable, to mark this object can clone Cloneable: This class implements the Cloneable interface to indicate Object.clone () Method can legitimately replicate the class instance by field. This interface is a markup interface, which tells us that the class implementing the interface can implement the object's replication. */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.) {this.age = age;} Overriding the Clone method child column can call @overrideprotected Object clone () throws Clonenotsupportedexception {return Super.clone ();}}
Test Clone Student Class:
Package clone clone;/* *protected void Finalize (): This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object. Used for garbage collection, but when recycling is uncertain. *protected Object Clone (): Creates and returns a copy of this object. *a: Override this method * * Cloneable: This class implements the Cloneable interface to indicate that the Object.clone () method can legitimately replicate the class instance by field. * This interface is a tagging interface, which tells us that the class implementing the interface can implement the object's replication. */public class Studentclone {public static void main (string[] args) throws Clonenotsupportedexception {//Create student object Student S = New Student (); S.setname ("Brigitte"); S.setage (27);//Clone student 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 practice student S3 = s; System.out.println (S3.getname () + "---" +s3.getage ()); System.out.println ("---------");//Actually there is a difference between s3.setname ("Elina"); 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 clone? S2.setname ("Name of clone Change"); S2.setage (66); SysteM.out.println (S.getname () + "---" +s.getage ()); System.out.println (S2.getname () + "---" +s2.getage ()); System.out.println (S3.getname () + "---" +s3.getage ()), which means that a reference to an object is assigned to a reference, and the object that the same reference points to changes when the reference is made, But the clone will not change,//change the cloned object to some of his properties, so it will not affect the value of other objects Change}}
The difference between a clone close () and an assignment reference in Java