欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決lombok的@Data注解無法打印繼承的父類信息問題

 更新時間:2024年11月06日 09:24:04   作者:樘棣寂寂  
在Java編程中,使用@Data注解可能導致子類繼承父類屬性后,打印只顯示子類信息不顯示父類信息,問題源于@Data注解作用域僅限于當前類,解決方法包括使用@ToString(callSuper=true)注解或重寫toString方法

問題場景

子類StudentResp繼承父類PersonResp,子類也擁有了父類的屬性。

給子類中繼承的父類屬性的賦值,但是打印了以后只會顯示子類信息,父類信息不顯示。

  • 子類:學生類繼承父類人員類
@Data
public class StudentResp extends PersonResp {

    /**
     * 學號
     */
    private Integer studentId;

    /**
     * 成績
     */
    private Integer score;
}
  • 父類:人員類
@Data
public class PersonResp {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年齡
     */
    private Integer age;
}

代碼中給子類以及繼承的父類信息賦值然后打印

    public static void main (String[] args) {

        StudentResp studentResp = new StudentResp();
        //學生子類賦值
        studentResp.setStudentId(1000000000);
        studentResp.setScore(92);
        //父類賦值
        studentResp.setName("風清揚");
        studentResp.setAge(18);
        //打印學生子類信息
        System.out.println(studentResp);
 
    }

打印出來只有子類自己的屬性,父類的屬性沒有出來

問題分析

單獨獲取子類中父類的信息(name跟age),觀察打印看有沒有值

    public static void main (String[] args) {

        StudentResp studentResp = new StudentResp();
        //學生子類賦值
        studentResp.setStudentId(1000000000);
        studentResp.setScore(92);
        //父類賦值
        studentResp.setName("風清揚");
        studentResp.setAge(18);

        //打印父類信息
        System.out.println("姓名:" + studentResp.getName());
        System.out.println("年齡:" + studentResp.getAge());
    }

打印出來發(fā)現(xiàn)是有值的

確認了繼承本身是沒有問題的,繼承的父類的值都可以給上,也能獲取到,隨后直接打開target對應目錄下的StudentResp子類,觀察編譯后的代碼

public class StudentResp extends PersonResp {
    private Integer studentId;
    private Integer score;

    public StudentResp() {
    }

    public Integer getStudentId() {
        return this.studentId;
    }

    public Integer getScore() {
        return this.score;
    }

    public void setStudentId(final Integer studentId) {
        this.studentId = studentId;
    }

    public void setScore(final Integer score) {
        this.score = score;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof StudentResp)) {
            return false;
        } else {
            StudentResp other = (StudentResp)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$studentId = this.getStudentId();
                Object other$studentId = other.getStudentId();
                if (this$studentId == null) {
                    if (other$studentId != null) {
                        return false;
                    }
                } else if (!this$studentId.equals(other$studentId)) {
                    return false;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $studentId = this.getStudentId();
        int result = result * 59 + ($studentId == null ? 43 : $studentId.hashCode());
        Object $score = this.getScore();
        result = result * 59 + ($score == null ? 43 : $score.hashCode());
        return result;
    }

    public String toString() {
        return "StudentResp(studentId=" + this.getStudentId() + ", score=" + this.getScore() + ")";
    }
}

看到這里,大多數(shù)同學應該已經(jīng)定位到出現(xiàn)問題的原因了。

最后的toString()方法可以明確的看到這里只取了子類本身的兩個屬性,并沒有去獲取父類的屬性。

問題原因

通過看編譯后的代碼可以得出@Data不會打印繼承的父類信息是因為該注解本身作用域的問題,@Data注解在編譯時會自動給實體類添加@Setter,@Getter,@ToString等方法。

但是@Data注解的作用域只在當前類中,所以最終打印的時候只會打印出當前類(子類)的信息。

即使子類擁有的是全屬性,但是打印不會顯示父類信息。

解決方式

本文介紹兩種解決方案。

  • 第一種:在子類上添加@ToString(callSuper = true)注解,該注解會將父類的屬性跟子類的屬性一起生成toString;
  • 第二種:不使用@Data注解,使用@Setter,@Getter注解,在父類中重寫toString()方法并用JSON的方式打印。

1.子類添加@ToString(callSuper = true)注解

@Data
@ToString(callSuper = true)
public class StudentResp extends PersonResp {

    /**
     * 學號
     */
    private Integer studentId;

    /**
     * 成績
     */
    private Integer score;
}

打印子類

    public static void main (String[] args) {

        StudentResp studentResp = new StudentResp();
        //學生子類賦值
        studentResp.setStudentId(1000000000);
        studentResp.setScore(92);
        //父類賦值
        studentResp.setName("風清揚");
        studentResp.setAge(18);
        //學生子類
        System.out.println(studentResp);
  
    }

結果可以看到父類信息

再看看target中子類編譯后的代碼

public class StudentResp extends PersonResp {
    private Integer studentId;
    private Integer score;

    public StudentResp() {
    }

    public Integer getStudentId() {
        return this.studentId;
    }

    public Integer getScore() {
        return this.score;
    }

    public void setStudentId(final Integer studentId) {
        this.studentId = studentId;
    }

