首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

equals跟hasCode疑惑

2011-12-14 
equals和hasCode疑惑代码如下:packagetestimportjava.util.*finalclassstudent{Stringnameintagepubli

equals和hasCode疑惑
代码如下:
package   test;

import   java.util.*;

final   class   student   {
String   name;

int   age;

public   student(String   name,   int   age)   {
this.name   =   name;
this.age   =   age;
}

public   boolean   equals(Object   o)   {
if   (o   ==   this)
return   true;
if   (!(o   instanceof   student))
return   false;
student   s   =   (student)   o;
if   (s.name   ==   name   &&   s.age   ==   age)
return   true;
else
return   false;
}

public   int   hasCode()   {
return   name.hashCode()   +   age;
}

}

public   class   test1   {
public   static   void   main(String   args[])   {
Map   map   =   new   HashMap();
student   s1   =   new   student( "lwb ",   26);
student   s2   =   new   student( "lwb ",   26);
map.put(s1,   "s1 ");
System.out.println(map.get(s2));
}

}

已经重写了equals和hasCode,为什么运行结果为null呢?


[解决办法]
public int hashCode();
不是hasCode()

热点排行