亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2882 | 回復(fù): 7
打印 上一主題 下一主題

equals() 方法對哪些類有效啊? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2005-08-10 23:29 |只看該作者 |倒序瀏覽
equals()  在Object 類里有的 ,所以任何類用這個方法語法上都不會報錯吧?

可是象自己定義的類里用的時候返回值和自己預(yù)期的不一樣。  還請各位大大指點一下,小弟我是一只剛剛孵出來的菜鳥。。。

就是說下哪些類適合用  這個方法/

論壇徽章:
0
2 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2005-08-11 10:16 |只看該作者

equals() 方法對哪些類有效。

應(yīng)該要override equals方法的, 在effective java有專門的幾章在講這個, jsdk的javadoc里也有關(guān)于如何寫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 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2005-08-11 16:19 |只看該作者

equals() 方法對哪些類有效啊?

這里的    equals()  沒有被重寫吧?
是不是不重寫吧  ?

why this method return false here ?

論壇徽章:
0
6 [報告]
發(fā)表于 2005-08-12 13:11 |只看該作者

equals() 方法對哪些類有效。

java Object類中equals的方法很簡單:

  1.     public boolean equals(Object obj) {
  2.         return (this == obj);
  3.     }
復(fù)制代碼


上面的MyClass沒有override equals方法, 所以調(diào)用的就是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 [報告]
發(fā)表于 2005-08-13 02:04 |只看該作者

equals() 方法對哪些類有效?

多謝cool大哥了,我好象明白了 。
換句話說是否只有那些重寫了   equals()  方法的類才能象String()  那樣用  equals()   來比較呢(語法上我知道沒錯的)  ?

論壇徽章:
0
8 [報告]
發(fā)表于 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");
       }
   }
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP