• Lombok中的@Builder注解的使用


    Lombok中的@Builder注解的使用

    作用

    • @Builder注解的作用主要是用来生成对象,并且可以为对象链式赋值。

    引入依赖

    • 因为@Builder注解是lombok中的东西,所以第一步我们需要引入lombok的依赖,如下图:
    • 在这里插入图片描述

    第二步给实体类加上@Builder注解

    • 第二步我们需要给我们的实体类加上一个@Builder注解,如下图:
    • 在这里插入图片描述

    第三步使用测试使用@Builder注解生成对象

    • 在这里插入图片描述

    实体类加上@Builder注解之后的编译结果

    • 实体类加上@Builder注解之后,编译之后会多出一个builder()方法,和一个CardBuilder静态内部类,如下图:

    • 在这里插入图片描述

    • 在这里插入图片描述

    • 代码如下:

    • public class Card {
          private int id;
          private String name;
          private boolean sex;
      
          public static Card.CardBuilder builder() {
              return new Card.CardBuilder();
          }
      
          public Card(int id, String name, boolean sex) {
              this.id = id;
              this.name = name;
              this.sex = sex;
          }
      
          public Card() {
          }
      
          public int getId() {
              return this.id;
          }
      
          public String getName() {
              return this.name;
          }
      
          public boolean isSex() {
              return this.sex;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public void setSex(boolean sex) {
              this.sex = sex;
          }
      
          public boolean equals(Object o) {
              if (o == this) {
                  return true;
              } else if (!(o instanceof Card)) {
                  return false;
              } else {
                  Card other = (Card)o;
                  if (!other.canEqual(this)) {
                      return false;
                  } else if (this.getId() != other.getId()) {
                      return false;
                  } else {
                      Object this$name = this.getName();
                      Object other$name = other.getName();
                      if (this$name == null) {
                          if (other$name == null) {
                              return this.isSex() == other.isSex();
                          }
                      } else if (this$name.equals(other$name)) {
                          return this.isSex() == other.isSex();
                      }
      
                      return false;
                  }
              }
          }
      
          protected boolean canEqual(Object other) {
              return other instanceof Card;
          }
      
          public int hashCode() {
              int PRIME = true;
              int result = 1;
              int result = result * 59 + this.getId();
              Object $name = this.getName();
              result = result * 59 + ($name == null ? 43 : $name.hashCode());
              result = result * 59 + (this.isSex() ? 79 : 97);
              return result;
          }
      
          public String toString() {
              return "Card(id=" + this.getId() + ", name=" + this.getName() + ", sex=" + this.isSex() + ")";
          }
      
          public static class CardBuilder {
              private int id;
              private String name;
              private boolean sex;
      
              CardBuilder() {
              }
      
              public Card.CardBuilder id(int id) {
                  this.id = id;
                  return this;
              }
      
              public Card.CardBuilder name(String name) {
                  this.name = name;
                  return this;
              }
      
              public Card.CardBuilder sex(boolean sex) {
                  this.sex = sex;
                  return this;
              }
      
              public Card build() {
                  return new Card(this.id, this.name, this.sex);
              }
      
              public String toString() {
                  return "Card.CardBuilder(id=" + this.id + ", name=" + this.name + ", sex=" + this.sex + ")";
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
  • 相关阅读:
    WordPress 博客实现登录后台邮件提醒的教程
    vue3中插槽,计算属性,路由(addRoutes()白屏),表单,vuex 笔记,localStorage和sessionStorage语法
    ISCC-2022 部分wp
    业务层、过滤器
    ESDA in PySal (5):空间数据的探索性分析:空间自相关
    Idea、VsCode、WebStorm常用插件
    打破焦虑!AI 时代的程序员为什么需要云端 IDE?
    Python 逗号的巧用
    08_面向对象高级_枚举
    LeetCode 1470. 重新排列数组:考研扣两分的做法
  • 原文地址:https://blog.csdn.net/Andrew_Chenwq/article/details/132893887