您的位置:首页 > 其它

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

2014-08-10 17:51 204 查看
在学习或者做东西,都懒得动手记,得习惯记下来才好...

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

1. 坚持使用override注解.

public class Bigram {

private final char first;
private final char second;
public Bigram(char first,char second) {
this.first = first;
this.second = second;
}

public boolean equals(Bigram bigram) {
return bigram.first == first && bigram.second == second;
}

public int hashCode() {
return 31*first + second;
}

public static void main(String[] args) {
Set<Bigram> bigrams = new HashSet<Bigram>();
for(int i=0;i<10;i++){
for(char ch='a';ch<='z';ch++){
bigrams.add(new Bigram(ch, ch));
}
}
System.out.println(bigrams.size());
}

}


View Code
这是其中的一段例子.

260


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

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

/**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}


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

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

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

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


这是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注解.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: