阅读本文大概需要 4 分钟。
来自:Mr.ml
前言
Lombok背景介绍
Lombok使用方法
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</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;
}
}
推荐阅读:
当IntelliJ IDEA2020.1遇上JDK14:所有美好环环相扣
微信扫描二维码,关注我的公众号
朕已阅
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/1010.html