Java Study Notes-you may not know the knowledge about object replication and reference.
1. private, protected, and static cannot be used to modify interfaces.
2. When processing basic data types (such as int, char, and double), java uses the value-based transmission method for execution, and Other types are passed by reference. In addition to the reference transfer during function calls, an object also uses reference transfer when "=" is assigned a value.
3. How to copy objects in java instead of references. Example:
class Obj implements Cloneable{private int a=0;public int getA(){return a;}public void setA(int b){this.a=b;}public void changedA(){this.a=1;}public Object clone(){Object o=null;try{0=(Obj)super.clone();}catch(CloneNotSupportedException e){e.printStatckTrace();}return o;}}public class TestRef{public static void main(String args[]){Obj a= new Obj();Obj b=(Obj)a.clone();b.changedA();System.out.println("a:"+a.getA());System.out.println("b:"+b.getA());}}
The program running result is:
A: 0
B: 1
Therefore, to assign a value to a class, you must implement the Cloneable interface. Steps:
(1) clone must first inherit the Cloneable interface. The Cloneable interface is essentially an identity interface (there is no method, that is, an empty Interface)
(2) rewrite the clone () method of the Object class in the class.
(3) Call super. clone () in the clone () method ().
(4) point the reference of the shallow copy to the new clone of the prototype object.
When a class contains complex object attributes other than basic data, the clone () method must be rewritten to perform deep replication on attributes of non-basic types. Example:
Class Obj implements Cloneable {private Date birthday = new Date (); private int a = 0; public int getA () {return a;} public void setA (int B) {this. a = B;} public void changedA () {this. a = 1 ;}public Date getDate () {return birthday () ;}public void setDate (Date date) {this. birthday = date;} public void changeDate () {this. birthday. setMonth (4);} public Object clone () {Object o = null; try {0 = (Obj) super. clone ();} catch (CloneNotSupportedException e) {e. printStatckTrace ();} // implements deep replication o. birthday = (Date) this. getDate (). clone (); return o ;}} public class TestRef {public static void main (String args []) {Obj a = new Obj (); Obj B = (Obj). clone (); B. changedA (); System. out. println ("a:" +. getA (); System. out. println ("B:" + B. getA ());}}
Differences between shallow replication and deep replication:
Shallow copy: all variables of the Copied object contain the same value as the original object, and all references of other objects still point to the same value of the original object, all references to other objects still point to the original objects.
Deep replication: all the variables of the Copied object contain the same value as the original object and get out the variables that reference other objects. The variables that reference other objects will point to the new objects to be copied, instead of the original objects that have been referenced.