Example parsing of clone () usages of Java

Source: Internet
Author: User

1. Background

One of the things that bothers you when you write a program in Java is that if you assign an object A to another object, B, then you change the value of a to be worth the time, and the values of B also correspond to the changes. If we want to simply get the condition of a at that moment to B, we need to use the Clone method.

For example, the following code:

public class Main {public static void main (string[] args) {//TODO auto-generated Method stub             Node n=new node ();       Node n1=n;             n.a=5;            System.out.print ("" +n1.a);}} public class Node {    int a=1;    }

The output is 5,

The following is the use of clone to achieve the above situation output is 1.


2. Code

The cloning implementation takes a few steps:

Overrides the base class's Clone () method in a derived class and declares it to be public.

In the Clone () method of the derived class, call Super.clone ().

Implement the Cloneable interface in a derived class. The Cloneable interface does not have any abstract methods, such that it becomes an identity interface. Implement this interface just to tell the compiler that this object can be cloned.

First we override the Clone () method of the object class in the derived class and declare it as public. Then we call the Super.clone () method, which throws an exception (for this exception you can view the Java Help document yourself), so you must use Try......catch ... The statement captures, and then returns this object. Here you need to explain that the Clone () method returns an object type, so you need to force the type conversion

public class Main {public static void main (string[] args) {//TODO auto-generated Method stub             Node n=new node ();       Node n1= (node) n.clone ();             n.a=5;            System.out.print ("" +n1.a);}} public class Node implements cloneable{    int a=1;    Public Object Clone () {      Node n=null;        try{          n= (Node) Super.clone ();        }        catch (Exception e) {        e.printstacktrace ();        }       return n;    }}

The output result is 1;

PS: Add a question

15. Can I create an object without a constructor function ()

A Yes B no

Answer: A

Parsing: Java creates objects in several ways (important):

(1) Create objects with the new statement, which is the most common way to create objects.
(2) using the reflection means, call the Java.lang.Class or Java.lang.reflect.Constructor class newinstance () instance method.
(3) Call the object's clone () method.
(4) The ReadObject () method of the Java.io.ObjectInputStream object is invoked by means of deserialization.

(1) and (2) will explicitly call the constructor, (3) is a photocopy of an existing object in memory, so the constructor is not called, (4) is the object that restores the class from the file, and the constructor is not called.



reference: "1" http://blog.csdn.net/mengxiangyue/article/details/6818611"2" http://blog.csdn.net/lanxuezaipiao/article/details/16753743

/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/

Example parsing of clone () usages of Java

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.