Suddenly saw the object cloning this thing, and looked at it is the object class in the native method, feeling can improve the operation efficiency of the program. To study this thing.
Part I. Briefly describe the cloning of an object
Usually when we use the object, as long as we get the reference of this object, the referrer can modify the properties of the object arbitrarily (first the object has the external Getter&setter method), sometimes we want the caller to get a copy of the object (that is, an object of exactly the same content, But there are two such objects in memory)? Is there any way to solve this?
1. Re-new an object and reassign each property of the object. (Low efficiency)
2. Clone an object. (High efficiency)
Part Ii. conditions for implementing an object clone
1. The object to be cloned must implement the Java.lang.Cloneable interface
2. Rewrite protected native object Clone () throws Clonenotsupportedexception in the object class; Method
Part III. Simple implementation Code
This is an object class public class person implements cloneable{private string naem; Private string gender;private string address;public person () {}public Person (string naem, string gender, string address) {this.naem = naem; this.gender = gender;this.address = address;} Omit getter setter method @overridepublic string tostring () {return " Person [naem= " + naem + ", gender= " + gender + ", Address= " + address + "] ";} Rewrite clone method @overridepublic object clone () throws Clonenotsupportedexception {return super.clone ();}} This is the test class Public class testcase {public static void main (String[] args) throws exception {person p1 = new person ("Tom", "Male", " Beijing "); person p2 = (person) p1.clone (); System.out.println (p1);// person [naem=tom, gender=male, address=beijing] System.out.println (p2);// person [naem=tom, gender=male, address=beijing] System.out.println (P1 == P2);// false}}
A simple clone is implemented, two objects are different objects, but the property values in the object are the same.
Partⅳ. Cloning interpretation
The Cloneable interface is implemented in the person class, and then the Clone () method in the Object class is overridden.
We go to see Java.lang.Cloneable Source code discovery, this interface is empty, no attributes, no method. Why? What is the meaning of this interface? is to mark if this class can be cloned!
We want to clone an object, but there is no corresponding cloning method in the Java.lang.Cloneable class, so how do we implement this cloning operation? This relies on the parent class of all classes: the Object class.
Take a look at the Clone () method in the Object class
Protected native Object clone () throws Clonenotsupportedexception;
is a protected local method, is a local method, we do not have to control how to implement, just call it, and since it is protected, then we can use it casually (the permissions of the protected method are subclasses and classes under the same package can be called), because all classes are subclasses of Object .
Performance:
There is no denying that the native method is fast.
This article is from the "focus on Java,linux Technology" blog, please be sure to keep this source http://wuqinglong.blog.51cto.com/9087037/1719833
About the cloning of objects Cloneable