1、有一个类Person,有两个字段age和name,我重写Object类的equal方法来比较两个对象的age和name是否相等,
但是不重写hashCode。

package com.hash;

public class Person {
    private Integer age;
    
    private String name;
    
    

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


    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    
    
}
View Code

相关文章:

  • 2021-10-20
  • 2022-12-23
  • 2021-12-02
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-06-07
猜你喜欢
  • 2021-07-11
  • 2021-12-15
  • 2021-06-16
  • 2021-09-24
  • 2021-06-22
  • 2022-12-23
  • 2021-11-21
相关资源
相似解决方案