一个类如果想重写 Object 的 clone 方法,则必须实现 Cloneable 接口,否则调用 clone 方法时将会抛出 CloneNotSupportException 异常
/**
* Author: heatdeath
* Date: 2018/7/7
* Desc:
*/
public class CloneDemo {
static class CloneClass {
public String str;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public static void main(String[] args) throws Exception {
CloneClass cloneClass1 = new CloneClass();
System.out.println(cloneClass1);
cloneClass1.str = "hello world";
CloneClass cloneClass2 = (CloneClass) cloneClass1.clone();
System.out.println(cloneClass2);
System.out.println(cloneClass2.str);
}
}
com.heatdeath.test_package.CloneDemo$CloneClass@4554617c
Exception in thread "main" java.lang.CloneNotSupportedException: com.heatdeath.test_package.CloneDemo$CloneClass
at java.lang.Object.clone(Native Method)
at com.heatdeath.test_package.CloneDemo$CloneClass.clone(CloneDemo.java:14)
at com.heatdeath.test_package.CloneDemo.main(CloneDemo.java:23)
正确的方式是,实现 Cloneable 接口,再重写 clone 方法(通常直接调用 super.clone() 方法,即 Object 类中受保护的 native clone 方法即可)
public class CloneDemo {
static class CloneClass implements Cloneable {
public String str;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public static void main(String[] args) throws Exception {
CloneClass cloneClass1 = new CloneClass();
System.out.println(cloneClass1);
cloneClass1.str = "hello world";
CloneClass cloneClass2 = (CloneClass) cloneClass1.clone();
System.out.println(cloneClass2);
System.out.println(cloneClass2.str);
}
}
com.heatdeath.test_package.CloneDemo$CloneClass@4554617c
com.heatdeath.test_package.CloneDemo$CloneClass@74a14482
hello world
Process finished with exit code 0
再捎带着复习一下浅拷贝和深拷贝好了
浅拷贝 demo
public class CloneDemo {
static class CloneClass implements Cloneable {
public String str;
public Hello helloRef;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
static class Hello {
}
public static void main(String[] args) throws Exception {
CloneClass cloneClass1 = new CloneClass();
System.out.println(cloneClass1);
cloneClass1.str = "hello world";
cloneClass1.helloRef = new Hello();
System.out.println(cloneClass1.helloRef);
CloneClass cloneClass2 = (CloneClass) cloneClass1.clone();
System.out.println(cloneClass2);
System.out.println(cloneClass2.str);
System.out.println(cloneClass2.helloRef);
}
}
深拷贝
public class CloneDemo {
static class CloneClass implements Cloneable {
public String str;
public Hello helloRef;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
((CloneClass) obj).helloRef = (Hello) helloRef.clone();
return obj;
}
}
static class Hello implements Cloneable {
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public static void main(String[] args) throws Exception {
CloneClass cloneClass1 = new CloneClass();
System.out.println(cloneClass1);
cloneClass1.str = "hello world";
cloneClass1.helloRef = new Hello();
System.out.println(cloneClass1.helloRef);
CloneClass cloneClass2 = (CloneClass) cloneClass1.clone();
System.out.println(cloneClass2);
System.out.println(cloneClass2.str);
System.out.println(cloneClass2.helloRef);
}
}