Java笔记(7)——equals和toString方法的重写和一个例子

2023-05-16

1. Object类

Object类是所有类的根父类,如果在类中没有extends关键字指明其父类,默认其父类为Object类。

2. ==equals方法的区别

==:是否指向了同一个对象。
equals:方法为Object方法,只能比较引用类型,作用于==相同,比较是否指向了同一个对象。但是,在某些判断中,只是比较其类的类型和内容,所以需要对该方法进行重写。

判断,只要根据两个对象的年月日相同,结果为true。重写equals方法。

public class MyDate {
    //
    private int day;
    private int month;
    private int year;

    public MyDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public boolean equals(Object o) {
        // 比较  obj  和  this  是否是一个对象
        if (this == o) // 完全是同一个对象 自然是相同的
            return true;
        if (o == null || getClass() != o.getClass()) // 待比较对象为空或者所属的类不同 不是
            return false;

        MyDate myDate = (MyDate) o; // 将object类强制转换为 MyDate类

        if (day != myDate.day)
            return false;
        if (month != myDate.month)
            return false;
        return year == myDate.year;
    }

    @Override
    public int hashCode() {
        int result = day;
        result = 31 * result + month;
        result = 31 * result + year;
        return result;
    }


    public static void main(String[] args) {
        MyDate myDate = new MyDate(5,8,2021);
        MyDate myDate1 = new MyDate(5,8,2021);

        System.out.println(myDate.equals(myDate1)); // true

    }
}

/*
true
*/

3. toString方法的重写

toString方法在Object类中定义,返回值是String,返回类名和引用地址。

Person类中重写toString方法。

public class Person {
    protected int age;
    protected String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // 重写Object中的equals方法
    public boolean equals(Object object){
        if(object instanceof Person){
            Person person = (Person) object;
            return (person.getAge() == this.age)
                    && (person.getName().equals(this.name));
        }
        return false;
    }


    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class TestToString {
    public static void main(String[] args) {
        Person person = new Person(12, "Tom");
        System.out.println(person);

    }
}

/*
Person{age=12, name='Tom'}
*/

4. 例题

重写equalstoString方法,比较两个圆的半径是否相等;输出圆的半径。

public class GeometricObject {
    protected  String  color;
    protected  double  weight;

    protected GeometricObject(){
        color = "white";
        weight = 1.0;
    }

    public GeometricObject(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

}


public class Circle extends GeometricObject{
    private double radius;

    public Circle(){
        this.weight = 1.0;
        this.color = "white";
        this.radius = 1.0;
    }

    public Circle(double radius) {
        this();
        this.radius = radius;
    }

