【Java】【类和对象】equals、hashCode、clone方法

重写复数类的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());
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写一个类实现复数的运算。 复数类ComplexNumber的属性: m_dRealPart:实部,代表复数的实数部分。 m_dImaginPart:虚部,代表复数的虚数部分。 复数类ComplexNumber的方法: ComplexNumber():构造函数,将实部、虚部都置为0。 ComplexNumber(double r,double i):构造函数,创建复数对象的同时完成复数的实部、虚部的初始化,r为实部的初值,i为虚部的初值。 getRealPart():获得复数对象的实部。 getImaginPart():获得复数对象的虚部。 setRealPart(double d):把当前复数对象的实部设置为给定的形式参数的数字。 setImaginaryPart(double d):把当前复数对象的虚部设置为给定的形式参数的数字。 complexAdd(ComplexNumber c):当前复数对象与形式参数复数对象相加,所得的结果也是复数值,返回给此方法的调用者。 complexAdd(double c):当前复数对象与形式参数实数对象相加,所得的结果仍是复数值,返回给此方法的调用者。 complexMinus(ComplexNumber c) :当前复数对象与形式参数复数对象相减,所得的结果也是复数值,返回给此方法的调用者。 complexMinus(double c) :当前复数对象与形式参数实数对象相减,所得的结果仍是复数值,返回给此方法的调用者。 complexMulti(ComplexNumber c):当前复数对象与形式参数复数对象相乘,所得的结果也是复数值,返回给此方法的调用者。 complexMulti(double c):当前复数对象与形式参数实数对象相乘,所得的结果仍是复数值,返回给此方法的调用者。 toString():把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中和分别为实部和虚部的数据。 2. 编写Java Application程序使用上题定义的类,检查类定义是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值