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 + ")";
            }
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jayues_lies

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值