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

Effective java 对像引述和hashcode和equals方法实现

2012-09-05 
Effective java 对像引用和hashcode和equals方法实现1:是考虑用static 来代替构造方法,通过私有构造器强化

Effective java 对像引用和hashcode和equals方法实现
1:是考虑用static 来代替构造方法,通过私有构造器强化不可实例化的能力
public class test{
pirvate  test(){
throw new AssertionError();
}
}
一般可用在工具类里面一般可都是static,
2:避免创建不必要的对像
String s=new String("sql");
改进后的版本可以:String s="sql";
重写:
public boolean equals (Object o){
if(o==this){
return true;
}if(!(0 instanceof Test)){
return false;
}
Test ts=(Test)o;
return ts.id=id&&ts.name=name;
}

pubilc int hashcode(){
int result=17;
result=31*result+id //如果是int类型直接int,如果是long .intval(),如果是float 计算Float.floatToIntBits(id),如果是double直接转换成:Double.doubleToLongbits(id);String 直接化成int类型。
boolean 计算(f?1:0)

}

热点排行