@Builder@SuperBuilder ---- Lombok篇

@Builder/@SuperBuilder ---- Lombok篇

Lombok中的@Builder注解的使用

@Builder 该注解主要作用与实体类,使用建造者模式来创建对象

需要注意的是注解 @Builder 与 @NoArgsConstructor 相互冲突,导致lombok不存在无参构造器,可参考下面的源码解析

//使用样例
@Data
@Builder
public class Student {

    private String name;

    private String sex;

    private Integer age;
}

//调用Student样例
public class Helloword {

    public String run(String str) {
        Student student = Student.builder().name("张无忌").age(23).sex("男").build();
        return student.toString();
    }
}
//这里贴出Student完整的源码,可以作为参考
//提示:和注解@Builder使用有关的内容可以只看源码中的注释部分

package com.atguigu.boot.entity;

public class Student {
    private String name;
    private String sex;
    private Integer age;

    //静态builder()方法 返回对象StudentBuilder
    public static Student.StudentBuilder builder() {
        return new Student.StudentBuilder();
    }

    public String getName() {
        return this.name;
    }

    public String getSex() {
        return this.sex;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setSex(final String sex) {
        this.sex = sex;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Student)) {
            return false;
        } else {
            Student other = (Student)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$age = this.getAge();
                    Object other$age = other.getAge();
                    if (this$age == null) {
                        if (other$age == null) {
                            break label47;
                        }
                    } else if (this$age.equals(other$age)) {
                        break label47;
                    }

                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                Object this$sex = this.getSex();
                Object other$sex = other.getSex();
                if (this$sex == null) {
                    if (other$sex != null) {
                        return false;
                    }
                } else if (!this$sex.equals(other$sex)) {
                    return false;
                }

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $age = this.getAge();
        int result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        return result;
    }

    public String toString() {
        return "Student(name=" + this.getName() + ", sex=" + this.getSex() + ", age=" + this.getAge() + ")";
    }

    //注意这里有且只有完整的有参构造器
    public Student(final String name, final String sex, final Integer age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    //自动构建**Builder静态内部类,这样做的原因是可以直接通过 className.attribute() 方式放入属性
    public static class StudentBuilder {
        private String name;
        private String sex;
        private Integer age;

        StudentBuilder() {
        }

        public Student.StudentBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public Student.StudentBuilder sex(final String sex) {
            this.sex = sex;
            return this;
        }

        public Student.StudentBuilder age(final Integer age) {
            this.age = age;
            return this;
        }

        //重要方法,最终调用StudentBuilder.build()返回对象Student
        public Student build() {
            return new Student(this.name, this.sex, this.age);
        }

        public String toString() {
            return "Student.StudentBuilder(name=" + this.name + ", sex=" + this.sex + ", age=" + this.age + ")";
        }
    }
}

@SuperBuilder注解的使用

注解@Builder只能实现当前类的创建,当子类继承父类时,如果子类需要使用父类属性进行构建时,那么该注解就无法实现,而@SuperBuilder就是为了解决这个问题的

//使用方法如下,此时Student可正常按照上面示例进行构建
@Data
@SuperBuilder(toBuilder = true)
public class Class {

    public String ClassName;

    public String remark;
    
    @Data
    @SuperBuilder(toBuilder = true)
    @AllArgsConstructor
    public class Student extends Class {

         public String name;

         public String sex;

         public Integer age;
    }

}

需要注意的是:注解@SuperBuilder的属性toBuilder要设置为true,因为默认是false,且父类和子类都需要注解@SuperBuilder(toBuilder = true)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值