lombok表达式中@Accessors注解
  TEZNKK3IfmPf 2024年03月29日 74 0

该注解用于配置lombok生成getter和setter方法的规则,总共有三个可配置的属性:

fluent

设置为true时,则gettersetter方法的方法名是基础属性名,且setter方法返回当前对象。默认值为false。此时,除非手动指定,否则chain默认为true

@Accessors(fluent = true)
public class AccessorsExample {
  @Getter @Setter
  private int age = 10;
}
/***等价于下面的代码****/
public class AccessorsExample {
  private int age = 10;
  
  public int age() {
    return this.age;
  }
  
  public AccessorsExample age(final int age) {
    this.age = age;
    return this;
  }
}

chain

一个boolean类型的参数,如果设置为true,则setter方法返回这个对象。默认值为:false,但是当fluent=true时,默认值为true

public class ChainExample {
  private int age;
  // 返回当前对象
  public ChainExample setAge(int age) {
    this.age = age;
    return this;
  }
}

prefix

一个String类型的参数,如果这个参数存在,则属性必须使用这个参数的值作为前缀。系统在创建gettersetter方法的时候会删除前缀。 注意:前缀后面的字符不能为小写字母。

class PrefixExample {
  @Accessors(prefix = "f") @Getter
  private String fName = "Hello, World!";
}
/***等价于下面的代码****/
class PrefixExample {
  private String fName = "Hello, World!";
  
  public String getName() {
    return this.fName;
  }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月29日 0

暂无评论

TEZNKK3IfmPf