学习笔记---使用override注解
2014-08-10 17:51
204 查看
在学习或者做东西,都懒得动手记,得习惯记下来才好...
最近在看《Effective Java》,确实对我有很大的提高,把自己看到和想到的东西写下来,标记...
1. 坚持使用override注解.
View Code
这是其中的一段例子.
结果很有意思,为什么是260而不是26呢?
bigrams采用的集合类型是HashSet,为什么没有去掉重复的元素呢?
看看HashSet的add方法的底层说明:3~5行说明了add元素e的条件:set中不存在元素e2( e==null;e2==null;e.equals(e2) )
在元素都不为null的情况下,说明 这行代码 e.equals(e2) 与我们想要的结果不一样.
那么再来看看源代码中,为什么代码中equals没有起到我们想要的作用呢?
这是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注解.
最近在看《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注解.
相关文章推荐
- Spring学习笔记(15)----使用Spring注解方式管理事务
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- 安卓手机卫士学习笔记系列——两个Activity之间切换动画效果overridependingtransition的使用
- spring in action 学习笔记(九)使用注解进行装配
- J2EE系列之MyBatis学习笔记(十)-- 使用注解配置sql映射器
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- Spring学习笔记之---注解的使用
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
- Spring 学习笔记(八)—— 注解使用整合
- 学习笔记-----------------Hibernian基础配置(注解的使用和主键生成策略)
- Spring学习笔记(17)----使用Spring注解方式管理事务
- 学习笔记-----Spring_MVC注解的使用
- MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- 【KK学习笔记】c# 虚方法 virtual 、 重写方法 override 、new 的使用与理解
- spring学习笔记(12)——使用注解方式配置bean
- 【Hibernate学习笔记-5】@Formula注解的使用
- J2EE系列之MyBatis学习笔记(十二)-- 使用注解配置sql映射器
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller