-3
這種使用clone
的方式是正確的嗎?我每次都遇到運行時錯誤。也有人可以提出一種方法來寫這個類的複製構造函數嗎?如何克隆/複製我自己的類的實例?
public class Pair {
final StringBuffer x;
final StringBuffer y;
public Pair(StringBuffer x, StringBuffer y) {
this.x = x;
this.y = y;
}
public StringBuffer getX() {
return x;
}
public StringBuffer getY() {
return y;
}
public Pair clone() {
Pair p = new Pair(new StringBuffer(), new StringBuffer());
try {
p = (Pair) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error();
}
return p;
}
}
哪裏'arraylist'在標題和標籤中提到? –
爲什麼你有一個複製構造函數,當你忽略它的作用? – Tom