zoukankan      html  css  js  c++  java
  • lombok——@EqualsAndHashCode(callSuper = true)注解的和exclude使用

    • 此注解会生成equals(Object other) 和 hashCode()方法。
    • 它默认使用非静态,非瞬态的属性
    • 可通过参数exclude排除一些属性
    • 可通过参数of指定仅使用哪些属性
    • 它默认仅使用该类中定义的属性且不调用父类的方法

    实现equals, hashCode方法是在编程生活中再常见不过的一个东西了,那么自然@EqualsAndHashCode 这个annotation就成为了一个非常方便的工具。默认情况下,被这个annotation标注的class会用到除了 static,transient修饰的所有属性作为判断标准,当然和之前的annotation一样,可是使用exclude选项除掉不想要的属性。也可以通过callSuper包含父类的equals 和 hashCode。 当然如果你的class 没有继承任何其他的class,你却写了callSuper,那么会收获一个编译报错。

    你只要这样用:

    Java代码  
    import lombok.EqualsAndHashCode;  
      
    @EqualsAndHashCode(exclude={"id", "shape"})  
    public class EqualsAndHashCodeExample {  
      private transient int transientVar = 10;  
      private String name;  
      private double score;  
      private Shape shape = new Square(5, 10);  
      private String[] tags;  
      private int id;  
        
      public String getName() {  
        return this.name;  
      }  
        
      @EqualsAndHashCode(callSuper=true)  
      public static class Square extends Shape {  
        private final int width, height;  
          
        public Square(int width, int height) {  
          this.width = width;  
          this.height = height;  
        }  
      }  
    }  

    @EqualsAndHashCode(callSuper = true)
    该注解用于子类对象之间进行比较的时候

    不加该注解的影响:子类对象属性值一致,但其继承的父类对象属性值不一致,在比较的时候会出现比较结果不对的情况。

    举个简单的例子:
    这边先定义一个分类对象 Parent,有一个属性:code

    @Data
    public class Parent {
    /**
    * 父类编码
    */
    private String code;
    }

    再定义一个子类对象 Child,一一个属性:name

    @Data
    public class Child extends Parent {
    /**
    * 子类名称
    */
    private String name;
    }

    在方法中 new 两个 Child 对象:childTest1、childTest2
    对这两个 Child 对象的自有属性 name 都赋值为:Child;但是对继承的父类属性 code 进行不同的赋值

    Child childTest1 = new Child();
    childTest1.setCode("1");
    childTest1.setName("child");

    Child childTest2 = new Child();
    childTest2.setCode("2");
    childTest2.setName("child");

    根据使用过程中,这两个对象肯定是不一样的,但是,在不加 @EqualsAndHashCode(callSuper = true) 注解的情况下对这两个对象进行比较得到的结果却是 true

    boolean isSame = Objects.equals(childTest1,childTest2);
    log.info("testEquals -> childTest1:{}, childTest2:{}", childTest1, childTest2);
    log.info("testEquals -> :{}", isSame);

    @EqualsAndHashCode(callSuper = true) 注解的作用就是将其父类属性也进行比较,下面是 Child 类加了注解后运行的结果:

    @EqualsAndHashCode(callSuper = true)
    @Data
    public class Child extends Parent {
    /**
    * 子类名称
    */
    private String name;
    }

    因为 @Data 生成的 equals 方法,只对该类里自有的属性进行了比较;

    下面看下加与不加注解的时候编译后的 Child 类
    (1)无 @EqualsAndHashCode(callSuper = true) 注解

    public boolean equals(Object o){
    if (o == this) {
    return true;
    }
    if (!(o instanceof Child)) {
    return false;
    }
    Child other = (Child)o;
    if (!other.canEqual(this)) {
    return false;
    }
    Object this$name = getName();Object other$name = other.getName();return this$name == null ? other$name == null : this$name.equals(other$name);
    }

    (2)有 @EqualsAndHashCode(callSuper = true) 注解

    public boolean equals(Object o){
    if (o == this) {
    return true;
    }
    if (!(o instanceof Child)) {
    return false;
    }
    Child other = (Child)o;
    if (!other.canEqual(this)) {
    return false;
    }
    if (!super.equals(o)) {
    return false;
    }
    Object this$name = getName();Object other$name = other.getName();return this$name == null ? other$name == null : this$name.equals(other$name);
    }

    对比一下,可以看到加了注解之后多了 super.equals 方法

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

    细心的朋友会发现,在用 log 打印两个对象的时候,toString 方法只打印了子类属性,隐藏了父类属性,这里其实和 equals 方法一样,@Data 注解生成的 toString 方法也只包含了子类自有属性。

    解决方案一样,加上 @ToString(callSuper = true) 注解,其实这里真正重要的是注解中的属性,callSuper = true,加上注解后打印结果如下:


    以上
    ————————————————
    版权声明:本文为CSDN博主「快看,飞天猪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_42888567/java/article/details/105145024

  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/xxl910/p/12877776.html
Copyright © 2011-2022 走看看