Overriding equals() of Object (Java)
来源:互联网 发布:看电视直播的软件 编辑:程序博客网 时间:2024/06/06 01:47
Today I encoutered a problem to compare to user-defined instances for value equavelence.
It is necessary to override the equals() method derived from System.Object.
I tried to make it as simple as
public boolean equals(userDefinedClass p){ return this.v1==p.v1&&this.v2==p.v2;}
but it refuses to work.
After carefully explored the context, I found 2mistakes:
1. "==" is used for reference compare, which is against my intention.
2. this function is not overriding the derived equals(), but a overload with different parameters.
So, this works
public boolean equals(Object q){ userDefinedClass p = (userDefinedClass)q; return this.v1.equals(p.v1)&&this.v2.equals(p.v2);}
阅读全文
0 0
- Overriding equals() of Object (Java)
- Java Object equals() & hashCode()
- JAVA Object类-equals()
- Java object equals hashcode
- Java Object equals方法
- java equals(object obj)
- java overriding
- The difference of overriding between C# and Java
- Why always override hashcode() if overriding equals()?
- Overriding toString(), hashCode() and equals() Methods
- Obey the general contract when overriding equals
- Java Basic 4:object Orientation,Overloading,Overriding,Constructors and Return types
- Method of Overriding and Overloading
- Java中Object.equals与String.equals的区别
- java中Object.equals()和==以及字符串.equals()
- 重写Object类中的Equals方法(Java)
- java 的Object的equals方法
- java Object 的equals方法的复写
- 广度优先遍历 C#
- Nosql学习之路(一)
- JVM GC调优一则--增大Eden Space提高性能
- 设计一个鼠标点击速度比赛游戏。 *(1)显示一个按钮和文本框。 *(2)文本框显示鼠标点击次数
- 《穿越计算机的迷雾》第二版说明
- Overriding equals() of Object (Java)
- Oracle 存储过程内创建存储过程、函数
- 实现顺序表多种操作(removeall、冒泡排序等)
- 网址
- nginx安装配置
- Effective C++之四:设计与声明
- CarbonData编译、安装和集成Spark 2.2
- 可持久化并查集
- 消息队列使用的四种场景介绍