SCJP 1.6版考題 185

出自 陳富國維基館
於 2013年3月30日 (六) 00:33 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: Given: 1. public class Person{ 2. private String name; 3. public Person(String name){this.name = name;} 4. public boolean equals(Person p){ 5. return p.name.equals(this...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
Given:
1. public class Person{
2.   private String name;
3.   public Person(String name){this.name = name;}
4.   public boolean equals(Person p){
5.     return p.name.equals(this.name);
6.   }
7. }

Which statement is true?
 A. The equals method does NOT properly override the Object.equals method.
 B. Compilation fails because the private attribute p.name cannot be accessed in line 5.
 C. To work correctly with hash-based data structures, this class must also implement the hashCode method.
 D. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

解答


Ans: A

解說:

Object的equals方法原型為:
boolean equals(Object o)
覆載equals方法應以上面的寫法
請參考String的equals
boolean equals(Object anObject)