一个普通的Java类,继承了Object对象,便继承了该对象的Clone方法
protected native Object clone() throws CloneNotSupportedException;
但是Java基础类或String类却不支持这样的操作
Integer x = new Integer(10);x.clone(); //编译报错String s = "abc";s.clone(); //编译报错
可理解为,Integer类或者String类直接虽然继承了Object,在Integer或者String内部使可以访问clone方法的,由于该方法是protected限制级别。
Java对象Clone对象
1.实现Cloneable接口。该接口标记类为可克隆的,Object.clone()方法会验证一个类是否实现了Cloneable接口,如果没有,在运行期会throw CloneNotSupportedException。
2.可覆盖clone方法,实现语境下的浅层复制或者深层复制。
3.调用super.clone()方法。
一个简单的例子实现浅层复制
package clone;/** * 克隆研究 * * @author xl * */public class Snake implements Cloneable {Snake next;char c;// 构造器public Snake(int i, char x) {this.c = x;if (--i > 0) {next = new Snake(i, (char) (x + 1));}}// 增量public void increment() {c++;if (next != null) {next.increment();}}@Overridepublic String toString() {String s = ":" + c;if (next != null) {s += next.toString();}return s;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}public static void main(String[] args) throws CloneNotSupportedException {Snake s = new Snake(5, 'a');System.out.println("s = " + s);Snake s2 = (Snake) s.clone();System.out.println("s2 = " + s2);System.out.println("s = " + s.next.hashCode() + ": " + s.c);System.out.println("s2 = " + s2.next.hashCode() + ": " + s2.c);//s2.increment();System.out.println("after s.increment, s2 = " + s2);}}
再次重载clone,可实现深层复制
@Overrideprotected Object clone() throws CloneNotSupportedException {Snake s = null;s = (Snake) super.clone();if (s.next != null)s.next = (Snake) s.next.clone();return s;}
克隆原理
Object.clone()方法负责建立正确的存储容量(原对象的大小),并通过“按位复制”将二进制位从原始对象中复制到新对象的存储空间。所以克隆的第一步应该调用super.clone()方法,为后续的复制建立基础,最后在自己的重载方法中完成必要的操作,以最终完成克隆。
人生就像是一场旅行,遇到的既有感人的,