lombok几个基本注解的使用

lombok是一款在java开发中简洁化代码十分有用的插件工具,这篇博客对较为常用的几种注解进行记录
使用lombok注解,目的和作用就在于不用再去写经常反复去写的(如Getter,Setter,Constructor等)一些代码了。

首先,用到的几个注解:
@Data
使用这个注解,就不用再去手写Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在编译时会自动加进去。
@AllArgsConstructor
使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor
使用后创建一个无参构造函数
@Builder
关于Builder较为复杂一些,Builder的作用之一是为了解决在某个类有很多构造函数的情况,也省去写很多构造函数的麻烦,在设计模式中的思想是:用一个内部类去实例化一个对象,避免一个类出现过多构造函数,

1.首先,建立一个简单的类,并用lombok进行注解:注意这是注解前的代码,可以与后面贴出的注解生成的代码进行比较

1
2
3
4
5
6
7
8
9
10
@Data //生成getter,setter等函数
@AllArgsConstructor //生成全参数构造函数
@NoArgsConstructor//生成无参构造函数
@Builder
public class test1 {
String name;
String age;
String sex;
}

  1. 测试
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public static void main(String[] args) {
    //使用@Builder注解后,可以直接通过Builder设置字段参数
    test1 t1=new test1.test1Builder()
    .name("wang")
    .age("12")
    .sex("man")
    .build();

    System.out.println("name is"+t1.getName()+'\n'+"age is :"+t1.getAge());

    }

3.通过查看编译后的类,比较注解前后的代码量,发现会省去了很多代码的书写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
public class test1 {
String name;
String age;
String sex;

public static test1.test1Builder builder() {
return new test1.test1Builder();
}

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

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

public String getSex() {
return this.sex;
}

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

public void setAge(String age) {
this.age = age;
}

public void setSex(String sex) {
this.sex = sex;
}

public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof test1)) {
return false;
} else {
test1 other = (test1)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
break label47;
}
} else if (this$name.equals(other$name)) {
break label47;
}

return false;
}

Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}

Object this$sex = this.getSex();
Object other$sex = other.getSex();
if (this$sex == null) {
if (other$sex != null) {
return false;
}
} else if (!this$sex.equals(other$sex)) {
return false;
}

return true;
}
}
}

protected boolean canEqual(Object other) {
return other instanceof test1;
}

public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $sex = this.getSex();
result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
return result;
}

public String toString() {
return "test1(name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ")";
}

@ConstructorProperties({"name", "age", "sex"})
public test1(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}

public test1() {
}

public static class test1Builder {
private String name;
private String age;
private String sex;

test1Builder() {
}

public test1.test1Builder name(String name) {
this.name = name;
return this;
}

public test1.test1Builder age(String age) {
this.age = age;
return this;
}

public test1.test1Builder sex(String sex) {
this.sex = sex;
return this;
}

public test1 build() {
return new test1(this.name, this.age, this.sex);
}

public String toString() {
return "test1.test1Builder(name=" + this.name + ", age=" + this.age + ", sex=" + this.sex + ")";
}
}
}


  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码

请我喝杯咖啡吧~

支付宝
微信