Dark-shade Cloning of Java objects

Source: Internet
Author: User
Tags dname

Package clonetest;

Class Dog implements cloneable{
Private String dname;
Private Integer dage;
Public Dog Clone () throws clonenotsupportedexception{
Dog cloned= (dog) Super.clone ();
return cloned;

}

Public String Getdname () {
return dname;
}

public void Setdname (String dname) {
This.dname = dname;
}

Public Integer Getdage () {
return dage;
}

public void Setdage (Integer dage) {
This.dage = Dage;
}

@Override
Public String toString () {
Return "Dog [dname=" + Dname + ", dage=" + Dage + "]";
}

Public Dog (String dname, Integer dage) {
Super ();
This.dname = dname;
This.dage = Dage;
}

}

Class Person implements Cloneable {
private String name;
Private Integer age;
Private dog Dog;

Public person (String name, Integer age, dog dog) {
THIS.name = name;
This.age = age;
This.dog = dog;
}

Public person Clone () throws Clonenotsupportedexception {
Person cloned = (person) super.clone ();
return cloned;

}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public Integer Getage () {
return age;
}

public void Setage (Integer age) {
This.age = age;
}

@Override
Public String toString () {
return "person [name=" + name + ", age=" + Age + ", dog=" + Dog + "]";
}

Public Dog Getdog () {
return dog;
}

public void Setdog (dog dog) {
This.dog = dog;
}

}

public class Clonetest {
public static void Main (string[] args) throws Clonenotsupportedexception {
Dog dog1 = new Dog ("Young Dog", 5);
person P1 = new Person ("Wantao", dog1);
System.out.println ("original object" + p1.getname () + "his Dog for"
+ P1.getdog (). Getdname ());
System.out.println ("Cloning for the first time:");
person P2 = P1.clone ();
System.out.println ("Clone object" + p2.getname () + "Clone Dog"
+ P2.getdog (). Getdname ());
System.out.println (P1 = = P2? "Clone object is the same object as the original object": "Clone object and original object are different objects");
System.out.println (P1.getdog () = = P2.getdog ()? "Clone the dog of the object with the same dog as the original object"
: "Clone the dog of the object with the original object of the dog as a different dog");
System.out.println ("Cloning for the second time");
Person P3=p1.clone ();
P3.setdog (P1.getdog (). Clone ());
System.out.println ("Clone object" + p3.getname () + "Clone Dog"
+ P3.getdog (). Getdname ());
System.out.println (P1 = = P3? "Clone object is the same object as the original object": "Clone object and original object are different objects");
System.out.println (P1.getdog () = = P3.getdog ()? "Clone the dog of the object with the same dog as the original object"
: "Clone the dog of the object with the original object of the dog as a different dog");

}

}

Dark-shade Cloning of Java objects

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.