| sakulagi 回复于:2005-08-11 08:34:16
|
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.
|
| cooljia 回复于:2005-08-11 10:16:29
|
应该要override equals方法的, 在effective java有专门的几章在讲这个, jsdk的javadoc里也有关于如何写equals的介绍
下面是几个原则:
The equals method implements an equivalence relation on non-null object references:
[list:074c2597f0]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.[/list:u:074c2597f0]
下面是如何写hashcode的原则:
The general contract of hashCode is:
[list:074c2597f0]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.[/list:u:074c2597f0]
BTW: CU的文本编辑真是太烂, 居然没有bullets或numbering.还要手工
|
| dudu86 回复于:2005-08-11 16:17:59
|
这里有道题目,谁能帮我讲解一下。
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";
|
| dudu86 回复于:2005-08-11 16:19:44
|
这里的 equals() 没有被重写吧?
是不是不重写吧 ?
why this method return [b:f0852cab33]false[/b:f0852cab33] here ?
|
| cooljia 回复于:2005-08-12 13:11:07
|
java Object类中equals的方法很简单:
[code:1:eb79b42d07]
public boolean equals(Object obj) {
return (this == obj);
}
[/code:1:eb79b42d07]
上面的MyClass没有override equals方法, 所以调用的就是object中的equals方法
而==号在java language specification中是这么解释的.
[list:eb79b42d07]
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.
[/list:u:eb79b42d07]
即只有指向同一object的reference才返回true, 因为a,b是指向两个object, 所以返回是false
即使两个String里面包含同样的字符, 用==一定是返回false, 只能用stringA.equals(stringB), 这里特别注意的是String类是override了Object父类的equals方法的了
|
| dudu86 回复于:2005-08-13 02:04:17
|
多谢cool大哥了,我好象明白了 。
换句话说是否只有那些重写了 equals() 方法的类才能象String() 那样用 equals() 来比较呢(语法上我知道没错的) ?
|