Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50797
  • 博文数量: 13
  • 博客积分: 1465
  • 博客等级: 上尉
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-01 10:15
文章分类

全部博文(13)

文章存档

2011年(2)

2008年(11)

我的朋友

分类: Java

2008-11-10 21:43:38

Q-1:
[hashcode & equals ]
 
clss Sortof{
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
// insert code
}
}

Which of the following would fulfill the equals() and hashCode() contracts of the class?

A & B irrelevant
C. return ((SortOf)o).code.length() * ((SortOf)o).bal == this.code.length()* this.bal;

D. return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate== this.code.length()* this.bal * this.rate;


The answer given is "C"
 
Note:
阅读(666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~