Realized it by itself:
Copy Code code as follows:
public class A implements cloneable {
Public String str[];
A () {
str = new STRING[2];
}
Public Object Clone () {
A o = null;
try {
o = (A) super.clone ();
catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
O.STR = new String[2];
return o;
}
}
void Run () throws Exception {
A a1 = new A (), a2 = new A ();
A1.str[0] = "a"; A1.STR[1] = "B";
A2 = (A) a1.clone ();
A2.str[0] = "C"; A2.STR[1] = "D";
System.out.println (A1.str[0] + "" + a2.str[0]);
}
Results:
A C
1.
Copy Code code as follows:
public class A implements cloneable {
public String name;
Public Object Clone () {
A o = null;
try {
o = (A) super.clone ();
catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
return o;
}
}
2.
Copy Code code as follows:
public class A implements cloneable {
Public String name[];
Public A () {
Name=new string[2];
}
Public Object Clone () {
A o = null;
try {
o = (A) super.clone ();
catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
return o;
}
}
3.
Copy Code code as follows:
public class A implements cloneable {
Public String name[];
Public vector<b> Clab;
Public A () {
Name=new string[2];
Clab=new vector<b> ();
}
Public Object Clone () {
A o = null;
try {
o = (A) super.clone ();
catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
O.name=new string[2];//Depth Clone
O.clab=new vector<b> ()//clone to the end
for (int i=0;i<clab.size (); i++) {
b temp= (b) clab.get (i). Clone ();//Of course class B implements the corresponding Clone method
O.clab.add (temp);
}
return o;
}
}