注解作用:一般用于实体类的子类,提供equals方法。后面的callsuper属性,继承父类的子类,如果callSuper = true,则表示继承的属性也要一致才能返回truecallSuper = false ,则表示继承的属性可以不一致

代码演示:

@Data
@EqualsAndHashCode(callSuper = true)
//继承父类的子类,如果callSuper = true,则表示继承的属性也要一致才能返回true
//callSuper = false ,则表示继承的属性可以不一致
public class Cat extends Animal {
    private String name;
    private int age;

    public Cat(String color, String name, int age) {
        super(color);
        this.name = name;
        this.age = age;
    }

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Animal {
    private String color;
}
public class Test {
    public static void main(String[] args) {
        Cat cat1 = new Cat("黑色","拉拉",2);
        Cat cat2 = new Cat("白色","拉拉",2);

        System.out.println(cat1.equals(cat2));//当callSuper属性为true时,返回false,反之则返回false

    }
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