视频里马士兵问道:Object类里面equals方法是怎么实现的?
一听我也懵了,因为一般都是重写了equals方法?
是怎么实现的呢?
一听我也懵了,因为一般都是重写了equals方法?
是怎么实现的呢?
解决方案 »
- 求助,没搞明白怎么就是空值了
- 请教大家一个问题,关于java连接2台机器的
- 谁能帮忙解决这个DATE的问题,比较有挑战,急,谢谢!
- 请问大家:这个程序只有几行,书上抄的,编译出错,问题在哪?
- drawRect()的小问题!
- 看了很多关于JAVA打印的问题,都是讨论分页打印的,不过我想做一个打印现金出入帐的程序,不要分页打印的,只是简单把每一笔流水帐都打印出来
- Mr karma: Another question?
- jbuilder4(答者加分!)
- 请教有关JMF的问题.我把仅有的20分相送!
- 定时 每天早上08:00执行
- java 串口通信,在main()能正常调用,在project 程序中调用,显示找不到端口的问题
- 泛型比较的问题
设有两个Object o1,o2
o1.equals(o2)==true 意味着o1和o2指向同一个实例对象(或者说有同样的内存地址)
这时候 o1.equals(o2) == (o1==o2)
只是这个比较的是内存地址
这样一来,如果是两个自定义类对象之间的比较,就是False了
是的,所以我们如果要用Equals,需要重现Equals方法。
equals是在Object类里的一个方法。而所有的类都是Object的子类,都会继承equals方法,但是在Object类中equals方法定义的是比较两个地址,
子类中要比较的都不是两个地址而是程序员想比较的属性。所以要重写,比较方法。
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The <code>equals</code> method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* <code>x</code>, <code>x.equals(x)</code> should return
* <code>true</code>.
* <li>It is <i>symmetric</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
* should return <code>true</code> if and only if
* <code>y.equals(x)</code> returns <code>true</code>.
* <li>It is <i>transitive</i>: for any non-null reference values
* <code>x</code>, <code>y</code>, and <code>z</code>, if
* <code>x.equals(y)</code> returns <code>true</code> and
* <code>y.equals(z)</code> returns <code>true</code>, then
* <code>x.equals(z)</code> should return <code>true</code>.
* <li>It is <i>consistent</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, multiple invocations of
* <tt>x.equals(y)</tt> consistently return <code>true</code>
* or consistently return <code>false</code>, provided no
* information used in <code>equals</code> comparisons on the
* objects is modified.
* <li>For any non-null reference value <code>x</code>,
* <code>x.equals(null)</code> should return <code>false</code>.
* </ul>
* <p>
* The <tt>equals</tt> method for class <code>Object</code> implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values <code>x</code> and
* <code>y</code>, this method returns <code>true</code> if and only
* if <code>x</code> and <code>y</code> refer to the same object
* (<code>x == y</code> has the value <code>true</code>).
* <p>
* Note that it is generally necessary to override the <tt>hashCode</tt>
* method whenever this method is overridden, so as to maintain the
* general contract for the <tt>hashCode</tt> method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return <code>true</code> if this object is the same as the obj
* argument; <code>false</code> otherwise.
* @see #hashCode()
* @see java.util.Hashtable
*/
public boolean equals(Object obj) {
return (this == obj);
}Object类里写的很清楚
你想比较什么就在里面比较什么public boolean equals(Object obj) {
if(obj.age==23 && obj.name == "zhangsan") return true;
else return false;
}