重写复数类的equals、hashCode、clone方法
Complex:
package com.itheima1;
public class ComplexNumber implements Cloneable{
private double realPart,imageinaryPart;
public ComplexNumber() {
realPart = 0.0;
imageinaryPart = 0.0;
}
public ComplexNumber(double realPart, double imageinaryPart) {
this.realPart = realPart;
this.imageinaryPart = imageinaryPart;
}
public double getRealPart() {
return realPart;
}
public void setRealPart(double realPart) {
this.realPart = realPart;
}
public double getImageinaryPart() {
return imageinaryPart;
}
public void setImageinaryPart(double imageinaryPart) {
this.imageinaryPart = imageinaryPart;
}
public ComplexNumber add(ComplexNumber aComNum){
if(aComNum == null){
System.out.println("对象不能够为null");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart + aComNum.getRealPart(),
this.imageinaryPart + aComNum.getImageinaryPart());
}
public ComplexNumber decrease(ComplexNumber aComNum){
if(aComNum == null){
System.out.println("对象不能够为null");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart - aComNum.getRealPart(),
this.imageinaryPart - aComNum.getImageinaryPart());
}
public ComplexNumber multiply(ComplexNumber aComNum){
if(aComNum == null){
System.out.println("对象不能够为null");
return new ComplexNumber();
}
double r = this.realPart * aComNum.getRealPart() -
this.imageinaryPart * aComNum.getImageinaryPart();
double i = this.imageinaryPart * aComNum.getRealPart() +
this.realPart * aComNum.getImageinaryPart();
return new ComplexNumber(r,i);
}
public ComplexNumber divide(ComplexNumber aComNum){
if(aComNum == null){
System.out.println("对象不能够为null");
return new ComplexNumber();
}
if((aComNum.imageinaryPart == 0) && (aComNum.realPart == 0)){
System.out.println("除数不能为零");
return new ComplexNumber();
}
double temp = aComNum.getRealPart() * aComNum.getRealPart() +
aComNum.getImageinaryPart() * aComNum.getImageinaryPart();
double crealpart = (this.realPart * aComNum.getRealPart()
+ this.imageinaryPart * aComNum.imageinaryPart) / temp;
double cimaginary = (this.imageinaryPart * aComNum.getRealPart() -
this.realPart * aComNum.getImageinaryPart()) / temp;
return new ComplexNumber(crealpart,cimaginary);
}
@Override
public String toString() {
return this.realPart + " + " + this.imageinaryPart + "i";
}
@Override
public int hashCode() {
return this.toString().hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) return true; // 直接判断地址是否相等
if (o == null || getClass() != o.getClass()) return false;
ComplexNumber that = (ComplexNumber) o;
if (Double.compare(that.realPart, realPart) != 0) return false;
return Double.compare(that.imageinaryPart, imageinaryPart) == 0;
}
@Override
public Object clone(){
try{
ComplexNumber newObject = (ComplexNumber) super.clone();
newObject.setRealPart(this.realPart);
newObject.setImageinaryPart(this.imageinaryPart);
return newObject;
}catch (CloneNotSupportedException e){
e.printStackTrace();
return null;
}
}
}
Main:
package com.itheima1;
public class Main {
public static void main(String[] args) {
ComplexNumber a = new ComplexNumber(2,4);
ComplexNumber b = new ComplexNumber(2,4);
System.out.println("ComplexNumber a: " + a.toString());
System.out.println("ComplexNumber b: " + b.toString());
System.out.println("a.equals(b) = " + a.equals(b));
System.out.println("a.hashCode = " + a.hashCode() + "; " + "b.hashCode = "+b.hashCode());
System.out.println("a.clone = " + a.clone());
}
}