本篇內容主要講解“Lombok中@Data的使用方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Lombok中@Data的使用方法是什么”吧!
先來簡單介紹一下 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 通過增加一些"處理程序",可以讓 Java 代碼變得簡潔、快速。
Lombok 提供了一系列的注解幫助我們簡化代碼,比如:
| 注解名稱 | 功能 |
|---|---|
| @Setter | 自動添加類中所有屬性相關的 set 方法 |
| @Getter | 自動添加類中所有屬性相關的 get 方法 |
| @Builder | 使得該類可以通過 builder (建造者模式)構建對象 |
| @RequiredArgsConstructor | 生成一個該類的構造方法,禁止無參構造 |
| @ToString | 重寫該類的toString()方法 |
| @EqualsAndHashCode | 重寫該類的equals()和hashCode()方法 |
| @Data | 等價于上面的@Setter、@Getter、@RequiredArgsConstructor、@ToString、@EqualsAndHashCode |
看起來似乎這些注解都很正常,并且對我們的代碼也有一定的優化,那為什么說@Data注解存在坑呢?
由上面的表格我們可以知道,@Data是包含了@EqualsAndHashCode的功能,那么它究竟是如何重寫equals()和hashCode()方法的呢?
我們定義一個類TestA:
@Data
public class TestA {
String oldName;
}我們將其編譯后的 class 文件進行反編譯:
public class TestA {
String oldName;
public TestA() {
}
public String getOldName() {
return this.oldName;
}
public void setOldName(String oldName) {
this.oldName = oldName;
}
public boolean equals(Object o) {
// 判斷是否是同一個對象
if (o == this) {
return true;
}
// 判斷是否是同一個類
else if (!(o instanceof TestA)) {
return false;
} else {
TestA other = (TestA) o;
if (!other.canEqual(this)) {
return false;
} else {
// 比較類中的屬性(注意這里,只比較了當前類中的屬性)
Object this$oldName = this.getOldName();
Object other$oldName = other.getOldName();
if (this$oldName == null) {
if (other$oldName != null) {
return false;
}
} else if (!this$oldName.equals(other$oldName)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TestA;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $oldName = this.getOldName();
int result = result * 59 ($oldName == null ? 43 : $oldName.hashCode());
return result;
}
public String toString() {
return "TestA(oldName=" this.getOldName() ")";
}
}針對其equals()方法,當它進行屬性比較時,其實只比較了當前類中的屬性。如果你不信的話,我們再來創建一個類TestB,它是TestA的子類:
@Data
public class TestB extends TestA {
private String name;
private int age;
}我們將其編譯后的 class 文件進行反編譯:
public class TestB extends TestA {
private String name;
private int age;
public TestB() {
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TestB)) {
return false;
} else {
TestB other = (TestB)o;
if (!other.canEqual(this)) {
return false;
} else {
// 注意這里,真的是只比較了當前類中的屬性,并沒有比較父類中的屬性
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
return this.getAge() == other.getAge();
}
} else if (this$name.equals(other$name)) {
return this.getAge() == other.getAge();
}
return false;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TestB;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 ($name == null ? 43 : $name.hashCode());
result = result * 59 this.getAge();
return result;
}
public String toString() {
return "TestB(name=" this.getName() ", age=" this.getAge() ")";
}
}按照代碼的理解,如果兩個子類對象,其子類中的屬性相同、父類中的屬性不同時,利用equals()方法時,依舊會認為這兩個對象相同,測試一下:
public static void main(String[] args) {
TestB t1 = new TestB();
TestB t2 = new TestB();
t1.setOldName("123");
t2.setOldName("12345");
String name = "1";
t1.name = name;
t2.name = name;
int age = 1;
t1.age = age;
t2.age = age;
System.out.println(t1.equals(t2));
System.out.println(t2.equals(t1));
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t1 == t2);
System.out.println(Objects.equals(t1, t2));
}結果為:
true
true
6373
6373
false
true
對于父類是Object且使用了@EqualsAndHashCode(callSuper = true)注解的類,這個類由 Lombok 生成的equals()方法只有在兩個對象是同一個對象時,才會返回 true ,否則總為 false ,無論它們的屬性是否相同。
這個行為在大部分時間是不符合預期的,equals()失去了其意義。即使我們期望equals()是這樣工作的,那么其余的屬性比較代碼便是累贅,會大幅度降低代碼的分支覆蓋率。
用了@Data就不要有繼承關系,類似 Kotlin 的做法。
自己重寫equals(), Lombok 不會對顯式重寫的方法進行生成。
顯式使用@EqualsAndHashCode(callSuper = true), Lombok 會以顯式指定的為準。
面試問你@Data注解的作用,一般人回答就是生成get/set/toString
真是這樣嗎?
其實不然,其實@Data注解作用是
get/set
無參數構造器
toString
hashcode
equals
@Data會自動生成hashcode和equals方法,一般人會把這點忘了
idea使用alt+6查看類的具體屬性和方法

到此,相信大家對“Lombok中@Data的使用方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。