视频里马士兵问道:Object类里面equals方法是怎么实现的?
一听我也懵了,因为一般都是重写了equals方法?
是怎么实现的呢?

解决方案 »

  1.   

    Object的equals采用最严格的比较
    设有两个Object o1,o2
     o1.equals(o2)==true 意味着o1和o2指向同一个实例对象(或者说有同样的内存地址)
    这时候 o1.equals(o2) == (o1==o2)
      

  2.   

    equals 其实的实现还是==
    只是这个比较的是内存地址
      

  3.   


    这样一来,如果是两个自定义类对象之间的比较,就是False了
      

  4.   


    是的,所以我们如果要用Equals,需要重现Equals方法。
      

  5.   


    equals是在Object类里的一个方法。而所有的类都是Object的子类,都会继承equals方法,但是在Object类中equals方法定义的是比较两个地址,
    子类中要比较的都不是两个地址而是程序员想比较的属性。所以要重写,比较方法。
      

  6.   


      /**
         * Indicates whether some other object is "equal to" this one.
         * <p>
         * The <code>equals</code> method implements an equivalence relation
         * on non-null object references:
         * <ul>
         * <li>It is <i>reflexive</i>: for any non-null reference value
         *     <code>x</code>, <code>x.equals(x)</code> should return
         *     <code>true</code>.
         * <li>It is <i>symmetric</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
         *     should return <code>true</code> if and only if
         *     <code>y.equals(x)</code> returns <code>true</code>.
         * <li>It is <i>transitive</i>: for any non-null reference values
         *     <code>x</code>, <code>y</code>, and <code>z</code>, if
         *     <code>x.equals(y)</code> returns <code>true</code> and
         *     <code>y.equals(z)</code> returns <code>true</code>, then
         *     <code>x.equals(z)</code> should return <code>true</code>.
         * <li>It is <i>consistent</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, multiple invocations of
         *     <tt>x.equals(y)</tt> consistently return <code>true</code>
         *     or consistently return <code>false</code>, provided no
         *     information used in <code>equals</code> comparisons on the
         *     objects is modified.
         * <li>For any non-null reference value <code>x</code>,
         *     <code>x.equals(null)</code> should return <code>false</code>.
         * </ul>
         * <p>
         * The <tt>equals</tt> method for class <code>Object</code> implements 
         * the most discriminating possible equivalence relation on objects; 
         * that is, for any non-null reference values <code>x</code> and
         * <code>y</code>, this method returns <code>true</code> if and only
         * if <code>x</code> and <code>y</code> refer to the same object
         * (<code>x == y</code> has the value <code>true</code>).
         * <p>
         * Note that it is generally necessary to override the <tt>hashCode</tt>
         * method whenever this method is overridden, so as to maintain the
         * general contract for the <tt>hashCode</tt> method, which states
         * that equal objects must have equal hash codes. 
         *
         * @param   obj   the reference object with which to compare.
         * @return  <code>true</code> if this object is the same as the obj
         *          argument; <code>false</code> otherwise.
         * @see     #hashCode()
         * @see     java.util.Hashtable
         */
        public boolean equals(Object obj) {
    return (this == obj);
        }Object类里写的很清楚
      

  7.   

    重新equals是你自己随便写的
    你想比较什么就在里面比较什么public boolean equals(Object obj) {
        if(obj.age==23 && obj.name == "zhangsan") return true;
        else return false;
    }
      

  8.   

    equals比较值==比较值和内存的地址
      

  9.   

    多谢楼上的各位,对equals用法清楚了