java 中的 clone()
clone() 是 Object 类中的函数
函数原型:
protected Object clone() throws CloneNotSupportedException
作用:java 中没有拷贝构造函数,所以有 clone()
我们可以自己写一个类,实现 Cloneable 接口
package test_clone;
public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {
private double side;
public Octagon() {
this(1);
}
public Octagon(double side) {
super();
this.side = side;
}
public void setSide(double side) {
this.side = side;
}
public double getArea() {
return (2 + 4 / Math.sqrt(2)) * this.side * this.side;
}
public double getPerimeter() {
return 8 * this.side;
}
@Override
public int compareTo(Octagon o) {
return (int) (this.side - o.side);
}
@Override
public Octagon clone() {
Octagon octagon = new Octagon();
octagon.setSide(this.side - 1);
return octagon;
}
@Override
public String toString() {
return "Octagon [side=" + side + "]";
}
其中 GeometricObject 是自定义的一个类,如下
package test_clone;
public class GeometricObject {
private String color;
private boolean filled;
public GeometricObject() {
this.color = "white";
this.filled = false;
}
public GeometricObject(String color, boolean filled) {
super();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
@Override
public String toString() {
return "GeometricObject [color=" + color + ", filled=" + filled + "]";
}
}
我们在类 Octagon 中可以不用覆盖 clone() 函数,即不写:
@Override
public Octagon clone() {
Octagon octagon = new Octagon();
octagon.setSide(this.side - 1);
return octagon;
}
那么当调用 clone() 时,是默认的值的复制,我们覆盖后可以让 side 减1。
测试代码
package test_clone;
import java.util.Arrays;
public class TestClone {
public static void main(String[] args) {
Octagon octagon1 = new Octagon(5);
Octagon octagon2 = octagon1.clone();
System.out.println(octagon1.toString());
System.out.println(octagon2.toString());
}
}
运行结果:

补充
数组是默认实现了 Cloneable 接口的。
例如:
int[] a = new int[] {1,2,3,4,5};
int[] b = a.clone();
System.out.println(Arrays.toString(b));
结果:
说明:
Arrays.toString(b)
能够将数组 b 中元素如图输出。
下面是重点:
那么对于实现了 Cloneable 接口并覆盖了 clone() 函数的类 Octagon 来讲,对 Octagon 的对象数组调用 clone() 会发生什么?
public static void main(String[] args) {
Octagon octagon1 = new Octagon(5);
Octagon octagon2 = octagon1.clone();
System.out.println(octagon1.toString());
System.out.println(octagon2.toString());
Octagon[] octagons1 = new Octagon[3];
for (int i = 0; i < octagons1.length; i++) {
octagons1[i] = new Octagon(i + 1);
}
Octagon[] octagons2 = octagons1.clone();
System.out.println(Arrays.toString(octagons1));
System.out.println(Arrays.toString(octagons2));
}
输出:
由 octagons1 数组克隆得到的 octagons2 数组中的值没有对应 减1。
官方文档解释是:
Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a “shallow copy” of this object, not a “deep copy” operation.
对象数组调用 clone 方法时,用的是浅拷贝。
以上均为个人见解,结果均在 win10:eclipse 上运行所得,如有错误,欢迎指正。