打字猴:1.70044226e+09
1700442260
1700442261 if(p.getName()==null||name==null){
1700442262
1700442263 return false;
1700442264
1700442265 }else{
1700442266
1700442267 return name.equalsIgnoreCase(p.getName());
1700442268
1700442269 }
1700442270
1700442271 }
1700442272
1700442273 return false;
1700442274
1700442275 }
1700442276
1700442277 当然,考虑到Employee也有可能被继承,也需要把它的instanceof修改为getClass。总之,在覆写equals时建议使用getClass进行类型判断,而不要使用instanceof。
1700442278
1700442279
1700442280
1700442281
1700442282 编写高质量代码:改善Java程序的151个建议 [:1700438117]
1700442283 编写高质量代码:改善Java程序的151个建议 建议48:覆写equals方法必须覆写hashCode方法
1700442284
1700442285 覆写equals方法必须覆写hashCode方法,这条规则基本上每个Javaer都知道,这也是JDK API上反复说明的,不过为什么要这样做呢?这两个方法之间有什么关系呢?本建议就来解释该问题,我们先来看如下代码:
1700442286
1700442287 public static void main(String[]args){
1700442288
1700442289 //Person类的实例作为Map的key
1700442290
1700442291 Map<Person, Object>map=new HashMap<Person, Object>(){
1700442292
1700442293 {
1700442294
1700442295 put(new Person(“张三”),new Object());
1700442296
1700442297 }
1700442298
1700442299 };
1700442300
1700442301 //Person类的实例作为List的元素
1700442302
1700442303 List<Person>list=new ArrayList<Person>(){
1700442304
1700442305 {
1700442306
1700442307 add(new Person(“张三”));
1700442308
1700442309 }
[ 上一页 ]  [ :1.70044226e+09 ]  [ 下一页 ]