How to use Clone

Source: Internet
Author: User

Package cn.hncu.day7.clone.v1;
Cloning the routine:
1th step: Rewrite the Clone () method of the user class for outside invocation. Because the outside class cannot directly invoke the Clone () method in the parent class of the user class--protected
2nd step: Let the user class implement the Cloneable interface, or clone will throw an exception
3rd Step: Call in other classes that require cloning: User U2 = (user) User.clone ()

For a class object to be able to be clone, the class must implement the Cloneable interface, otherwise the clone throws an exception
public class User implements cloneable{
private String name;
private int age;

Public User (String name, int age) {
Super ();
THIS.name = name;
This.age = age;
}

@Override
Public Object Clone () throws Clonenotsupportedexception {
return Super.clone ();
}

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

@Override
Public String toString () {
Return "User [name=" + name + ", age=" + Age + "]";
}

}

--------------------------------------------------

Package cn.hncu.day7.clone.v1;

public class Clonedemo {

public static void Main (string[] args) {
User user = New User ("Jack", 23);
SYSTEM.OUT.PRINTLN (user);

try {
User User2 = (user) user.clone ();//The Clone () method must be the user class's own and cannot invoke the Clone () method in its parent class, object, because the permission is protected--only the user object itself gram ( Clone that can be called in the user Class)
User2.setage (100);
System.out.println (User2);
SYSTEM.OUT.PRINTLN (user);
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
}
}

-------------------------------------------------------------

Package cn.hncu.day7.clone.v2;

public class User implements cloneable{
private String name;
private int age;

Public User (String name, int age) {
Super ();
THIS.name = name;
This.age = age;
}

@Override
Public Object Clone () throws Clonenotsupportedexception {
return Super.clone ();
}

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

@Override
Public String toString () {
Return "User [name=" + name + ", age=" + Age + "]";
}

}

----------------------------------------------------------------------

Package cn.hncu.day7.clone.v2;
The V2 version is problematic---shallow copy problem
※※ Note that simple clone is a shallow copy, and if the member variables in the object are reference types, then such variables are bundled-clones and prototype objects

Public class account implements cloneable{
Private user user;//account corresponding to users
Private double balance;//Balance

Public account (user user, double balance) {
Super ();
This.user = user;
This.balance = balance;
}

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

Public double getbalance () {
return balance;
}

public void setbalance (double balance) {
This.balance = balance;
}

@Override
Public Object Clone () throws Clonenotsupportedexception {
return Super.clone ();
}

@Override
Public String toString () {
Return "account [user=" + user + ", balance=" + Balance + "]";
}

}

-------------------------------------------------------------------------------------------

Package cn.hncu.day7.clone.v2;

public class Clonedemo {

public static void Main (string[] args) {
User user = New User ("Jack", 23);
Account Account = new account (user, 100);
SYSTEM.OUT.PRINTLN (account);
try {
Account Account2 = (account) Account.clone ();
Account2.setbalance (200);
Account2.getuser (). SetName ("Tom");
System.out.println (Account2);
SYSTEM.OUT.PRINTLN (account);

} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
}
}

-----------------------------------------------------------------------

Package cn.hncu.day7.clone.v3;

public class User implements cloneable{
private String name;
private int age;

Public User (String name, int age) {
Super ();
THIS.name = name;
This.age = age;
}

@Override
Public Object Clone () throws Clonenotsupportedexception {
return Super.clone ();
}

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

@Override
Public String toString () {
Return "User [name=" + name + ", age=" + Age + "]";
}

}

---------------------------------

Package cn.hncu.day7.clone.v3;
In contrast to the V2 version, our solution to the shallow copy problem is to modify this class.
※※ if a variable in a class is primitive (the base data type or the final type of the reference type), the overridden clone method calls the Super.clone () directly on the OK
※※ if a class contains reference variables, the Clone method is as---this class writes a reference to the shallow copy to the new clone of the prototype object.

Public class account implements cloneable{
Private user user;//account corresponding to users
Private double balance;//Balance

Public account (user user, double balance) {
Super ();
This.user = user;
This.balance = balance;
}

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

Public double getbalance () {
return balance;
}
public void setbalance (double balance) {
This.balance = balance;
}
@Override//Deep clone
Public Object Clone () throws Clonenotsupportedexception {
Account A = (account) Super.clone ();

if (user!=null) {
※※ the reference of the shallow copy to the new clone of the prototype object.
A.user = (user) user.clone ();
}
return A;
}

@Override
Public String toString () {
Return "account [user=" + user + ", balance=" + Balance + "]";
}

}

-------------------------------------------

Package cn.hncu.day7.clone.v3;

public class Clonedemo {

public static void Main (string[] args) {
User user = New User ("Jack", 23);
Account Account = new account (user, 100);
SYSTEM.OUT.PRINTLN (account);
try {
Account Account2 = (account) Account.clone ();
Account2.setbalance (200);
Account2.getuser (). SetName ("Tom");
System.out.println (Account2);
SYSTEM.OUT.PRINTLN (account);

} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
}
}

How to use Clone

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.