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 上運作所得,如有錯誤,歡迎指正。