    public Circle(String color, double weight, double radius) {
        super(color, weight);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double findArea(){
        return Math.PI * this.radius * this.radius;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Circle circle = (Circle) o;

        return Double.compare(circle.radius, radius) == 0;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(radius);
        return (int) (temp ^ (temp >>> 32));
    }


    @Override
    public String toString() {
        return "Circle{" +
                "radius=" + radius +
                '}';
    }
}


public class TestCircle {
    public static void main(String[] args) {
        Circle circle1 = new Circle("red", 3,2);
        Circle circle2 = new Circle("green", 3, 4);
        System.out.println(circle1.getColor().equals(circle2.getColor()));
        System.out.println(circle1.equals(circle2));
        System.out.println(circle1);

    }
}

/*
false
false
Circle{radius=2.0}
Circle{radius=4.0}
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java笔记(7)——equals和toString方法的重写和一个例子 的相关文章

随机推荐

  • Settings模块的简单设计

    Settings 模块简单设计 任务 1 在一级菜单页底部增加一行 xff0c 点击这行 xff0c 跳转到二级菜单 xff0c 在二级菜单 xff0c 有一行 xff0c 是开关按钮 xff0c 可以记录上次的选中状态 解决思路 首先在设
  • Ubuntu下鼠标无法点击解决方案

    大数据之Ubuntu学习笔记 Ubuntu环境下鼠标无法点击问题描述解决方法 Ubuntu环境下鼠标无法点击 问题描述 在Ubuntu下使用idea构建maven项目时 xff0c 鼠标无法点击 xff0c 但可以在屏幕上移动 解决方法 1
  • 树莓派VNC显示模糊

    网上有人说设置分辨率 xff0c 亲测设置完后并没有提升清晰度 还有人拿远程桌面和vnc对比 xff0c 说远程桌面非常清晰 xff0c 的确 xff0c 用远程桌面登录是特别清晰 以上两点说明 xff1a 不清晰不是树莓派本身的原因 xf
  • 虚拟机之vmtools踩坑全解

    1 测试虚拟机 xff1a Ubuntu 18 04 manjaro 19 02 kali 19 03 2 目的 xff1a 安装vmtools xff0c 来使虚拟机全屏 鼠标移动不延迟 剪切板共用 可以拖入文件等功能 3 作者自述 xf
  • 驱动使能DAC模块

    DAC的操作原理图如下 xff1a 输入数据从DIN进入16为的移位寄存器 xff0c 其中低2位必须为0 xff0c 高4位为无效数据 xff0c 中间的10位为有效数据 xff0c 将中间的10位有效数据上传到DAC寄存器 DAC寄存器
  • LeetCode多线程

    1114 按序打印 我们提供了一个类 xff1a public class Foo public void one print 34 one 34 public void two print 34 two 34 public void th
  • 通过docker和gitlab实现项目自动打包部署

    Gitlab如何实现自动打包部署到docker集群 xff1f 预备知识 xff1a 开发模式转变 xff1a 瀑布模型 敏捷开发 DevOps DevOps xff1a Development Operations的组合词 CICD xf
  • JPA部分字段查询

    部分字段查询 实体类 编写想要查询字段的构造方法 span class token annotation punctuation 64 Data span span class token annotation punctuation 64
  • 数字马力一面

    1 自我介绍 2 讲下项目 xff0c 技术架构 3 xxl job的配置 xff0c 负载均衡策略讲一下 xff1f 基础配置如执行器名称 xff0c ip 端口等 负载均衡策略列举 xff1a 故障转移 xff0c 最近最久未使用 xf
  • Idea自动生成get()和set()方法的快捷操作

    1 首先创建好一个类 xff0c 里面可以暂时有2个属性 span class token keyword public span span class token keyword class span span class token c
  • 数字马力二面

    1 自我介绍下 2 介绍下项目 我们做的是CRM售后项目 xff0c 包括工单 xff0c 商返 xff0c 备件 xff0c 结算等九个模块 xff0c 我主要做的是工单 xff0c 备件模块 xff0c 工单模块就包括了各种报单 xff
  • 七层网络协议及三次握手四次挥手

    网络协议层次划分 为什么会有七层 xff0c 五层 xff0c 四层之说 七层协议 xff1a ISO定义的网络分层 xff0c 理论上的国际标准 四层协议 xff1a TCP IP分层 xff0c 实际的国际标准 五层协议 xff1a 我
  • git rebase 合并多次提交记录为一次提交

    使用git rebase 合并多次提交记录为一次提交 例 xff1a 下面有3次提交 xff0c 提交3的hash值为e79117d0 执行下面命令 span class token function git span rebase spa
  • 微服务整体架构图解

  • Spring自己注入自己,解决异步失效问题

    1 为什么会产生同一类内异步注解失效 xff1f 我们要清楚 xff0c 异步 xff0c 事务这些注解生效的原理 xff0c 在于通过切面创建了代理类 xff0c 通过操作代理类我们实现了异步 xff0c 事务 xff0c 但是当我们在同
  • 修改ubuntu(18.04)登录密码

    1 首先对Ubuntu系统进行重启 xff0c 在系统重启的过程中长按键盘shift键 xff0c 此时会进入GNU GRUB界面 xff0c 如下图所示 选择Advanced options for Ubuntu 按enter键进入 2
  • 怎样通过SpringMVC生成图片验证码?

    生成图片验证码 首先要添加一个jar包 kaptcha 2 3 2 jar 是一个java开源的验证码工具包 xff0c kaptcha 是一个非常实用的验证码生成工具 有了它 xff0c 你可以生成各种样式的验证码 xff0c 因为它是可
  • 使用phpstorm在线编辑服务器所创建的ftp文件,phpstorm小技巧。

    一 从服务器创建的开始 二 保证电脑上面可以访问服务器创建的ftp下的文件 可以使用FlashFXP 5 来连接服务器的ftp下的文件 使用说明如下 此处输入对应的ftp的IP地址 xff0c 用户名 xff0c 密码 就可以访问ftp文件
  • SR850出现fault in slot ALL PCI error on system ThinkSystem SR850报错

    SR850出现fault in slot ALL PCI error on system ThinkSystem SR850报错 一 报错信息显示二 判断故障并修复 一 报错信息显示 服务器型号为lenovo SR850 xff0c XCC
  • Java笔记(7)——equals和toString方法的重写和一个例子

    1 Object类 Object类是所有类的根父类 xff0c 如果在类中没有extends关键字指明其父类 xff0c 默认其父类为Object类 2 61 61 与equals方法的区别 61 61 xff1a 是否指向了同一个对象 e