Java deep Copy, shallow copy __java

Source: Internet
Author: User

Shallow Copy Code instance:

public class CloneTest1
{
public static void Main (string[] args) throws Clonenotsupportedexception
{
Student Student = new Student ();

Student.setage (20);
Student.setname ("Zhangsan");

Student Student2 = (Student) student.clone ();

System.out.println (Student2.getage ());
System.out.println (Student2.getname ());
System.out.println ("---------------");

Student2.setname ("Lisi");

System.out.println (Student.getname ());
System.out.println (Student2.getname ());
}
}


Class Student implements Cloneable
{
private int age;

private String 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;
}

@Override
Public Object Clone () throws Clonenotsupportedexception
{
Object object = Super.clone (); Shallow copy

return object;
}
}

Small instances of deep replication



public class CloneTest2
{
public static void Main (string[] args) throws Clonenotsupportedexception
{
Teacher Teacher = new Teacher ();

Teacher.setage (40);
Teacher.setname ("Teacher Zhang");

Student2 S1 = new Student2 ();

S1.setage (20);
S1.setname ("Zhangsan");
S1.setteacher (teacher);

Student2 s2 = (Student2) s1.clone ();

System.out.println (S2.getname ());
System.out.println (S2.getage ());

Teacher.setname ("Teacher Li");

System.out.println (S2.getteacher (). GetName ());
System.out.println (S2.getteacher (). Getage ());

}
}


Class Teacher implements Cloneable
{
private int age;

private String 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;
}

@Override
Public Object Clone () throws Clonenotsupportedexception
{
Object object = Super.clone ();

Return (Teacher) object;
}


}




Class Student2 implements Cloneable
{
private int age;

private String name;

Private Teacher Teacher;


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 Teacher Getteacher ()
{
return teacher;
}


public void Setteacher (Teacher Teacher)
{
This.teacher = teacher;
}


@Override
Public Object Clone () throws Clonenotsupportedexception
{
Student2 Student2 = (Student2) super.clone ();

Student2.setteacher ((Teacher) Student2.getteacher (). Clone ());


return student2;
}


}



Deep replication of objects can also be achieved using serialization





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.