• java lombok框架


    @Data 的作用?

    使用这个注解,就不用再去手写Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在编译时会自动加进去。

    @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

    @NoArgsConstructor:生成无参构造函数
    @RequiredArgsConstructor:生成一个构造函数,并对打了@NonNull的参数进行NullPointer检查 (这里有坑?为啥?)
    例如:

    // 源代码
    @RequiredArgsConstructor(staticName = "of")
    class Car{
        @NonNull
        private String origin;
        @NonNull
        private String model;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 编译后代码
    class Car{
        @NonNull
        private String origin;
        @NonNull
        private String model;
        
        // 省略get、set方法
        private Car(@NonNull String origin, @NonNull String model) {
            if (origin == null) {
                throw new NullPointerException("origin is marked @NonNull but is null");
            } else {
                this.origin = origin;
            }
            if (model == null) {
                throw new NullPointerException("model is marked @NonNull but is null");
            } else {
                this.model = model;
            }
        }
        public static Car of(@NonNull String origin, @NonNull String model) {
            return new Car(origin, model);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    @AllArgsConstructor:生成全参构造函数

    @NoArgsConstructor will generate a constructor with no parameters. If
    this is not possible (because of final fields), a compiler error will
    result instead, unless @NoArgsConstructor(force = true) is used, then
    all final fields are initialized with 0 / false / null. For fields
    with constraints, such as @NonNull fields, no check is generated,so be
    aware that these constraints will generally not be fulfilled until
    those fields are properly initialized later. This annotation is useful
    primarily in combination with either @Data or one of the other
    constructor generating annotations.

    lombok还有什么常见用法?

    上面的@Data、@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor都是lombok的注解。
    其他还有:

    @builder有什么用?

    当一个类的属性很多,但构造时只需要其中几个属性时,更方便。

    @builder的原理是什么

    @builder自动实现了以下原生的builder模式

    class Car{
        private String origin;
        private String model;
    
        private Car(String model){
            this.model = model;
        }
    
        private Car(Builder builder){
            this.origin = builder.origin;
            this.model = builder.model;
        }
    
        public static Car of(String model){
            return new Car(model);
        }
    
        public static Builder builder(){
            return new Builder();
        }
    
        public static class Builder{
            private String origin;
            private String model;
    
            public Builder origin(String origin){
                this.origin = origin;
                return this;
            }
    
            public Builder model(String model){
                this.model = model;
                return this;
            }
    
            public Car build(){
                return new Car(this);
            }
        }
    }
    
    • 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

    @SuperBuilder用法

  • 相关阅读:
    Promise异步编程
    Could not find resource src/config/dataConfig.xml错误
    IC基础——FIFO
    「贪心笔记」通过最少操作次数使得数组的和相等
    三个步骤搞定 MySQL,offer到手。
    docker打包多架构镜像(manifest)
    代码随想录 Day - 56|#583 两个字符串的删除操作|#72 编辑距离
    【必会】Kafka基本概念(topic、partition、offset、broker、生产者、消费者、消费者组等)【知识点速记速查】
    OpenCV入门(C++/Python)- 使用OpenCV调整尺寸大小(三)
    2021年下半年软件设计师上午真题答案及解析(六)
  • 原文地址:https://blog.csdn.net/virusos/article/details/127981892