Pfeffer
2017-08-04 05:09
采纳率: 53%
浏览 1.2k
已采纳

java核心技术卷一 equals方法 EqualTest.java

 public class EqualTest {

    public static void main(String[] args) {

        Employee alice1 = new Employee("Alice Adams", 75000, 1989, 12, 15);
        Employee alice2 = alice1;
        Employee alice3 = new Employee("Alice Adams", 75000, 1989, 12, 15);
        Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

        System.out.println("alice1 == alice2: "+(alice1==alice2));
        System.out.println("alice1 == alice3: "+(alice1==alice3));
        System.out.println("alice1.equals(alice3): "+alice1.equals(alice3));
        System.out.println("alice1.equals(bob): "+alice1.equals(bob));
        System.out.println("bob.toString(): "+bob);

        Manager car1 = new Manager("Carl Cracker", 80000, 1987, 12, 15);
        Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
        boss.setBonus(5000);
        System.out.println("boss.toString(): "+boss);
        System.out.println("carl.equals(boss): "+car1.equals(boss));
        System.out.println("alice1.hashCode(): "+alice1.hashCode());
        System.out.println("alice3.hashCode(): "+alice3.hashCode());
        System.out.println("bob.hashCode(): "+bob.hashCode());
        System.out.println("car1.hashCode(): "+car1.hashCode());

    }

}


import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

public class Employee {

    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n,double s,int year,int month,int day){
        name = n;
        salary = s;
        GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
        hireDay = calendar.getTime();
    }

    public String getName(){
        return name;
    }
    public double getSalary(){
        return salary;
    }
    public Date getHireDay(){
        return hireDay;
    }

    public void raiseSalary(double byPercent){
        double raise = salary * byPercent / 100;
        salary += raise;
    }
    public boolean equals(Object otherObject){
    //这里获得一个对象参数,第一个if语句判断两个引用是否是同一个,如果是那么这两个对象肯定相等
        if(this == otherObject)
            return true;
//这里判断这个参数是否引用空值
        if(otherObject == null)
            return false;
//getClass()方法是得到对象的类,这里就是如果两个对象的类不一样,那么就不相等
        if(getClass()!=otherObject.getClass())
            return false;
//在以上判断完成,再将得到的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,然后再判断对象的属性是否相同      
        Employee other = (Employee)otherObject;

        return Objects.equals(name, other.name)&&salary ==other.salary&&
                Objects.equals(hireDay, other.hireDay);
    }
    //哈希散列,有自己的计算方法,根据字符串来得到一段数字
    public int hashCode(){
        return Objects.hash(name,salary,hireDay);
    }

//toString()方法,可自动生成
    @Override
    public String toString() {
        return getClass().getName()+ "[name=" + name + ", salary=" + salary + ", hireDay=" + hireDay + "]";
    }


}


public class Manager extends Employee {

    private double bonus;

    public Manager(String n,double s, int year,int month,int day){
        super(n, s, year, month, day);
        bonus = 0;
    }

    public double getSalary(){
        double baseSalary = super.getSalary();
        return baseSalary + bonus;
    }

    public void setBonus(double b){
        bonus = b;
    }

    public boolean equals(Object otherObject){
        if(!super.equals(otherObject))
            return false;
        Manager other = (Manager)otherObject;
        return bonus == other.bonus;
    }

    public int hashCode(){
        return super.hashCode()+17*new Double(bonus).hashCode();
    }

    public String toString(){
        return super.toString()+"[bonus=]"+bonus+"]";
    }
}

这个程序里面有一部分,是public boolean equals(object otherObject)方法,
在这个方法的return语句里面莫名其妙的出现了一个Object.equals(a,b),我不明白这个是从哪里来的,求大神解释一下。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • yangm_ingliang 2017-08-04 09:33
    已采纳

    上源码

       public static boolean equals(Object a, Object b) {
            return (a == b) || (a != null && a.equals(b));
        }
    
    点赞 打赏 评论
  • Q544471255 2017-08-04 05:20

    equals是Object的方法,而Object是所有类的根类,所以所有类自创建起,就拥有了equals方法.

    你可以看下java.lang.Object这个类的源码,里面就有equals的定义(子类可以覆盖Object的equals,比如String就覆盖了Object的equals).

    点赞 打赏 评论
  • 昭杏之念 2017-08-04 10:27

    Object.equals(a,b)调用了Object类的静态equals方法,比较ab是否同一地址。
    Object类中有一个equals(Object o)的非静态方法还有一个equals(Object a, Object b)静态方法

    点赞 打赏 评论
  • Jerry_CW 2018-11-20 12:18

    static Objects.equals()方法是Java7中新加入的,之前的equals()方法如果传入null,运行时报错,空指针异常,

    而这个新方法会返回false,而不报异常

    点赞 打赏 评论

相关推荐 更多相似问题