免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2881 | 回复: 7
打印 上一主题 下一主题

equals() 方法对哪些类有效啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-10 23:29 |只看该作者 |倒序浏览
equals()  在Object 类里有的 ,所以任何类用这个方法语法上都不会报错吧?

可是象自己定义的类里用的时候返回值和自己预期的不一样。  还请各位大大指点一下,小弟我是一只刚刚孵出来的菜鸟。。。

就是说下哪些类适合用  这个方法/

论坛徽章:
0
2 [报告]
发表于 2005-08-11 08:34 |只看该作者

equals() 方法对哪些类有效啊?

equal() can be overriden freely. I'm not sure how you implemented your own equal(). Could you provide some more info?

Sorry I cannot input Chinese these days.

论坛徽章:
0
3 [报告]
发表于 2005-08-11 10:16 |只看该作者

equals() 方法对哪些类有效啊?

应该要override equals方法的, 在effective java有专门的几章在讲这个, jsdk的javadoc里也有关于如何写equals的介绍

下面是几个原则:
The equals method implements an equivalence relation on non-null object references:

    1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    2. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
    5. For any non-null reference value x, x.equals(null) should return false.


下面是如何写hashcode的原则:
The general contract of hashCode is:

    1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
    2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
    3. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.


BTW: CU的文本编辑真是太烂, 居然没有bullets或numbering.还要手工

论坛徽章:
0
4 [报告]
发表于 2005-08-11 16:17 |只看该作者

equals() 方法对哪些类有效啊?

这里有道题目,谁能帮我讲解一下。
Question 19
What is the output of the following code?
  1:    class MyClass    {
  3:        static int maxElements;   
  5:        MyClass(int maxElements)     {
  7:            this.maxElements = maxElements;
  8:        }
  10:    }
  12:    public class Q19  {
  14:        public static void main(String[] args)     {
  16:            MyClass a = new MyClass(100);
  18:            MyClass b = new MyClass(100);
  20:            if(a.equals(b))  System.out.println("Objects have the same values";
  22:            else          System.out.println("Objects have different values";
  24:        }
  25:    }
  A) Compilation error at line 20. equals() method was not defined.
  B) Compiles fine, runtime exception at line 20.
  C) Prints "Objects have the same values".
  D) Prints "Objects have different values";

论坛徽章:
0
5 [报告]
发表于 2005-08-11 16:19 |只看该作者

equals() 方法对哪些类有效啊?

这里的    equals()  没有被重写吧?
是不是不重写吧  ?

why this method return false here ?

论坛徽章:
0
6 [报告]
发表于 2005-08-12 13:11 |只看该作者

equals() 方法对哪些类有效啊?

java Object类中equals的方法很简单:

  1.     public boolean equals(Object obj) {
  2.         return (this == obj);
  3.     }
复制代码


上面的MyClass没有override equals方法, 所以调用的就是object中的equals方法

而==号在java language specification中是这么解释的.

    15.21.3 Reference Equality Operators == and !=

    ..
    The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.

    While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t). See also §3.10.5.


即只有指向同一object的reference才返回true, 因为a,b是指向两个object, 所以返回是false

即使两个String里面包含同样的字符, 用==一定是返回false, 只能用stringA.equals(stringB), 这里特别注意的是String类是override了Object父类的equals方法的了

论坛徽章:
0
7 [报告]
发表于 2005-08-13 02:04 |只看该作者

equals() 方法对哪些类有效啊?

多谢cool大哥了,我好象明白了 。
换句话说是否只有那些重写了   equals()  方法的类才能象String()  那样用  equals()   来比较呢(语法上我知道没错的)  ?

论坛徽章:
0
8 [报告]
发表于 2005-11-30 16:36 |只看该作者
class MyClass    {
        private int maxElements;     
        MyClass(int maxElements)     {
            this.maxElements = maxElements;
       }
        public boolean equals(Object myClass)
        {
            return this.maxElements == ((MyClass)myClass).maxElements;
        }
    }  
    public class Q19  {
        public static void main(String[] args)     {
            MyClass a = new MyClass(100);
           MyClass b = new MyClass(100);
            if(a.equals(b))  System.out.println("Objects have the same values");
           else          System.out.println("Objects have different values");
       }
   }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP