CODE ONE:
class Uer{
public static void main(String args[]){
System.out.println(s.hashCode());
}
private static String s="me";
private static int n=11;
public int hashCode()
{
return n*s.hashCode();
}
} 我在注释掉重写的hashCode方法和没有注释掉之前,两次运行的输出怎么都是一样的?
============================
CODE TWO:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
} 这是equals方法的重写,我想问下int n=count;这句的意思,我单纯写了这个句子,编译错误。
class Uer{
public static void main(String args[]){
System.out.println(s.hashCode());
}
private static String s="me";
private static int n=11;
public int hashCode()
{
return n*s.hashCode();
}
} 我在注释掉重写的hashCode方法和没有注释掉之前,两次运行的输出怎么都是一样的?
============================
CODE TWO:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
} 这是equals方法的重写,我想问下int n=count;这句的意思,我单纯写了这个句子,编译错误。
解决方案 »
- ----------------------echo 写文件,超级简单简单
- 两个double型变量相乘和相除的问题
- [求救] JAVA 分组
- jni加载dll库时,环境变量设定问题
- 问个开源GIS开发组件?
- 关于postEvent()对awt和swing组件的差别疑惑,求教高手,谢谢
- 如何实现 服务器端图片传输及客户端的接收显示啊, 高手请指示啊
- 怎么把数据库里面的图片在jsp里面取出来?????
- 没事进来看看!来者不一定有分!
- Exception in thread "main" java.lang.NoClassDefFoundError:d:\java\helloworld是怎么回事
- java.lang.NOClassDefFondError
- 说说虚拟机的相关知识点--适合初学者
一般,hashCode和static字段无关。
你不要照抄String的方法。你没法访问String的private字段。
s.hashCode()调用的就是他本身的hashcode()算法。你重写的是Usr类的hashCode。
class Uer {
public static void main(String args[]) { System.out.println(Uer.hashCode(s));//调用下面的hashCode,当然现在不是重写类的hashcode
System.out.println(s.hashCode());//这个调用的s对象自己的hashcode
} private static String s = "me";
private static int n = 11; public static int hashCode(String s) {
return n * s.hashCode();
} @Override
public int hashCode() {
return n * s.hashCode();
}}
import java.util.Date;class Dog{
private String name;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Cat { /**Cat类中含有name和birthday两私有成员变量,且重写了equals方法和hashCode方法
*
* @param name 和 birthday
*
*/
private String name;
private Date birthday;
public Cat(){}
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/*
* 重写equals必须注意:
* 1 自反性:对于任意的引用值x,x.equals(x)一定为true
* 2 对称性:对于任意的引用值x 和 y,当x.equals(y)返回true,y.equals(x)也一定返回true
* 3 传递性:对于任意的引用值x、y和z,如果x.equals(y)返回true,并且y.equals(z)也返回true,那么x.equals(z)也一定返 回 true
* 4 一致性:对于任意的引用值x 和 y,如果用于equals比较的对象信息没有被修改,
* 多次调用x.equals(y)要么一致地返回true,要么一致地返回false
* 5 非空性:对于任意的非空引用值x,x.equals(null)一定返回false
*
* 请注意:
* 重写equals方法后最好重写hashCode方法,否则两个等价对象可能得到不同的hashCode,这在集合框架中使用可能产生严重后果
*/
/*
* 1.重写equals方法修饰符必须是public,因为是重写的Object的方法.
* 2.参数类型必须是Object.
*/
public boolean equals(Object other){ //重写equals方法,后面最好重写hashCode方法
if(this == other) //先检查是否其自反性,后比较other是否为空。这样效率高
return true;
if(other == null)
return false;
if( !(other instanceof Cat))
return false;
final Cat cat = (Cat)other;
if( !getName().equals(cat.getName()))
return false;
if( !getBirthday().equals(cat.getBirthday()))
return false;
return true;
}
public int hashCode(){ //hashCode主要是用来提高hash系统的查询效率。当hashCode中不进行任何操作时,可以直接让其返回 一常数,或者不进行重写。
int result = getName().hashCode();
result = 29 * result +getBirthday().hashCode();
return result;
//return 0;
} public static void main(String[] args) {
Date dayA = new Date(4000000);
Cat a = new Cat();
a.setName("a");
a.setBirthday(dayA);
Date dayB = new Date(1000000);
Cat b = new Cat();
b.setName("a");
b.setBirthday(dayB);
Date dayC = dayA;
Cat c = new Cat();
c.setName("a");
c.setBirthday(dayC);
Date dayE = dayA;
Cat e = new Cat();
e.setName(a.getName());
e.setBirthday(a.getBirthday());
Date dayD = dayC;
Dog d = new Dog();
d.setName("a");
d.setBirthday(dayD);
System.out.println(a.equals(b)); //调用自己类中所定义的equals方法
System.out.println(a.equals(a));
System.out.println(a.equals(c));
System.out.println(d.equals(a));
System.out.println(a.equals(d)); //验证重写equals的对称性
System.out.println(a.equals(e));
System.out.println(e.equals(c)); //验证重写equals的传递性
System.out.println(d.getName().equals(a.getName())); //调用Object类中equals方法
System.out.println(d.getBirthday().equals(b.getBirthday()));
System.out.println("比较hanshCode的值");
/*
* * 比较hashCode方法中返回的值
* 如果equals返回为true,则hashCode一定返回true。
* 如果equals返回为false,hashCode返回值不一定不相同。
* 如果hashCode返回值不同,则equals返回值一定为false。
* 如果hashCode返回值不同,则equals返回值不一定为false。
*/
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(a.hashCode()== b.hashCode()); //如果equals返回false,则各hashCode不一定返回不同值
System.out.println(a.hashCode() == c.hashCode());
}}