news 2026/2/26 14:53:26

解决lombok的@Data注解无法打印继承的父类信息问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
解决lombok的@Data注解无法打印继承的父类信息问题

https://www.jb51.net/program/330116r71.htm

问题场景

子类StudentResp继承父类PersonResp,子类也拥有了父类的属性。

给子类中继承的父类属性的赋值,但是打印了以后只会显示子类信息,父类信息不显示。

  • 子类:学生类继承父类人员类

1

2

3

4

5

6

7

8

9

10

11

12

13

@Data

publicclassStudentRespextendsPersonResp {

/**

* 学号

*/

privateInteger studentId;

/**

* 成绩

*/

privateInteger score;

}

  • 父类:人员类

1

2

3

4

5

6

7

8

9

10

11

12

13

@Data

publicclassPersonResp {

/**

* 姓名

*/

privateString name;

/**

* 年龄

*/

privateInteger age;

}

代码中给子类以及继承的父类信息赋值然后打印

1

2

3

4

5

6

7

8

9

10

11

12

13

publicstaticvoidmain (String[] args) {

StudentResp studentResp =newStudentResp();

//学生子类赋值

studentResp.setStudentId(1000000000);

studentResp.setScore(92);

//父类赋值

studentResp.setName("风清扬");

studentResp.setAge(18);

//打印学生子类信息

System.out.println(studentResp);

}

打印出来只有子类自己的属性,父类的属性没有出来

问题分析

单独获取子类中父类的信息(name跟age),观察打印看有没有值

1

2

3

4

5

6

7

8

9

10

11

12

13

14

publicstaticvoidmain (String[] args) {

StudentResp studentResp =newStudentResp();

//学生子类赋值

studentResp.setStudentId(1000000000);

studentResp.setScore(92);

//父类赋值

studentResp.setName("风清扬");

studentResp.setAge(18);

//打印父类信息

System.out.println("姓名:"+ studentResp.getName());

System.out.println("年龄:"+ studentResp.getAge());

}

打印出来发现是有值的

确认了继承本身是没有问题的,继承的父类的值都可以给上,也能获取到,随后直接打开target对应目录下的StudentResp子类,观察编译后的代码

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

publicclassStudentRespextendsPersonResp {

privateInteger studentId;

privateInteger score;

publicStudentResp() {

}

publicInteger getStudentId() {

returnthis.studentId;

}

publicInteger getScore() {

returnthis.score;

}

publicvoidsetStudentId(finalInteger studentId) {

this.studentId = studentId;

}

publicvoidsetScore(finalInteger score) {

this.score = score;

}

publicbooleanequals(finalObject o) {

if(o ==this) {

returntrue;

}elseif(!(oinstanceofStudentResp)) {

returnfalse;

}else{

StudentResp other = (StudentResp)o;

if(!other.canEqual(this)) {

returnfalse;

}else{

Objectthis$studentId =this.getStudentId();

Object other$studentId = other.getStudentId();

if(this$studentId ==null) {

if(other$studentId !=null) {

returnfalse;

}

}elseif(!this$studentId.equals(other$studentId)) {

returnfalse;

}

Objectthis$score =this.getScore();

Object other$score = other.getScore();

if(this$score ==null) {

if(other$score !=null) {

returnfalse;

}

}elseif(!this$score.equals(other$score)) {

returnfalse;

}

returntrue;

}

}

}

protectedbooleancanEqual(finalObject other) {

returnotherinstanceofStudentResp;

}

publicinthashCode() {

intPRIME =true;

intresult =1;

Object $studentId =this.getStudentId();

intresult = result *59+ ($studentId ==null?43: $studentId.hashCode());

Object $score =this.getScore();

result = result *59+ ($score ==null?43: $score.hashCode());

returnresult;

}

publicString toString() {

return"StudentResp(studentId="+this.getStudentId() +", score="+this.getScore() +")";

}

}

看到这里,大多数同学应该已经定位到出现问题的原因了。

最后的toString()方法可以明确的看到这里只取了子类本身的两个属性,并没有去获取父类的属性。

问题原因

通过看编译后的代码可以得出@Data不会打印继承的父类信息是因为该注解本身作用域的问题,@Data注解在编译时会自动给实体类添加@Setter,@Getter,@ToString等方法。

但是@Data注解的作用域只在当前类中,所以最终打印的时候只会打印出当前类(子类)的信息。

即使子类拥有的是全属性,但是打印不会显示父类信息。

解决方式

本文介绍两种解决方案。

  • 第一种:在子类上添加@ToString(callSuper = true)注解,该注解会将父类的属性跟子类的属性一起生成toString;
  • 第二种:不使用@Data注解,使用@Setter,@Getter注解,在父类中重写toString()方法并用JSON的方式打印。

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@Data

@ToString(callSuper =true)

publicclassStudentRespextendsPersonResp {

/**

* 学号

*/

privateInteger studentId;

/**

* 成绩

*/

privateInteger score;

}

打印子类

1

2

3

4

5

6

7

8

9

10

11

12

13

publicstaticvoidmain (String[] args) {

StudentResp studentResp =newStudentResp();

//学生子类赋值

studentResp.setStudentId(1000000000);

studentResp.setScore(92);

//父类赋值

studentResp.setName("风清扬");

studentResp.setAge(18);

//学生子类

System.out.println(studentResp);

}

