Cloneable 接口为何不声明clone 方法?
刚接触 clone 的人会觉得奇怪,当克隆一个对象时,除了声明 public Object clone() 方法之外,还需要实现 Cloneable 接口。而Cloneable 是一个没有声明任何方法的空接口。
既然如此,为何不在Cloneable 接口中声明clone 方法:
public Object clone() throws CloneNotSupportedException;
//自定义接口public interface MyCloneable extends Cloneable{ public Object clone() throws CloneNotSupportedException;}
public class TestClone implements MyCloneable {int i;TestClone(int i) {this.i = i;}@Overridepublic Object clone() throws CloneNotSupportedException {return super.clone();}public static void main(String[] args) {TestClone t1 = new TestClone(10);TestClone t2 = (TestClone) cloneObject(t1);t1.i = 88;System.out.println(t1.i); //88System.out.println(t2.i); //10}//将所有可克隆对象统一对待,调用其 clone 方法。public static Object cloneObject(MyCloneable mc){try {return mc.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return null;}}
package java.lang;public class Object{//...protected native Object clone() throws CloneNotSupportedException;//...}
//没有声明clone 方法的类class NotDeclareCloneMethod implements Cloneable {int i;public String toString() {return "" + i;}}public class Main {public static void main(String[] args) throws NoSuchMethodException,SecurityException, IllegalAccessException,IllegalArgumentException, InvocationTargetException {NotDeclareCloneMethod n = new NotDeclareCloneMethod();n.i = 77;java.lang.reflect.Method m = Object.class.getDeclaredMethod("clone");//得到clone 方法m.setAccessible(true); //获得访问权限Object o = m.invoke(n); //调用n.i = 99;System.out.println(n); //99System.out.println(o); //77}}
final class NotCloneable1{//不被继承,也无法被克隆}class NotCloneable2{//子类即使实现了Cloneable ,调用clone() 时,无法经过这里调用Object 的clone 方法@Overrideprotected Object clone() throws CloneNotSupportedException{throw new CloneNotSupportedException();}}
import java.lang.reflect.Method;public class CloneUtil {@SuppressWarnings("unchecked")public static <T extends Cloneable> T clone(T obj) {T rtn = null;try {Method method = Object.class.getDeclaredMethod("clone");method.setAccessible(true);rtn = (T) method.invoke(obj);} catch (Throwable t) {//继承链中的clone() 方法被重写t.printStackTrace();}return rtn;}}