Lombok @Accessors用法

简介

  • @Accessors 访问器 在lombok v0.11.0版本引入.
  • @Accessors注释用于配置lombok如何生成和查找getter和setter
  • @Accessors有三个选项
    • fluent 为true时, 省略set/get方法关键字, 并且set方法返回对象本身。
    • chain 为true时,set方法返回对象本身。
    • prefix 为true时,省略配置的属性名前缀

具体实现

fluent

省略set/get方法关键字, 并且set方法返回对象本身

  • 实体类 UserFluent.java
@Getter
@Setter
@Accessors(fluent = true)
public class UserFluent {
    private String name;
    private Integer age;
}
  • 编译类 UserFluent.class
public class UserFluent {
    private String name;
    private Integer age;

    public UserFluent() {
    }

    public String name() {
        return this.name;
    }

    public Integer age() {
        return this.age;
    }

    public UserFluent name(final String name) {
        this.name = name;
        return this;
    }

    public UserFluent age(final Integer age) {
        this.age = age;
        return this;
    }
}
  • 测试类
@Slf4j
public class UserFluentTest {

    public static void main(String[] args) {
        UserFluent userFluent = new UserFluent();
        userFluent.name("常山赵子龙").age(19);
        log.info("name : {}, userFluent = {} " , userFluent.name(), userFluent.age());
    }
}
  • 执行结果
name : 常山赵子龙, age= 19 

chain

链式setter, set方法返回对象本身

@Getter
@Setter
@Accessors(chain = true)
public class UserChain {
    private String name;
    private Integer age;
}

编译结果UserChain.class

public class UserChain {
    private String name;
    private Integer age;

    public UserChain() {
    }

    public String getName() {
        return this.name;
    }

    public Integer getAge() {
        return this.age;
    }

    public UserChain setName(final String name) {
        this.name = name;
        return this;
    }

    public UserChain setAge(final Integer age) {
        this.age = age;
        return this;
    }
}

  • 测试类
@Slf4j
public class UserChainTest {

    public static void main(String[] args) {
        UserChain userChain = new UserChain().setName("常山赵子龙").setAge(19);
        log.info("name: {}, age = {} " , userChain.getName(), userChain.getAge());
    }
}
  • 执行结果
name : 常山赵子龙, age : 19

prefix

get/set方法省略配置的属性名前缀

  • 实体类
@Getter
@Setter
@Accessors(prefix = "u")
public class UserPrefix {
    private String uName;
    private Integer uAge;
}
  • 编译类
public class UserPrefix {
    private String uName;
    private Integer uAge;

    public UserPrefix() {
    }

    public String getName() {
        return this.uName;
    }

    public Integer getAge() {
        return this.uAge;
    }

    public void setName(final String uName) {
        this.uName = uName;
    }

    public void setAge(final Integer uAge) {
        this.uAge = uAge;
    }
}
  • 测试类
@Slf4j
public class UserPrefixTest {

    public static void main(String[] args) {
        UserPrefix userPrefix = new UserPrefix();
        userPrefix.setName("常山赵子龙");
        userPrefix.setAge(19);
        log.info("name : {}, age : {}" , userPrefix.getName(), userPrefix.getAge());
    }
}
  • 执行结果
name : 常山赵子龙, age : 19

参考文档

官网 Accessors

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值