美文网首页
继承关系中子类使用@Data注解问题

继承关系中子类使用@Data注解问题

作者: smallAttr | 来源:发表于2018-09-29 16:50 被阅读0次

HashSet中使用@Data注解问题

平时习惯使用lombok工具,免去了我们写getset方法之类的,当然了,我们使用@Data注解后,equals()hashCode()toString() 也省却了。但是当你代码存在继承关系时,就得留心结果是否是你想要的了?

下面我直接列举个例子吧:

父类:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Tag {

  private Long id;

  private String tagName;

  private String position;
}

子类:

@Data
@NoArgsConstructor
public class UserTag extends Tag {

  private Long userId;

  public UserTag(Long id, String tagName, String position, Long userId) {
    super(id, tagName, position);
    this.userId = userId;
  }
}

其实关系就这么easy,最后我们test来说明问题

public class UserTagTest {

  public static void main(String[] args) {
    UserTag firstUserTag = new UserTag(1L, "tag1", "up", 2000L);
    UserTag secondUserTag = new UserTag(2L, "tag2", "down", 2000L);

    Set<Tag> tagSet = new HashSet<>();
    boolean firstAdd = tagSet.add(firstUserTag);
    boolean secondAdd = tagSet.add(secondUserTag);

    System.out.println("firstAdd:" + firstAdd);
    System.out.println("secondAdd:" + secondAdd);
    System.out.println("tagSet size:" + tagSet.size());
  }
}

运行实际结果:

firstAdd:true
secondAdd:false
tagSet size:1

当看着实际结果和预期结果不同,当然了,很容易就想到是equals()hashCode()的问题。最后我反编译看着@Data帮我们生成的equals()hashCode(),如下:

public boolean equals(Object o) {
    if (o == this) {
      return true;
    } else if (!(o instanceof UserTag)) {
      return false;
    } else {
      UserTag other = (UserTag)o;
      if (!other.canEqual(this)) {
        return false;
      } else {
        Object this$userId = this.getUserId();
        Object other$userId = other.getUserId();
        if (this$userId == null) {
          if (other$userId != null) {
            return false;
          }
        } else if (!this$userId.equals(other$userId)) {
          return false;
        }

        return true;
      }
    }
  }

  protected boolean canEqual(Object other) {
    return other instanceof UserTag;
  }

  public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $userId = this.getUserId();
    int result = result * 59 + ($userId == null ? 43 : $userId.hashCode());
    return result;
  }

实际上只比较了userId,说到这,得到上面的运行结果也就很正常了。

相关文章

  • 继承关系中子类使用@Data注解问题

    HashSet中使用@Data注解问题 平时习惯使用lombok工具,免去了我们写get、set方法之类的,当然了...

  • 《Java编程的逻辑》笔记15--初识继承和多态

    继承 继承概念(1)计算机程序经常使用类之间的继承关系来表示对象之间的分类关系(2)在继承关系中,有父类和子类,父...

  • Java-----继承

    1、什么是继承? 继承就是让类与类之间产生关系(子父类关系),子类可以直接使用父类中的非私有成员。注意:子类不能使...

  • 创建型模式-抽象工厂

    使用场景:继承,创建各种子类对象,多种继承关系 意义: 隐藏了选择子类、创建子类对象的过程,隐藏各对象间的相互关系...

  • 注解继承

    接口注解不能被实现类继承。接口注解不能被子接口继承。父类接口能被子类接口继承,注解需要被@Inherited元注解...

  • @PostConstruct注解的继承问题

    子类重写父类带有@PostConstruct注解的方法,子类方法可以继承到@PostConstruct注解的效果。...

  • 《每天进步一点点》DAY4:面向对象

    关于继承 继承使用场景:存在is a关系 子类只能继承父类所有的非私有成员(成员变量和成员方法) 子类不能继承父类...

  • 面向对象-继承

    继承思想 继承extents 继承子类与父类的关系 public 子类都可以继承protected 子类可以继承,...

  • 创建型模式-工厂方法

    使用场景:继承,创建各种子类对象,一种继承关系 意义: 隐藏了选择子类、创建子类对象的过程,统一创建接口 原理描述...

  • Spring Boot中Spring data注解的使用

    Spring Boot中Spring data注解的使用 Sring data JPA为我们提供了很多有用的注解,...

网友评论

      本文标题:继承关系中子类使用@Data注解问题

      本文链接:https://www.haomeiwen.com/subject/gxdboftx.html