结果可以看到父类信息

再看看target中子类编译后的代码

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

publicclassStudentRespextendsPersonResp {

privateInteger studentId;

privateInteger score;

publicStudentResp() {

}

publicInteger getStudentId() {

returnthis.studentId;

}

publicInteger getScore() {

returnthis.score;

}

publicvoidsetStudentId(finalInteger studentId) {

this.studentId = studentId;

}

publicvoidsetScore(finalInteger score) {

this.score = score;

}

publicbooleanequals(finalObject o) {

if(o ==this) {

returntrue;

}elseif(!(oinstanceofStudentResp)) {

returnfalse;

}else{

StudentResp other = (StudentResp)o;

if(!other.canEqual(this)) {

returnfalse;

}else{

Objectthis$studentId =this.getStudentId();

Object other$studentId = other.getStudentId();

if(this$studentId ==null) {

if(other$studentId !=null) {

returnfalse;

}

}elseif(!this$studentId.equals(other$studentId)) {

returnfalse;

}

Objectthis$score =this.getScore();

Object other$score = other.getScore();

if(this$score ==null) {

if(other$score !=null) {

returnfalse;

}

}elseif(!this$score.equals(other$score)) {

returnfalse;

}

returntrue;

}

}

}

protectedbooleancanEqual(finalObject other) {

returnotherinstanceofStudentResp;

}

publicinthashCode() {

intPRIME =true;

intresult =1;

Object $studentId =this.getStudentId();

intresult = result *59+ ($studentId ==null?43: $studentId.hashCode());

Object $score =this.getScore();

result = result *59+ ($score ==null?43: $score.hashCode());

returnresult;

}

publicString toString() {

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

}

}

可以明确的看到最后的toString()方法中多了一个super.toString()方法,将父类的信息打印出来

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

子类使用@Setter,@Getter注解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@Getter

@Setter

publicclassStudentRespextendsPersonResp {

/**

* 学号

*/

privateInteger studentId;

/**

* 成绩

*/

privateInteger score;

}

父类中重写toString()方法并用JSON的方式打印

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@Data

publicclassPersonResp {

/**

* 姓名

*/

privateString name;

/**

* 年龄

*/

privateInteger age;

@Override

publicString toString() {

returnJSON.toJSONString(this);

}

}

打印子类

1

2

3

4

5

6

7

8

9

10

11

12

13

publicstaticvoidmain (String[] args) {

StudentResp studentResp =newStudentResp();

//学生子类赋值

studentResp.setStudentId(1000000000);

studentResp.setScore(92);

//父类赋值

studentResp.setName("风清扬");

studentResp.setAge(18);

//学生子类

System.out.println(studentResp);

}

结果也可以看到父类信息

总结

这个问题本身不算是个大问题,延伸出来更为重要的是我们解决问题的思路。因为从本质上来说属性本身都是存在的,只是没打印出来。

但是遇到这个问题的时候第一反应都是,明明继承了父类,为啥父类的值会没有,从而会带偏我们去解决问题方向,遇到此类问题,第一我们应该先确认问题的本质,到底有没有给上值,确认了已经给上值,就说明只是打印的问题,进而去编译后的代码中查看为啥没打印出来,最终定位到问题从而解决问题。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/25 22:52:23

Langflow第三方组件完全指南:从基础使用到高级集成

Langflow第三方组件完全指南:从基础使用到高级集成 【免费下载链接】langflow ⛓️ Langflow 是 LangChain 的用户界面,使用 react-flow 设计,旨在提供一种轻松实验和原型设计流程的方式。 项目地址: https://gitcode.com/GitHub_Trending/…

作者头像 李华
网站建设 2026/2/21 4:21:18

模型漂移的检测与应对:软件测试者的实战手册

当AI模型开始"失准"——测试工程师的新挑战 一、认识模型漂移:从静态测试到动态监控的范式转移 在传统软件测试中,我们习惯于对确定性的输入输出进行验证。但当系统引入机器学习模型后,我们面临的全新问题是:模型性能…

作者头像 李华
网站建设 2026/2/26 7:54:13

金融科技的智能风控测试

引言:智能风控测试的时代挑战 随着深度学习与大数据技术在金融风控领域的深度应用,传统基于规则引擎的测试方法已难以满足新一代智能风控系统的质量保障需求。测试工程师面临模型动态更新、数据维度多元、决策链路隐蔽等全新挑战,亟需建立适…

作者头像 李华
网站建设 2026/2/24 11:42:51

Open WebUI重排序终极指南:三步提升搜索精准度90%

Open WebUI重排序终极指南:三步提升搜索精准度90% 【免费下载链接】open-webui Open WebUI 是一个可扩展、功能丰富且用户友好的自托管 WebUI,设计用于完全离线操作,支持各种大型语言模型(LLM)运行器,包括O…

作者头像 李华
网站建设 2026/2/24 23:14:15

测试预算的动态优化:从静态规划到敏捷响应

在当今快速迭代的软件开发环境中,测试预算管理不再仅仅是年初的固定分配,而是一个需要持续调整的动态过程。软件测试从业者面临着诸多挑战:项目需求频繁变更、新技术工具涌现、测试环境成本波动,以及市场竞争对质量的更高要求。静…

作者头像 李华