2009年2月26日 星期四

第211題

Given:
11. public class Key{
12. private long id1,
13. private long id2;
14.
15. //class Key methods
16. }
A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap.
Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)
A. public int hashCode()
B. public void hashCode()
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k)
答案:AD
參考:12-4-2 equals()、hashCode()方法的改寫

-----------------------------------------------

Key 會先用hashCode 來比較, 如果得到的值一樣,無法分辨不同時就會用 equals.
hashCode 回傳是 int.

沒有留言:

張貼留言