@Data: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor!
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor!
@Data 是lombok的快捷的方便的注解,它包含了@ToString, @EqualsAndHashCode, @Getter / @Setter 和@RequiredArgsConstructor 的特点。换句话说,@Data会生成所有正常关联的POJO 和 beans ,如全域的getter和所有的非常量域的setter,并且,为保证这些域非空,@Data会挪用toString,equals 和 hashCode的涉及类域的实现类与初始化所有静态域的的构造器,还有初始化器被标记为@NonNull的非常量域。
@Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate(可供模仿的)样板文件 that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull, in order to ensure the field is never null.
@Data is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructors already exist). However, the parameters of these annotations (such as callSuper, includeFieldNames and exclude) cannot be set with @Data. If you need to set non-default values for any of these parameters, just add those annotations explicitly; @Data is smart enough to defer to those annotations.
@Data 就类似设置隐式的@Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor在类上(除了因为任意显式的写好的构造器已经存在而构造器不会被创造的情况外)。然而,这些注解的参数(如callSuper, includeFieldNames 和 exclude)不能和@Data一起被设置。如果你需要设置非默认参数,就直接显示地添加注解。
All generated getters and setters will be public. To override the access level, annotate the field or class with an explicit @Setter and/or @Getter annotation. You can also use this annotation (by combining it with AccessLevel.NONE) to suppress generating a getter and/or setter altogether.
所有的生成的getters 和 setters 将会是public。为了重载这个范围等级,你可以显示地用@Setter和@Getter注解域或者类。你也可以使用它(通过将它合并为无访问等级)去抑制生成getter 和 setter。
All fields marked as transient will not be considered for hashCode and equals. All static fields will be skipped entirely (not considered for any of the generated methods, and no setter/getter will be made for them).
所有被标记为transient的域将不会为hashCode和equals而被考虑。所有静态域将会被完全跳过(不考虑任何被产生的方法,并且也不产生setter/getter)
If the class already contains a method with the same name and parameter count as any method that would normally be generated, that method is not generated, and no warning or error is emitted. For example, if you already have a method with signature equals(AnyType param), no equals method will be generated, even though technically it might be an entirely different method due to having different parameter types. The same rule applies to the constructor (any explicit constructor will prevent @Data from generating one), as well as toString, equals, and all getters and setters. You can mark any constructor or method with @lombok.experimental.Tolerate to hide them from lombok.
如果类已经包含个有同样类名和参数数量(方法签名一致)的方法作为一个将会正常创造的任意方法,那么这方法将不会被生成,并且没有警告和错误发出。例如,如果你已经有了个方法签名:equals(AnyType param),即使技术上因为有不同的参数类型它可能是个完全不同的方法,也没有equals方法将会生成。这个规则同样适用于构造器(任何明确的构造器将会防止@Data生成有同样类名和参数数量的方法),同理,toString,equals,和所有getters 和setters。你可通过@lombok.experimental.Tolerate 标记任何构造器或者方法来从lombok里去隐藏它们。
@Data can handle generics parameters for fields just fine. In order to reduce the boilerplate when constructing objects for classes with generics, you can use the staticConstructor parameter to generate a private constructor, as well as a static method that returns a new instance. This way, javac will infer the variable name. Thus, by declaring like so: @Data(staticConstructor="of") class Foo<T> { private T x;} you can create new instances of Foo by writing: Foo.of(5); instead of having to write: new Foo<Integer>(5);.
本文来自博客园,作者:z_s_s,转载请注明原文链接:https://www.cnblogs.com/zhoushusheng/p/15886384.html
浙公网安备 33010602011771号