Here is a story like this. First, let's look at the Members Xie Yifeng, Zhang baizhi and his two sons. If the entity does not implement the cloneable method and does not call the clone method, two different results will be obtained.
First, check the result:
Error result 1: (The cloneable method of the object is not implemented)
I'm the first child. My name is Lucas. My dad is Nicholas Tse. My mom is Cecilia Cheung. I'm the second child. My name is Quintus. My dad is Nicholas Tse. My mom is Cecilia Cheung. One day, hong Kong media reporter reported that the father of the first child was Edison Chan. Lucas's father was Nicholas Tse. Quintus's father was Nicholas Tse.
Expected result 2: (implement the cloneable method of the object)
I'm the first child. My name is Lucas. My dad is Nicholas Tse. My mom is Cecilia Cheung. I'm the second child. My name is Quintus. My dad is Nicholas Tse. My mom is Cecilia Cheung. One day, hong Kong media reporter reported that the father of the first child was Edison Chan. Lucas's father was Edison Chan. Quintus's father was Nicholas Tse.
Code Implementation
Coding:
Entity:
package com.example.model;public class Student implements Cloneable { public String Name; public String Father; public String Monther; @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub Student o = null; try { o = (Student) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return o; }}
Coding:
List <student> alllist = new arraylist <student> (); Student chirld = new student (); Student Lucas = new student (); Student Quintus = new student (); chirld. father = "Xie Yunfeng"; chirld. monther = "Cecilia Cheung"; Quintus = chirld; Lucas. name = "Lucas"; log. D ("1", "I am the first child" + "my name is" + Lucas. name + "My dad is" + Lucas. father + "my mom is" + Lucas. monther); Quintus. name = "Quintus"; log. D ("2", "I am the second child" + "my name is" + Quintus. name + "My dad is" + Quintus. father + "my mom is" + Quintus. monther); log. D ("3", "suddenly one day, the Hong Kong media reporter reported that the father of the first child was Edison Chen"); // The for (INT I = 0; I <2; I ++) {Student name = new student (); name = chirld; if (I = 1) Name. father = "Xie Yunfeng"; else name. father = "Edison Chen"; alllist. add (name) ;}// the log is cleared. D ("they finally say", ""); For (INT I = 0; I <alllist. size (); I ++) {string name = ""; if (I = 0) {name = "Lucas" ;}else {name = "Quintus ";} student STR = alllist. get (I); log. D ("they said", name + "Dad is" + Str. father );}
The main error is in the cycle of the leak process. Although two objects are newly added, they point to the same memory area. At this time, you must call the clone method of cloneable to clone an object to change the value.
Coding
// For (INT I = 0; I <2; I ++) {Student name = new student (); try {name = (student) chirld. clone ();} catch (clonenotsupportedexception e) {// todo auto-generated Catch Block E. printstacktrace ();} if (I = 1) Name. father = "Xie Yunfeng"; else name. father = "Edison Chen"; alllist. add (name );}
A reference object Problem