
/**
* 根据现有对象克隆一个新对象
*/
public Object clone() {
// 如果你要使自定义的类能够被clone,就必须实现Cloneable接口并且重写它的clone()方法.
// 如果你仅仅重写了clone方法而没有在类的声明中添加实现Cloneable接口,调用clone方法时将会出现
// CloneNotSupportedException异常,读者可以试试。
try {
ComplexNumber newObject = (ComplexNumber) super.clone();
newObject.setRealPart(this.realPart);
newObject.setImaginaryPart(this.imaginaryPart);
return newObject;
} catch (CloneNotSupportedException e) {
// //如果没有实现Cloneable接口,抛出异常
e.printStackTrace();
return null;
}
}


