Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public boolean equals(Object o) {
17. if( !o instanceof Person ) return false;
18. Person p = (Person) o;
19. return p.name.equals(this.name);
20. }
21. }
Which is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same
name.
C. All Person objects will have the same hash code because the
hashCode method is not overridden.
D. If a HashSet contains more than one Person object with
name=”Fred”, then removing another Person, also with name=”Fred”,
will remove them all.
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public boolean equals(Object o) {
17. if( !o instanceof Person ) return false;
18. Person p = (Person) o;
19. return p.name.equals(this.name);
20. }
21. }
Which is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same
name.
C. All Person objects will have the same hash code because the
hashCode method is not overridden.
D. If a HashSet contains more than one Person object with
name=”Fred”, then removing another Person, also with name=”Fred”,
will remove them all.
解决方案 »
- 继续问多线程情况下的数据库操作的线程安全性问题
- 重载,重写只是多态的体现怎么理解?
- 求助.JBUILDER一打开就死机.
- 帮忙看个错哦java.io.IOException: Cannot run program "rundll32 url.dll,FileProtocolHandler": CreateProcess error=2, ?????????
- jdbc连接mysql不能updateRow问题
- java文件监控问题
- jboss3.2中配置mssqlserver的解决方案?
- JDK1.3 JDK1.4的问题
- 菜鸟级问题!
- Java的一个读取文件的超简单问题,为什么会出错!!
- EJB主要讲的什么,一般人要掌握什么?
- 高手指点一下!
没有重写hashCode()方法,hashCode()仍然返回由内存地址生成的一个整数,new了不同的对象,其值也不同,就可以放进hashset了
ps:楼主是不是贴的不全啊,定义name前的String,还有if( !o instanceof Person ) return false;是不是应该为if( !(o instanceof Person )) return false;?
A选项如果去掉because后面的就对了,呵呵