Shallow cloning of Java objects

Source: Internet
Author: User

Shallow clone of/** * java object  *  when cloning an object, if the member variable of the object is the base type, a shallow clone is used to complete it.  *  if the object's member variable includes a mutable reference type, deep cloning is required  *  if the reference type is immutable, such as the string class, you do not have to deep clone  *  when you need to clone an object, you need to use clone () method, which replicates the various domains in the class, *  if there is a problem with the domain of the reference type, it becomes a shallow clone.  *  the class that provides the cloning function needs to implement the Cloneable interface, otherwise it throws an exception  */public class Address{private String  State;private string province;private string city;public address (String state,  string province, string city)  {this.state = state;this.province =  province;this.city = city;} Public string getstate ()  {return state;} Public void setstate (String state)  {this.state = state;} Public string getprovince ()  {return province;} Public void setprovince (string province)  {this.province = province;} Public string getcity ()  {return city;} Public void sEtcity (string city)  {this.city = city;} @Overridepublic  string tostring ()  {return  "address [state="  + state +   ",  province="  + province +  ",  city=" + city +  "]";}
Public class employee implements cloneable {private string name;private  int age;//New Address Object Private address address;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;} Public employee (string name, int age)  {this.name = name;this.age =  age;} Public employee (string name, int age, address address)  {this.name =  name;this.age = age;this.address = address;} Public address getaddress ()  {return address;} Public void setaddress (address address)  {this.address = address;} Public employee ()  {} @Overridepublic  string tostring ()  {return  "employee  [Name= " + name + ",  age= " + age + ",  address= "+ address  +  "]";} Implement the @overrideprotected employee clone ()  throws clonenotsupportedexception {of light cloning employee emp = null;try {emp =  (Employee)  super.clone ();}  catch  (clonenotsupportedexception e)  {e.printstacktrace ();} Return emp;}}

Test:

public class Testemployeeandaddress {public static void main (string[] args) throws Clonenotsupportedexception { System.out.println ("Before Cloning:"); Address address = new address ("China", "Jilin", "Changchun"); Employee EMP1 = new Employee ("Zhang xx", 30,address); SYSTEM.OUT.PRINTLN ("Employee 1 Information:" +EMP1); System.out.println ("After cloning:"); Employee emp2 = Emp1.clone (); Emp2.getaddress (). SetState ("China"); Emp2.getaddress (). Setprovince ("Sichuan"); emp2.getaddress (). Setcity ("Chengdu"); Emp2.setname ("Li xx"); Emp2.setage (24); SYSTEM.OUT.PRINTLN ("Employee 1 Information:" +EMP1); System.out.println ("Employee 2 Information:" +EMP2);}}

The results are as follows:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/8E/D7/wKioL1jMoyrjvs9ZAACRMGEbWPk435.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jmoyrjvs9zaacrmgebwpk435.jpg "/>

This article is from the "It Rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.com/3174958/1907844

Shallow cloning of Java objects

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.