先看问题,如图所示
注解解释
@equalsandhashcode 作用与子类上
callsuper = true
,根据子类自身的字段值和从父类继承的字段值来生成hashcode,当两个子类对象比较时,只有子类对象的本身的字段值和继承父类的字段值都相同,equals方法的返回值是true。callsuper = false
,根据子类自身的字段值来生成hashcode, 当两个子类对象比较时,只有子类对象的本身的字段值相同,父类字段值可以不同,equals方法的返回值是true。
idea检查提示并快速修复
正在生成 equals/hashcode 实现,但即使此类未扩展 java.lang.object,也不调用超类。如果这是有意为之,请在您的类型中添加 ‘(callsuper=false)’。
检查信息: 提供 lombok 注解的一般检查。
点击后,快速修复添加注解
测试代码
package com.st; import lombok.allargsconstructor; import lombok.data; import lombok.equalsandhashcode; import lombok.noargsconstructor; import org.junit.test; /** * @author st * @date 2024/1/9 15:36 */ @data @noargsconstructor @allargsconstructor class animal { private string name; } @equalsandhashcode(callsuper = true) @data @noargsconstructor @allargsconstructor class cat extends animal { private string age; public cat(string name, string age) { super(name); this.age = age; } } public class testannotation { @test public void testcat() { cat cat1 = new cat("1", "1"); cat cat2 = new cat("2", "1"); system.out.println(cat1.equals(cat2)); } }
到此这篇关于springboot 注解equalsandhashcode的文章就介绍到这了,更多相关springboot 注解equalsandhashcode内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论