Student Class Student:
Cloning clone clone package;
/* To clone must implement this excuse: cloneable, to mark this object can be cloned
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 markup interface that tells us that the class that implements the interface can replicate the object.
*
/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;
}
Overriding the Clone method child column to invoke
@Override
protected 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.
For garbage collection, but when recycling is not determined.
* Protected Object Clone (): Creates and returns a copy of this object.
* A: Rewrite the 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 markup interface that tells us that the class that implements the interface can replicate the object. */public class Studentclone {public static void main (string[] args) throws Clonenotsupportedexception {//Create student Object St
udent 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 ());
The previous practice Student S3 = s;
System.out.println (S3.getname () + "---" +s3.getage ());
System.out.println ("---------");
In fact there is a difference between the 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 Clones?
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 assigning a reference to an object to a reference, when this reference makes a change, the object that the same reference points to will change, but the clone does not change,//The cloned object changes some of his properties, so it does not affect the change of the value of other objects}}
The above is a small series for you to bring a brief discussion of cloning close () and assignment reference in Java all the contents of the difference, I hope to help you, a lot of support cloud Habitat Community ~