1.Cloneable顾名思义就是复制,克隆。接下来我们将要使用Cloneable进行Java中克隆。(浅克隆)
实现一个简单的克隆如下:
import java.util.Arrays;
public class Test2 {
public static void main(String[] args)throws CloneNotSupportedException {
Person person1=new Person(18,"niechentao");
Person person2=(Person)person1.clone();
System.out.println(person1);
System.out.println(person2);;
}
}
package demo2;
public class Person implements Cloneable{
public int age;
public String name;
public Person(int age,String name){
this.age=age;
this.name=name;
}
public String toString(){
return "["+this.name+":"+this.age+"]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52174:D:\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\niech\IdeaProjects\untitled15\out\production\untitled15 demo2.Test2
[niechentao:18]
[niechentao:18]
Process finished with exit code 0
要注意几步:
1.要在person中重写clone方法。
2.要注意在person中接入Cloneable接口。
3.要注意异常的排除。
4.记得要向下转型。
2.如何实现深度克隆
import java.util.Arrays;
public class Test2 {
public static void main(String[] args)throws CloneNotSupportedException {
Person person1=new Person(18,"niechentao");
Person person2=(Person)person1.clone();
person1.m.money=10;
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
}
class Money{
public int money;
}
public class Person implements Cloneable{
public int age;
public String name;
public Money m=new Money();
public Person(int age,String name){
this.age=age;
this.name=name;
}
public String toString(){
return "["+this.name+":"+this.age+"]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52374:D:\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\niech\IdeaProjects\untitled15\out\production\untitled15 demo2.Test2
10
10
Process finished with exit code 0
如上式代码可见:person1.m.money改变person2.m.money与其发生一样的改变。我们希望person2.m.money不随着person1.m.money改变而改变。
1.我们需要money也实现clone方法。
2.在person中的clone方法中先要克隆person类中的数,再通过引用克隆money的类。
package demo2;
class Money implements Cloneable{
public int m=99;
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Person implements Cloneable{
public int age;
public String name;
public Money money=new Money();
public Person(int age,String name){
this.age=age;
this.name=name;
}
public String toString(){
return "["+this.name+":"+this.age+"]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
Person tmp=(Person)super.clone();
tmp.money=(Money)this.money.clone();
return tmp;
}
}
package demo2;
import java.util.Arrays;
public class Test2 {
public static void main(String[] args)throws CloneNotSupportedException {
Person person1=new Person(18,"niechentao");
Person person2=(Person)person1.clone();
person1.money.m=0;
System.out.println(person1.money.m);
System.out.println(person2.money.m);
}
}