当前位置: 技术问答>java相关
初学者问问题,,请各位帮帮忙啊。
来源: 互联网 发布时间:2015-10-25
本文导语: 重载equals方法,代码如下: class Employee { //…… public boolean equals(Object otherObject) { if (this==otherObject) return true; if (otherObject==null) return false; if (getClass()!=otherObject.getClass()) return false; Employee other=...
重载equals方法,代码如下:
class Employee
{
//……
public boolean equals(Object otherObject)
{
if (this==otherObject) return true;
if (otherObject==null) return false;
if (getClass()!=otherObject.getClass()) return false;
Employee other=(Employee)otherObject;
return name.equals(other.name)&&salary==other.salary&&hireDay.equals(other.hireDay);
}
}
其中Employee类中有基本类型字段salary和对象字段name,hireDay
小弟想问的是在java中"=="进行了什么样的比较?比如"this==otherObject"进行了什么样的比较?
还有就是如果调用此方法与一个Employee的子类比较的话,这里"getClass()
!=otherObject.getClass()"会返回什么?
谢谢各位!
class Employee
{
//……
public boolean equals(Object otherObject)
{
if (this==otherObject) return true;
if (otherObject==null) return false;
if (getClass()!=otherObject.getClass()) return false;
Employee other=(Employee)otherObject;
return name.equals(other.name)&&salary==other.salary&&hireDay.equals(other.hireDay);
}
}
其中Employee类中有基本类型字段salary和对象字段name,hireDay
小弟想问的是在java中"=="进行了什么样的比较?比如"this==otherObject"进行了什么样的比较?
还有就是如果调用此方法与一个Employee的子类比较的话,这里"getClass()
!=otherObject.getClass()"会返回什么?
谢谢各位!
|
==直接比较两个句柄所指向的对象的内存地址;
如果调用此方法与一个Employee的子类比较的话,应该是返回false;
如果调用此方法与一个Employee的子类比较的话,应该是返回false;
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。