Given: 11. public class Person{ 12. private String name, comment; 13. private int age; 14. public Person(String n, int a, String c){ 15. name = n; age = a; comment = c; 16. } 17. public boolean equals(Object o){ 18. if (!(o instanceof Person)) return false; 19. Person p = (Person)o; 20. return age == p.age && name.equals(p.name); 21. } 22. } What is the appropriate definition of the hashCode method in class Person? A. return super.hashCode(); B. return name.hashcode() + age * 7; C. return name.hashCode() + comment.hashCode() / 2; D. return name.hashCode() + comment.hashCode() / 2- age * 3; Ans: D 解說 : 此題原本的流傳的答案為D,但照Larry大大說此題答案應為B,我想B才是對的,理由是Person這個類別中有一個equals方法,此方法以類別中的name與age做為Person物件的equals比較,也就是說二個Person物件是否相等 視其name與age 二個成員資料而定,而hashCode做為一個物件的雜湊值計算應取...
Comments