1 package com.jdk7.chapter1; 2 3 public class ComplexNumber implements Cloneable{ 4 5 private double realPart; 6 private double imagePart; 7 8 public double getRealPart() { 9 return realPart; 10 } 11 12 public void setRealPart(double realPart) { 13 this.realPart = realPart; 14 } 15 16 public double getImagePart() { 17 return imagePart; 18 } 19 20 public void setImagePart(double imagePart) { 21 this.imagePart = imagePart; 22 } 23 24 @Override 25 public String toString() { 26 return "realPart=" + realPart + ", imagePart=" + imagePart + "i"; 27 } 28 29 /** 30 * 默认构造函数 31 */ 32 public ComplexNumber(){ 33 this.realPart = 0.0; 34 this.imagePart = 0.0; 35 } 36 37 /** 38 * 自定义带参构造函数 39 * @param a 40 * @param b 41 */ 42 public ComplexNumber(double a,double b){ 43 this.realPart = a; 44 this.imagePart = b; 45 } 46 47 /** 48 * 复数相加 49 * 返回复数类型 50 * @return 51 */ 52 public ComplexNumber add(ComplexNumber comn){ 53 if(comn==null){ 54 System.err.println("对象不能为null"); 55 return new ComplexNumber(); 56 }else{ 57 return new ComplexNumber(this.realPart+comn.realPart,this.imagePart+comn.imagePart); 58 } 59 } 60 61 /** 62 * 实现Cloneable接口,将equal()重写 63 * 分为以下3中情况: 64 * 1.比较对象为空; 65 * 2.比较对象为被比较或者子类 66 * 2.1复数相等 67 * 2.2复数不等 68 * 3.比较对象不为被比较或者子类 69 */ 70 public boolean equals(Object obj){ 71 if(obj==null){ 72 System.out.println("对象不能为空"); 73 return false; 74 }else if(obj instanceof ComplexNumber){ 75 ComplexNumber c = (ComplexNumber)obj; 76 if(this.realPart==c.realPart && this.imagePart==c.imagePart){ 77 return true; 78 }else{ 79 System.out.println("复数不相同"); 80 return false; 81 } 82 }else{ 83 System.out.println("对象不属于复数类型"); 84 return false; 85 } 86 } 87 88 /** 89 * 如果两个对象的值是相同的->调用toString()方法转换为字符串也相同->再将字符串转换为hashcode则一定是相同的 90 */ 91 public int hashCode(){ 92 return this.toString().hashCode(); 93 } 94 95 /** 96 * 一定要继承接口中的clone()方法,克隆一个自定义类型,并将被克隆对象的值赋给克隆对象 97 */ 98 public ComplexNumber clone(){ 99 try{ 100 ComplexNumber d = (ComplexNumber)super.clone(); //继承接口的clone()方法,将Object类型强制转换为要克隆的类型 101 d.setRealPart(this.realPart); 102 d.setImagePart(this.imagePart); 103 return new ComplexNumber(d.realPart,d.imagePart); 104 }catch(CloneNotSupportedException e){ 105 e.printStackTrace(); 106 return null; 107 } 108 } 109 110 public static void main(String[] args) { 111 ComplexNumber a = new ComplexNumber(1,2); 112 // ComplexNumber a = new ComplexNumber(); 113 // ComplexNumber a = null; 114 ComplexNumber b = new ComplexNumber(1,2); 115 // ComplexNumber b = new ComplexNumber(2,4); 116 // ComplexNumber b = new ComplexNumber(); 117 // ComplexNumber b = null; 118 // ComplexNumber1 b = new ComplexNumber1(); 119 // 被加数不能为null 120 // System.out.println("(a+b) = "+a.add(b).toString()); 121 122 //a和b的值虽然相同,但是在声明为类类型变量时为不同的内存地址,object提供的equels方法比较的是内存地址 123 System.out.println("(a==b) : "+a.equals(b)); 124 //对象实例内存地址不同,hashcode以对象实例在内存的地址生成hashcode也不相同 125 System.out.println("hashcode of a: "+a.hashCode()); 126 System.out.println("hashcode of b: "+b.hashCode()); 127 System.out.println("(a.hashCode==b.hashCode) = "+(a.hashCode()==b.hashCode())); 128 System.out.println("clone a: "+a.clone()); 129 130 } 131 132 } 133 134 135 运行结果: 136 (a==b) : true 137 hashcode of a: 1012487935 138 hashcode of b: 1012487935 139 (a.hashCode==b.hashCode) = true 140 clone a: realPart=1.0, imagePart=2.0i