The difference between a clone close () and an assignment reference in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.