四、Lombok 注解详解(2)

1,@Setter 和 @Getter

(1)这两个注解用于生成 setter 和 getter 方法,可以用在类或者属性上:
  • 如果用在属性上:则只为该属性提供 setter 和 getter 方法
  • 如果是用在类上:则为这个类所有属性供 setter 和 getter方法
 
(2)下面我们在一个类上添加 @Setter 和 @Getter 注解:
package com.example.demo;

import lombok.Getter;
import lombok.Setter; @Getter
@Setter
public class User {
private String name;
private Integer age;
}

(3)然后我们就可以直接使用它的 setter 和 getter 方法了:

User user = new User();
user.setName("hangge");
user.setAge(123);
user.getName();
user.getAge();

2,@Getter(lazy=true)

(1)该标注用于生成一个 lazy 版的 getter,它会在第一次调用这个 getter 时计算一次值,然后从那里开始缓存它。如果计算该值需要大量 CPU,或者该值占用大量内存,这可能很有用。
注意:Lombok 会自动去管理线程安全的问题,所以不会存在重复赋值的问题。
(2)要使用此功能,需要创建一个 private final 变量,并且使用运行成本高的表达式对其进行初始化,同时使用 @Getter(lazy=true) 注解进行标注。
 1 // 使用注解
2 public class GetterLazyExample {
3 @Getter(lazy=true) private final double[] cached = expensive();
4
5 private double[] expensive() {
6 double[] result = new double[1000000];
7 for (int i = 0; i < result.length; i++) {
8 result[i] = Math.asin(i);
9 }
10 return result;
11 }
12 }
13
14 // 不使用注解
15 public class GetterLazyExample {
16 private final java.util.concurrent.AtomicReference<java.lang.Object> cached =
17 new java.util.concurrent.AtomicReference<java.lang.Object>();
18
19 public double[] getCached() {
20 java.lang.Object value = this.cached.get();
21 if (value == null) {
22 synchronized(this.cached) {
23 value = this.cached.get();
24 if (value == null) {
25 final double[] actualValue = expensive();
26 value = actualValue == null ? this.cached : actualValue;
27 this.cached.set(value);
28 }
29 }
30 }
31 return (double[])(value == this.cached ? null : value);
32 }
33
34 private double[] expensive() {
35 double[] result = new double[1000000];
36 for (int i = 0; i < result.length; i++) {
37 result[i] = Math.asin(i);
38 }
39 return result;
40 }
41 }

3,@ToString

(1)@ToString 注解在类上, 为类提供 toString() 方法:

  • 默认情况下,它会按顺序(以逗号分隔)打印这个类名称以及每个字段。
  • 可以这样设置不包含哪些字段:@ToString(exclude = "id") 或者 @ToString(exclude = {"id","name"})
 1 package com.example.demo;
2
3 import lombok.Getter;
4 import lombok.Setter;
5 import lombok.ToString;
6
7 @Getter
8 @Setter
9 @ToString
10 public class User {
11 private String name;
12 private Integer age;
13 }

(2)下面是一个简单的测试样例:

1 User user = new User();
2 user.setName("hangge");
3 user.setAge(123);
4 System.out.println(user.toString());

 

4,@EqualsAndHashCode

(1)当其注解在类上,为该类提供 hashCode() 和 equals() 方法:
  • 默认情况下,它将使用所有非静态,非 transient 字段。
  • 可以通过在可选的 exclude 参数中来排除更多字段。
  • 也可以通过在 parameter 参数中命名它们来准确指定希望使用哪些字段。
 1 package com.amos.lombok;
2
3 import lombok.EqualsAndHashCode;
4
5 @EqualsAndHashCode
6 public class EqualsAndHashCodeExample {
7
8 private transient int transientVar = 10;
9 private String name;
10 private double score;
11
12 /**
13 * 不包含该字段
14 */
15 @EqualsAndHashCode.Exclude
16 private Shape shape = new Square(5, 10);
17 private String[] tags;
18
19 /**
20 * 不包含该字段
21 */
22 @EqualsAndHashCode.Exclude
23 private int id;
24
25 public String getName() {
26 return this.name;
27 }
28
29 @EqualsAndHashCode(callSuper = true)
30 public static class Square extends Shape {
31 private final int width, height;
32
33 public Square(int width, int height) {
34 this.width = width;
35 this.height = height;
36 }
37 }
38
39 public static class Shape {
40 }
41 }

(2)上面编译后会变成如下代码:

  1 package com.amos.lombok;
