lombok——@EqualsAndHashCode(callSuper = true)註解的使用
@EqualsAndHashCode(callSuper = true)
該註解用於子類物件之間進行比較的時候
不加該註解的影響:子類物件屬性值一致,但其繼承的父類物件屬性值不一致,在比較的時候會出現比較結果不對的情況。
舉個簡單的例子:
這邊先定義一個分類物件 Parent,有一個屬性:code
@Data public class Parent { /** * 父類編碼 */ private String code; }
再定義一個子類物件 Child,一一個屬性:name
@Data public class Child extends Parent {/** * 子類名稱 */ private String name; }
在方法中 new 兩個 Child 物件:childTest1、childTest2
對這兩個 Child 物件的自有屬性 name 都賦值為:Child;但是對繼承的父類屬性 code 進行不同的賦值
Child childTest1 = new Child(); childTest1.setCode("1"); childTest1.setName("child"); Child childTest2 = new Child(); childTest2.setCode("2"); childTest2.setName("child");
根據使用過程中,這兩個物件肯定是不一樣的,但是,在不加@EqualsAndHashCode(callSuper = true)註解的情況下對這兩個物件進行比較得到的結果卻是 true
boolean isSame = Objects.equals(childTest1,childTest2); log.info("testEquals -> childTest1:{}, childTest2:{}", childTest1, childTest2); log.info("testEquals -> :{}", isSame);
@EqualsAndHashCode(callSuper = true) 註解的作用就是將其父類屬性也進行比較
@EqualsAndHashCode(callSuper = true) @Data public class Child extends Parent { /** * 子類名稱 */ private String name; }
因為 @Data 生成的 equals 方法,只對該類裡自有的屬性進行了比較;
下面看下加與不加註解的時候編譯後的 Child 類
(1)無 @EqualsAndHashCode(callSuper = true) 註解
public boolean equals(Object o){ if (o == this) { return true; } if (!(o instanceof Child)) { return false; } Child other = (Child)o; if (!other.canEqual(this)) { return false; } Object this$name = getName();
Object other$name = other.getName();
return this$name == null ? other$name == null : this$name.equals(other$name); }
(2)有 @EqualsAndHashCode(callSuper = true) 註解
public boolean equals(Object o){ if (o == this) { return true; } if (!(o instanceof Child)) { return false; } Child other = (Child)o; if (!other.canEqual(this)) { return false; } if (!super.equals(o)) { return false; } Object this$name = getName();
Object other$name = other.getName();
return this$name == null ? other$name == null : this$name.equals(other$name); }
對比一下,可以看到加了註解之後多了 super.equals 方法
if (!super.equals(o)) { return false; }
細心的朋友會發現,在用 log 列印兩個物件的時候,toString 方法只打印了子類屬性,隱藏了父類屬性,這裡其實和 equals 方法一樣,@Data 註解生成的 toString 方法也只包含了子類自有屬性。
解決方案一樣,加上 @ToString(callSuper = true) 註解,其實這裡真正重要的是註解中的屬性,callSuper = true,加上註解後列印結果如下:
ref:(21條訊息) lombok——@EqualsAndHashCode(callSuper = true)註解的使用_快看,飛天豬的部落格-CSDN部落格