www.zhblog.net

Java判断两个对象是否相同

Java 中 “==” 是判断指针是否一样;Object 中 “equals”默认就是 “==”;String 类中重写了 “equals”方法。

public class Employee {   

    . . .   

    

    public boolean equals(Object otherObject)   {      

        // a quick test to see if the objects are identical

        if (this == otherObject) return true;



        // must return false if the explicit parameter is null      

        if (otherObject == null) return false;



        // if the classes don't match, they can't be equal      

        if (getClass() != otherObject.getClass()) return false;



        // now we know otherObject is a non-null Employee      

        Employee other = (Employee) otherObject;



        // test whether the fields have identical values      

        return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);   

    } 



}



 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情