Personal memo-light clone and deep clone

Source: Internet
Author: User

Shallow copy:

Package clonemethod; public class shallowcopy {public static void main (string [] ARGs) throws clonenotsupportedexception {student s = new student ("S", 12 ); teacher T = new teacher ("T", 40, S); teacher Ct = (teacher) T. clone (); system. out. println (CT); system. out. println ("-------------------------------"); // the property of the copied bean cannot change the Copied object T. setname ("Copy t"); system. out. println (t); system. out. println (CT); system. out. println ("-----------------------------"); // the attributes of the copied bean may change the copying object. Ct. setname ("Copy t"); system. out. println (CT); system. out. println ("-----------------------------"); // Changes the attributes of another object referenced by the object (yes) changes the reference attributes of the Copied object (the difference between shallow replication and deep replication) s. setname ("Copy s"); system. out. println (CT) ;}} class student {int age; string name; Public student (string name, int age) {This. age = age; this. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}@ overridepublic string tostring () {return "Student name:" + name + ", age:" + age ;}} // copy must inherit cloneableclass teacher implements cloneable {int age; string name; student s; public teacher (string name, int age, student s) {This. age = age; this. name = Name; this. S = s;} public int getage () {return age;} public void setage (INT age) {This. age = age;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public student gets () {return s;} public void sets (student s) {This. S = s ;}@ overridepublic string tostring () {return "teacher name:" + name + ", age:" + age + "\ ninclude: \ n" + S ;} // change the protected type to the public type @ overridepublic object clone () throws clonenotsupportedexception {// super. clone () can only implement shallow copy object OBJ = super. clone (); Return OBJ ;}}

Running result:

Teacher name: T, age: 40
Include:
Student name: S, age: 12
-------------------------------
Teacher name: Copy T, age: 40
Include:
Student name: S, age: 12
Teacher name: T, age: 40
Include:
Student name: S, age: 12
-------------------------------
Teacher name: Copy T, age: 40
Include:
Student name: S, age: 12
-------------------------------
Teacher name: Copy T, age: 40
Include:
Student name: Copy S, age: 12

Deep replication:

Package CloneMethod; public class DeepCopy {public static void main (String [] args) throws CloneNotSupportedException {Student s = new Student ("s", 12 ); teacher t = new Teacher ("t", 40, s); Teacher ct = (Teacher) t. clone (); System. out. println (ct); System. out. println ("-------------------------------"); // the property of the copied bean cannot change the Copied object t. setName ("copy t"); System. out. println (t); System. out. println (ct); System. out. println ("-----------------------------"); // the attributes of the copied bean may change the copying object. ct. setName ("copy t"); System. out. println (ct); System. out. println ("-----------------------------"); // Changes the attributes of another object referenced by the object (not allowed) changes the reference attributes of the Copied object (the difference between shallow replication and deep replication) s. setName ("copy s"); System. out. println (ct) ;}} class Student implements Cloneable {int age; String name; public Student (String name, int age) {this. age = age; this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic String toString () {return "student name:" + name + ", age:" + age ;}@ Overridepublic Object clone () throws CloneNotSupportedException {return super. clone () ;}// copy must inherit Cloneableclass Teacher implements Cloneable {int age; String name; Student s; public Teacher (String name, int age, Student s) {this. age = age; this. name = name; this. s = s;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Student getS () {return s;} public void setS (Student s) {this. s = s ;}@ Overridepublic String toString () {return "teacher name:" + name + ", age:" + age + "\ ninclude: \ n" + s ;} // change the protected type to the public type @ Overridepublic Object clone () throws CloneNotSupportedException {// super. clone () can only implement shallow copy Object obj = super. clone (); // This method implements deep replication of Teacher t = (Teacher) obj; t. setS (Student) t. getS (). clone (); return obj ;}}

Running result:

Teacher name: T, age: 40
Include:
Student name: S, age: 12
-------------------------------
Teacher name: Copy T, age: 40
Include:
Student name: s, age: 12
Teacher name: t, age: 40
Include:
Student name: s, age: 12
-------------------------------
Teacher name: copy t, age: 40
Include:
Student name: s, age: 12
-------------------------------
Teacher name: copy t, age: 40
Include:
Student name: s, age: 12

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.