CTO:不要在代码中写 set/get 方法了,逮一次罚款...
开发者技术前线
共 4991字,需浏览 10分钟
·
2020-11-07 05:30
点击“开发者技术前线”,选择“星标?”
在看|星标|留言, 真爱
前言
Lombok背景介绍
Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.
Lombok使用方法
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.10version>
<scope>providedscope>
dependency>
将 Lombok 插件安装到 IDEA
file -> setting
选择默认的编译方式为 javac,因为 eclipse 是不支持 Lombok 的编译方式的,javac 支持 Lombok 的编译方式。
打开注解生成器 Enable annotation processing
https://plugins.jetbrains.com/plugin/6317-lombok/versions
@Data
public class Student {
private String name;
private Integer age;
private Integer id;
private String major;
}
public class Student {
@Setter private String name;
private Integer age;
private Integer id;
private String major;
public static void main(String[] args) {
Student stu = new Student();
stu.setName("Mr.ml");
}
}
public class Student {
@Setter private String name;
private Integer age;
private Integer id;
private String major;
public Student(@NonNull String name) {
this.name = name;
}
}
public class CleanupExample {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}
@EqualsAndHashCode(exclude={"id", "shape"})
public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.name;
}
@EqualsAndHashCode(callSuper=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
@ToString(exclude="id")
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.getName();
}
@ToString(callSuper=true, includeFieldNames=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
@NoArgsConstructor
public static class NoArgsExample {
@NonNull private String field;
}
}
END
扫码助手小姐姐微信,进群大厂内推&大佬技术交流
后台回复“电子书” “资料” 领取一份干货,数百面试手册等你 开发者技术前线 ,汇集技术前线快讯和关注行业趋势,大厂干货,是开发者经历和成长的优秀指南。
评论