2
3 import java.util.Arrays;
4
5 public class EqualsAndHashCodeExample {
6 private transient int transientVar = 10;
7 private String name;
8 private double score;
9 private EqualsAndHashCodeExample.Shape shape = new EqualsAndHashCodeExample.Square(5, 10);
10 private String[] tags;
11 private int id;
12
13 public EqualsAndHashCodeExample() {
14 }
15
16 public String getName() {
17 return this.name;
18 }
19
20 public boolean equals(final Object o) {
21 if (o == this) {
22 return true;
23 } else if (!(o instanceof EqualsAndHashCodeExample)) {
24 return false;
25 } else {
26 EqualsAndHashCodeExample other = (EqualsAndHashCodeExample)o;
27 if (!other.canEqual(this)) {
28 return false;
29 } else {
30 label31: {
31 Object this$name = this.getName();
32 Object other$name = other.getName();
33 if (this$name == null) {
34 if (other$name == null) {
35 break label31;
36 }
37 } else if (this$name.equals(other$name)) {
38 break label31;
39 }
40
41 return false;
42 }
43
44 if (Double.compare(this.score, other.score) != 0) {
45 return false;
46 } else {
47 return Arrays.deepEquals(this.tags, other.tags);
48 }
49 }
50 }
51 }
52
53 protected boolean canEqual(final Object other) {
54 return other instanceof EqualsAndHashCodeExample;
55 }
56
57 public int hashCode() {
58 int PRIME = true;
59 int result = 1;
60 Object $name = this.getName();
61 int result = result * 59 + ($name == null ? 43 : $name.hashCode());
62 long $score = Double.doubleToLongBits(this.score);
63 result = result * 59 + (int)($score >>> 32 ^ $score);
64 result = result * 59 + Arrays.deepHashCode(this.tags);
65 return result;
66 }
67
68 public static class Shape {
69 public Shape() {
70 }
71 }
72
73 public static class Square extends EqualsAndHashCodeExample.Shape {
74 private final int width;
75 private final int height;
76
77 public Square(int width, int height) {
78 this.width = width;
79 this.height = height;
80 }
81
82 public boolean equals(final Object o) {
83 if (o == this) {
84 return true;
85 } else if (!(o instanceof EqualsAndHashCodeExample.Square)) {
86 return false;
87 } else {
88 EqualsAndHashCodeExample.Square other = (EqualsAndHashCodeExample.Square)o;
89 if (!other.canEqual(this)) {
90 return false;
91 } else if (!super.equals(o)) {
92 return false;
93 } else if (this.width != other.width) {
94 return false;
95 } else {
96 return this.height == other.height;
97 }
98 }
99 }
100
101 protected boolean canEqual(final Object other) {
102 return other instanceof EqualsAndHashCodeExample.Square;
103 }
104
105 public int hashCode() {
106 int PRIME = true;
107 int result = super.hashCode();
108 result = result * 59 + this.width;
109 result = result * 59 + this.height;
110 return result;
111 }
112 }
113 }

最新文章

  1. hdu 5122
  2. PHP网页
  3. 使用 Grafana、collectd 和 InfluxDB 打造现代监控系统
  4. 【GruntMate】一个让你更方便使用Grunt的工具
  5. JavaScript高级程序设计之数值数组排序
  6. JQUERY 动态时钟
  7. javascript进击(九)参考手册
  8. 精通 Oracle+Python,第 5 部分:存储过程、Python 编程
  9. javascript 高级程序设计(二)-在html中使用javascript
  10. 客户端JavaScript(window、document、element)
  11. 写两个线程,一个对n每次加一,一个对n每次减一
  12. [置顶] Application,Session,Cookie之Application对象
  13. [Django] Base class in the model layer
  14. oracle动态视图v$,v_$,gv$,gv_$与x$之间的关系
  15. 通过hue提交oozie定时任务
  16. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
  17. Luogu4137:Rmq Problem/mex
  18. 使用jQuery.form库中ajaxSubmit提交表单时遇到的一些问题
  19. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
  20. django生成迁移文件

热门文章

  1. for in | for in 比较 解释 | 以后找知识点先从这里面搜索
  2. csr_matrix与ndarray类型互转
  3. https原理(四)双向实践(java客户端+tcp代理)
  4. IntelliJ IDEA修改系统缓存目录
  5. 08 安装虚拟机:Windows 10
  6. 渲染杂谈:early-z、z-culling、hi-z、z-perpass到底是什么?
  7. vmware 二次虚拟化
  8. Vite项目打包配置详解
  9. SpringBoot2.2.2+SpringCloud-Hoxton.SR1整合eureka/gateway
  10. Mysql数据库基础第二章:(六)连接查询