黑马程序员技术交流社区

标题: 我说为何new出两个内容一样的类。equals不能相等。。原来。。 [打印本页]

作者: poi1234bnm    时间: 2016-11-6 13:48
标题: 我说为何new出两个内容一样的类。equals不能相等。。原来。。
@Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                People other = (People) obj;
                if (age != other.age)
                        return false;
                if (name == null) {
                        if (other.name != null)
                                return false;
                } else if (!name.equals(other.name))
                        return false;
                return true;
        }

这里this == obj就否决new出的两个不同对象不能相等了!!!

作者: ylz1997    时间: 2016-11-6 14:46
如果没重写的话默认的是继承object  比较地址值吧

作者: poi1234bnm    时间: 2016-11-6 14:57
ylz1997 发表于 2016-11-6 14:46
如果没重写的话默认的是继承object  比较地址值吧

这是用快捷键直接生成这个类的hashCode和equals方法。没改过。。this == obj直接比较地址。。
作者: Yeauty    时间: 2016-11-6 15:32
没重写equals






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2