马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我在类中仅仅只是重写了clone方法 而没有在类的声明中添加实现Cloneable接口,那么在调用clone方法的时候为什么没有抛出CloneNotSupportedException异常?
public class ComplexNumber implements Cloneable{
private double realPart;//复数的实部
private double imaginaryPart;//复数的虚部
public ComplexNumber()//默认构造函数
{
this.imaginaryPart=0.0;
this.realPart=0.0;
//System.out.println("无参构造函数被调用");
}
public ComplexNumber(double a,double b)//构造一个真正的复数
{
this.realPart=a;
this.imaginaryPart=b;
//System.out.println("有参构造函数被调用");
}
public double getImaginaryPart() {
return imaginaryPart;
}
public double getRealPart() {
return realPart;
}
//将一个复数显示为字符串
public String toString() {
return this.realPart+"+"+this.imaginaryPart+"i";
}
//重新设置实部
public void setRealPart(double a) {
this.realPart=a;
}
//重新设置虚部
public void setImaginaryPart(double a) {
this.imaginaryPart=a;
}
//复数的加法运算
public ComplexNumber add(ComplexNumber aNum) {
if(aNum==null) {
System.out.println("对象不能够为null");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart+aNum.realPart,this.imaginaryPart+aNum.imaginaryPart);
}
//复数的减法运算
public ComplexNumber sub(ComplexNumber aNum) {
if(aNum==null) {
System.out.println("对象不能够为null");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart-aNum.realPart,this.imaginaryPart-aNum.imaginaryPart);
}
//复数的乘法运算
public ComplexNumber multiply(ComplexNumber aNum) {
if(aNum==null) {
System.out.println("对象不能够为null");
return new ComplexNumber();
}
double newReal=this.realPart*aNum.realPart-this.imaginaryPart*aNum.imaginaryPart;
double newImaginary=this.imaginaryPart*aNum.realPart+this.realPart*aNum.imaginaryPart;
ComplexNumber result=new ComplexNumber(newReal,newImaginary);
return result;
}
//复数的除法运算
public ComplexNumber divide(ComplexNumber aNum) {
if(aNum==null) {
System.out.println("对象不能够为null");
return new ComplexNumber();
}
if(aNum.realPart==0&&aNum.imaginaryPart==0) {
System.out.println("除数不能够为0");
return new ComplexNumber();
}
double temp=aNum.realPart*aNum.realPart+aNum.imaginaryPart*aNum.imaginaryPart;
double newReal=(this.realPart*aNum.realPart+this.imaginaryPart*aNum.imaginaryPart)/temp;
double newImaginary=(this.imaginaryPart*aNum.realPart-this.realPart*aNum.imaginaryPart)/temp;
ComplexNumber result=new ComplexNumber(newReal,newImaginary);
return result;
}
//比较值是否相等
public boolean equals(Object obj) {
if(obj==null) {
return false;
}
//首先判断是不是ComplexNumber类的一个实例对象;可用instanceof来判断
if(obj instanceof ComplexNumber) {
ComplexNumber b=(ComplexNumber)obj;
if(this.realPart==b.realPart&&this.imaginaryPart==b.imaginaryPart)
return true;
else
return false;
}else return false;
}
//获取对象的哈希码 即地址
/*hashCode是编译器为不同对象产生的不同整数,根据equal方法的定义:
如果两个对象是相等(equal)的,那么两个对象调用 hashCode必须产生相同的整数结果,
即:equal为true,hashCode必须为true,equal为false,hashCode也必须 为false,所以必须重写hashCode来保证与equal同步。*/
public int hashCode(){
return this.toString().hashCode();
}
//根据现有对象克隆一个新对象,新对象和现有对象的值一样
public Object clone() {
try {
//克隆一个对象时,应先调用父类的clone方法
ComplexNumber newObject=(ComplexNumber)super.clone();
//将现有对象的值给新对象
newObject.setRealPart(this.realPart);//可有可无
newObject.setImaginaryPart(this.imaginaryPart);
return newObject;
}catch(CloneNotSupportedException e) {
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
return null;}
}
public static void main(String[] args) throws CloneNotSupportedException{
ComplexNumber test1=new ComplexNumber(2,4);
ComplexNumber test2=new ComplexNumber(2,4);
System.out.println("ComplexNumber test1:"+test1.toString());
System.out.println("ComplexNumber test2:"+test2.toString());
System.out.println("test1.equals(test 2):"+test1.equals(test2));
System.out.println("test1.hashCode:"+test1.hashCode()+";"+"test2.hashCode:"+test2.hashCode());
System.out.println("test1.clone="+test1.clone().toString());
//System.out.println("test1.clone.hashCode="+test1.clone().toString().hashCode());
}
}
|