简体   繁体   English

在龙目岛省略一个 Setter/Getter

[英]Omitting one Setter/Getter in Lombok

I want to use a data class in Lombok.我想在龙目岛使用数据 class。 Since it has about a dozen fields, I annotated it with @Data in order to generate all the setters and getter.因为它有十几个字段,所以我用@Data注释它以生成所有的 setter 和 getter。 However there is one special field for which I don't want to the accessors to be implemented.但是,有一个特殊领域我不想为其实现访问器。

How does Lombok omit this field? Lombok 是如何省略这个字段的?

You can pass an access level to the @Getter and @Setter annotations.您可以将访问级别传递给@Getter@Setter注释。 This is useful to make getters or setters protected or private.这对于使 getter 或 setter 受保护或私有很有用。 It can also be used to override the default.它也可以用来覆盖默认值。

With @Data , you have public access to the accessors by default.使用@Data ,默认情况下您可以公开访问访问器。 You can now use the special access level NONE to completely omit the accessor, like this:您现在可以使用特殊访问级别NONE来完全省略访问器,如下所示:

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private int mySecret;

According to @Data description you can use:根据@Data 描述你可以使用:

All generated getters and setters will be public.所有生成的 getter 和 setter 都将是公开的。 To override the access level, annotate the field or class with an explicit @Setter and/or @Getter annotation.要覆盖访问级别,请使用显式 @Setter 和/或 @Getter 注释对字段或 class 进行注释。 You can also use this annotation (by combining it with AccessLevel.NONE) to suppress generating a getter and/or setter altogether.您还可以使用此注释(通过将其与 AccessLevel.NONE 结合使用)来完全禁止生成 getter 和/或 setter。

Use the below code for omit/excludes from creating setter and getter.使用以下代码从创建 setter 和 getter 中省略/排除 value key should use inside @Getter and @Setter .键应该在@Getter@Setter

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
private String mySecret;

I want to use a data class in Lombok.我想在 Lombok 中使用数据类。 Since it has about a dozen fields, I annotated it with @Data in order to generate all the setters and getter.因为它有大约十几个字段,我用@Data注释它以生成所有的 setter 和 getter。 However there is one special field for which I don't want to the accessors to be implemented.但是,有一个特殊领域,我不希望实现访问器。

How does Lombok omit this field? Lombok 如何省略这个字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM