equals复写

daomingxiong 2015-03-09 11:20:15
public boolean equals(object o){
if(this==o) return ture;
if(!o instanceof Monkey)
return ture;
final Monkey other=(Monkey)o;
if(this.name.equals(other.name)
//1为什么这里other可以直接访问name属性(name已封装)

return ture;
else
return false;
}
Monkey m1=new Monkey();
Monkey m2=new Monkey();
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
骆宏 2015-03-10
  • 打赏
  • 举报
回复
如果不熟悉equals,hashCode,直接使用IDE自带生成这两个方法吧...
普凡 2015-03-10
  • 打赏
  • 举报
回复
[code=java ]if(!o instanceof Monkey) return ture; [/code] 这么写法是不是有问题? 这样?

if (obj instanceof Monkey) {
            return name.equals(o.name);
        }
        return false;
rumlee 2015-03-10
  • 打赏
  • 举报
回复
在当前类中是可以访问私有属性的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