码迷,mamicode.com
首页 > 其他好文 > 详细

重写equals方法

时间:2016-06-30 23:09:27      阅读:117      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!