    public void setScore(final Integer score) {
        this.score = score;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof StudentResp)) {
            return false;
        } else {
            StudentResp other = (StudentResp)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$studentId = this.getStudentId();
                Object other$studentId = other.getStudentId();
                if (this$studentId == null) {
                    if (other$studentId != null) {
                        return false;
                    }
                } else if (!this$studentId.equals(other$studentId)) {
                    return false;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $studentId = this.getStudentId();
        int result = result * 59 + ($studentId == null ? 43 : $studentId.hashCode());
        Object $score = this.getScore();
        result = result * 59 + ($score == null ? 43 : $score.hashCode());
        return result;
    }

    public String toString() {
        return "StudentResp(super=" + super.toString() + ", studentId=" + this.getStudentId() + ", score=" + this.getScore() + ")";
    }
}

可以明確的看到最后的toString()方法中多了一個super.toString()方法,將父類的信息打印出來

2.不使用@Data注解,使用@Setter,@Getter注解,在父類中重寫toString()方法并用JSON的方式打印。

子類使用@Setter,@Getter注解

@Getter
@Setter
public class StudentResp extends PersonResp {

    /**
     * 學號
     */
    private Integer studentId;

    /**
     * 成績
     */
    private Integer score;
}

父類中重寫toString()方法并用JSON的方式打印

@Data
public class PersonResp {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年齡
     */
    private Integer age;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


}

打印子類

    public static void main (String[] args) {

        StudentResp studentResp = new StudentResp();
        //學生子類賦值
        studentResp.setStudentId(1000000000);
        studentResp.setScore(92);
        //父類賦值
        studentResp.setName("風清揚");
        studentResp.setAge(18);
        //學生子類
        System.out.println(studentResp);

    }

結果也可以看到父類信息

總結

這個問題本身不算是個大問題,延伸出來更為重要的是我們解決問題的思路。因為從本質(zhì)上來說屬性本身都是存在的,只是沒打印出來。

但是遇到這個問題的時候第一反應都是,明明繼承了父類,為啥父類的值會沒有,從而會帶偏我們?nèi)ソ鉀Q問題方向,遇到此類問題,第一我們應該先確認問題的本質(zhì),到底有沒有給上值,確認了已經(jīng)給上值,就說明只是打印的問題,進而去編譯后的代碼中查看為啥沒打印出來,最終定位到問題從而解決問題。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Struts2攔截器Interceptor的原理與配置實例詳解

    Struts2攔截器Interceptor的原理與配置實例詳解

    攔截器是一種AOP(面向切面編程)思想的編程方式.它提供一種機制是開發(fā)者能夠把相對獨立的代碼抽離出來,配置到Action前后執(zhí)行。下面這篇文章主要給大家介紹了關于Struts2攔截器Interceptor的原理與配置的相關資料,需要的朋友可以參考下。
    2017-11-11
  • 解決mybatis映射結果集失效的問題

    解決mybatis映射結果集失效的問題

    這篇文章主要介紹了解決mybatis映射結果集失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • java中l(wèi)ist使用時需避免的場景總結

    java中l(wèi)ist使用時需避免的場景總結

    眾所周知,Java為開發(fā)者提供了多種集合類的實現(xiàn),其中幾乎所有業(yè)務代碼都需要用到List,但List的錯誤使用也會導致諸多問題,所以本文我們就來看一看幾個錯誤使用List的場景吧
    2023-10-10
  • Java?Map的compute方法舉例詳解

    Java?Map的compute方法舉例詳解

    Java中的Map是一種用于存儲鍵值對的數(shù)據(jù)結構,它提供了一系列的方法來操作和訪問其中的元素,下面這篇文章主要給大家介紹了關于Java?Map的compute方法舉例詳解的相關資料,需要的朋友可以參考下
    2024-06-06
  • 一文帶你搞懂Java8的LocalDateTime

    一文帶你搞懂Java8的LocalDateTime

    LocalDateTime?是Java8中新加入的日期時間類,現(xiàn)在都?Java20?了,不會還有人沒用過?LocalDateTime?吧?今天給大家演示一下?LocalDateTime?的常用方法
    2023-04-04
  • Java的@Transactional、@Aysnc、事務同步問題詳解

    Java的@Transactional、@Aysnc、事務同步問題詳解

    這篇文章主要介紹了Java的@Transactional、@Aysnc、事務同步問題詳解,現(xiàn)在我們需要在一個業(yè)務方法中插入一個用戶,這個業(yè)務方法我們需要加上事務,然后插入用戶后,我們要異步的方式打印出數(shù)據(jù)庫中所有存在的用戶,需要的朋友可以參考下
    2023-11-11
  • SpringBoot Nacos實現(xiàn)自動刷新

    SpringBoot Nacos實現(xiàn)自動刷新

    這篇文章主要介紹了SpringBoot Nacos實現(xiàn)自動刷新,Nacos(Dynamic Naming and Configuration Service)是阿里巴巴開源的一個動態(tài)服務發(fā)現(xiàn)、配置管理和服務管理平臺
    2023-01-01
  • 淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)

    淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)

    下面小編就為大家?guī)硪黄獪\談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Java線程協(xié)作的兩種方式小結

    Java線程協(xié)作的兩種方式小結

    Java中線程協(xié)作的最常見的兩種方式是利用Object.wait()、Object.notify()和使用Condition,本文主要介紹了Java線程協(xié)作的兩種方式小結,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • 淺談Java中的LinkedHashSet哈希鏈表

    淺談Java中的LinkedHashSet哈希鏈表

    這篇文章主要介紹了淺談Java中的LinkedHashSet哈希鏈表,LinkedHashSet 是 Java 中的一個集合類,它是 HashSet 的子類,并實現(xiàn)了 Set 接口,與 HashSet 不同的是,LinkedHashSet 保留了元素插入的順序,并且具有 HashSet 的快速查找特性,需要的朋友可以參考下
    2023-09-09

最新評論