Lombok---@EqualsAndHashCode(callSuper = true)的使用

package com.xiaobu.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2020/9/1 9:24
 * @description
 */
@Data
public class Father implements Serializable {

    private static final long serialVersionUID = -3605840195099107460L;
    private int age;

    private String name;



}

package com.xiaobu.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2020/9/1 9:24
 * @description
 */
@Data
public class Son  extends Father implements Serializable {

    private static final long serialVersionUID = -3605840195099107460L;
    private String address;



    public static void main(String[] args) {
        Son son1 = new Son();
        son1.setAge(1);
        son1.setName("laowang");
        son1.setAddress("shenzhen");
        Son son2 = new Son();
        son2.setAge(2);
        son2.setName("laoliu");
        son2.setAddress("shenzhen");
        System.out.println("son2 .equals(son1) " + son2 .equals(son1) );
    }
}

结果为true。

1599011252(1).png

在Son类添加 @EqualsAndHashCode(callSuper = true) 默认为false。

可以看出没有@EqualsAndHashCode(callSuper = true)的Son类编译后的equals()方法

  public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Son)) {
            return false;
        } else {
            Son other = (Son)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$address = this.getAddress();
                Object other$address = other.getAddress();
                if (this$address == null) {
                    if (other$address != null) {
                        return false;
                    }
                } else if (!this$address.equals(other$address)) {
                    return false;
                }

                return true;
            }
        }
    }

有@EqualsAndHashCode(callSuper = true)的Son类编译后的equals()方法

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Son)) {
            return false;
        } else {
            Son other = (Son)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (!super.equals(o)) {
                return false;
            } else {
                Object this$address = this.getAddress();
                Object other$address = other.getAddress();
                if (this$address == null) {
                    if (other$address != null) {
                        return false;
                    }
                } else if (!this$address.equals(other$address)) {
                    return false;
                }

                return true;
            }
        }
    }

可以看出来后者多了个

else if (!super.equals(o)) {
                          return false;
                      }

拓展:

Data 注解生成的 toString 方法也只包含了子类自有属性。
解决方案一样,加上 @ToString(callSuper = true) 注解,其实这里真正重要的是注解中的属性,callSuper = true,加上注解后打印结果如下:

参考:

lombok——@EqualsAndHashCode(callSuper = true)注解的和exclude使用

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值