Java——》lombok中的@Accessors注解

本文详细介绍了 Lombok 库中 @Accessors 注解的功能及使用方法,包括 fluent、chain 和 prefix 属性的作用,并通过示例展示了如何利用这些属性自定义 getter 和 setter 方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中文:存取器
用于:配置getter和setter方法的生成结果
参考:@Accessors官方文档
参考:总结——》【Java】

一、Accessors源码

package lombok.experimental;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * A container for settings for the generation of getters and setters.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/experimental/Accessors">the project lombok features page for &#64;Accessors</a>.
 * <p>
 * Using this annotation does nothing by itself; an annotation that makes lombok generate getters and setters,
 * such as {@link lombok.Setter} or {@link lombok.Data} is also required.
 */
@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}.
	 * <strong>default: false</strong>
	 * 
	 * @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}.
	 * <strong>default: false</strong>, unless {@code fluent=true}, then <strong>default: true</strong>
	 * 
	 * @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 {};
}

二、Accessors属性

使用@Accessors时,fluent默认为false,chain默认false,prefix默认{}
在属性上增加注解,在类上的注解就会自动忽略=

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author Created by xiaoxian
 * @version v1.0.0
 */
@Data
@Accessors
public class TestUser {
    Long testUerId;
    String testUserName;
}

在这里插入图片描述

1、fluent

如果fluent=true,那么chain默认为真。

@Accessors(fluent = true)

  • 属性:testUerId
  • getter:testUserId()
  • setter:testUserId(T newValue):返回当前对象的实例

在这里插入图片描述

2、chain

set方法返回的是对象的实例,可以直接再使用set方法或者直接调用函数

@Accessors(chain = true)

  • 属性:testUerId
  • getter:getTestUserId()
  • setter:setTestUserId(T newValue):返回当前对象的实例

在这里插入图片描述

3、prefix:默认{}

遵守驼峰命名,忽视指定前缀(匹配有前缀+大写字母这样的匹配)
@Accessors(prefix = "test")

  • 属性:testUerId
  • getter:getUserId()
  • setter:setUserId(T newValue)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值