码迷,mamicode.com
首页 > 其他好文 > 详细

学习笔记---使用override注解

时间:2014-08-10 18:36:10      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   使用   os   for   

在学习或者做东西,都懒得动手记,得习惯记下来才好...

最近在看《Effective Java》,确实对我有很大的提高,把自己看到和想到的东西写下来,标记...

1. 坚持使用override注解.

bubuko.com,布布扣
 1 public class Bigram {
 2     
 3     private final char first;
 4     private final char second;
 5     public Bigram(char first,char second) {
 6         this.first = first;
 7         this.second = second;
 8     }
 9     
10     public boolean equals(Bigram bigram) {
11         return bigram.first == first && bigram.second == second;
12     }
13     
14     public int hashCode() {
15         return 31*first + second;
16     }
17     
18     public static void main(String[] args) {
19         Set<Bigram> bigrams = new HashSet<Bigram>();
20         for(int i=0;i<10;i++){
21             for(char ch=‘a‘;ch<=‘z‘;ch++){
22                 bigrams.add(new Bigram(ch, ch));
23             }
24         }
25         System.out.println(bigrams.size());
26     }
27 
28 }
View Code

这是其中的一段例子.

260

结果很有意思,为什么是260而不是26呢?

bigrams采用的集合类型是HashSet,为什么没有去掉重复的元素呢?

 1     /**
 2      * Adds the specified element to this set if it is not already present.
 3      * More formally, adds the specified element <tt>e</tt> to this set if
 4      * this set contains no element <tt>e2</tt> such that
 5      * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
 6      * If this set already contains the element, the call leaves the set
 7      * unchanged and returns <tt>false</tt>.
 8      *
 9      * @param e element to be added to this set
10      * @return <tt>true</tt> if this set did not already contain the specified
11      * element
12      */
13     public boolean add(E e) {
14     return map.put(e, PRESENT)==null;
15     }

看看HashSet的add方法的底层说明:3~5行说明了add元素e的条件:set中不存在元素e2( e==null;e2==null;e.equals(e2) )

在元素都不为null的情况下,说明 这行代码 e.equals(e2) 与我们想要的结果不一样.

那么再来看看源代码中,为什么代码中equals没有起到我们想要的作用呢?

1     public boolean equals(Object obj) {
2     return (this == obj);
3     }

这是Object类中的equals方法,仔细看,代码中原本想要重写equals方法,可是却重载了equals方法.

这样的话,在进行这行代码( e.equals(e2) )时,就调用了Object类的equals方法,而Object类的equals方法是比较对象同一性的,即( e==e2 )

例子中:这行代码:bigrams.add(new Bigram(ch, ch)); 每次添加都会new Bigram(ch,ch),每一个Object都不一样,自然循环多少次,就add多少Object了。

 

而如果加了注解@override,现代的IDE就会报错:The method equals(Bigram) of type Bigram must override or implement a supertype method

所以呢,应该在你想要覆盖父类声明的每个方法声明中使用override注解.

 

学习笔记---使用override注解,布布扣,bubuko.com

学习笔记---使用override注解

标签:style   blog   http   color   java   使用   os   for   

原文地址:http://www.cnblogs.com/guyuexia/p/3903149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!