首页 > 其他 > 详细

重写equals方法

时间:2016-06-30 23:08:28      阅读:178      评论:0      收藏:0      [点我收藏+]

  用下面的例子来进行解释。

  

    String name;
    int id;

    @Override
    public boolean equals(Object otherObject) {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (this.getClass() != otherObject.getClass())
            return false;
        if(!(otherObject instanceof father))//father 代表它们的父类,如果子类都具有同样的语义可以使用这种方法。
            return false;
        Test test = (Test) otherObject;
        return this.name.equals(test.name) && this.id == test.id;
    }

  

重写equals方法

原文:http://www.cnblogs.com/benniaoxuefei/p/5631434.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!