accessors 作用_@Accessors介绍配置getter和setter

accessors 作用_@Accessors介绍配置getter和setter前言今天在看 GitHub 某大神写的代码的时候看到一个标签并没有使用过 所有百度了一下使用方法在此记录一下 AccessorsAcc 的中文含义是存取器 Accessors 用于配置 getter 和 setter 方法的生成结果 源代码 Target ElementType TYPE ElementType FIELD Retention RetentionPol SOURCE

前言

今天在看GitHub某大神写的代码的时候看到一个标签并没有使用过,所有百度了一下使用方法在此记录一下。

@Accessors

Accessor的中文含义是存取器,@Accessors用于配置getter和setter方法的生成结果。

源代码:

@Target({ElementType.TYPE, ElementType.FIELD})

@Retention(RetentionPolicy.SOURCE)

public @interface Accessors {

/

* If true, accessors will be named after the field and not include a {@code get} or {@code set}

* prefix. If true and {@code chain} is omitted, {@code chain} defaults to {@code true}.

* default: false

*

* @return Whether or not to make fluent methods (named {@code fieldName()}, not for example {@code setFieldName}).

*/

boolean fluent() default false;

/

* If true, setters return {@code this} instead of {@code void}.

* default: false, unless {@code fluent=true}, then default: true

*

* @return Whether or not setters should return themselves (chaining) or {@code void} (no chaining).

*/

boolean chain() default false;

/

* If present, only fields with any of the stated prefixes are given the getter/setter treatment.

* Note that a prefix only counts if the next character is NOT a lowercase character or the last

* letter of the prefix is not a letter (for instance an underscore). If multiple fields

* all turn into the same name when the prefix is stripped, an error will be generated.

*

* @return If you are in the habit of prefixing your fields (for example, you name them {@code fFieldName}, specify such prefixes here).

*/

String[] prefix() default {};

}

此注解可以使用在方法上还有类上。

可以看到一共有3个属性,下面单独记录一下各个属性区别。

chain

chain的中文含义是链式的,设置为true,则setter方法返回当前对象.

accessors 作用_@Accessors介绍配置getter和setter

fluent

fluent的时候:getter和setter方法的方法名都是基础属性名,且setter方法返回当前对象。生成的方法没有get/set名字。

accessors 作用_@Accessors介绍配置getter和setter

prefix

生成getter和setter方法的字段名会忽视指定前缀。可以看到生产的方法中没有job这个前缀了。

accessors 作用_@Accessors介绍配置getter和setter

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216873.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午10:52
下一篇 2026年3月18日 上午10:52


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号