@Accessors(chain=true)
链式访问,该注解设置chain=true,生成setter方法返回this(也就是返回的是对象),代替了默认的返回void
@Data
@Accessors(chain = true)
public class Student {
private String name;
private Integer age;
public static void main(String[] args) {
Student student = new Student().setName("lsii").setAge(12);
System.out.println(student);
}
}
@Accessors(fluent = true)
与chain=true类似,区别在于getter和setter不带set和get前缀。
@Data
@Accessors(fluent = true)
public class Student {
private String name;
private Integer age;
public static void main(String[] args) {
Student student = new Student().name("lsii").age(12);
System.out.println(student);
}
}
这篇博客介绍了Java中@Accessors注解的用法,包括`chain=true`和`fluent=true`两种模式。`chain=true`使得setter方法返回this,方便链式调用,而`fluent=true`则去掉getter和setter的set和get前缀,提供更流畅的API风格。示例代码展示了这两种模式的实际应用。
506

被折叠的 条评论
为什么被折叠?



