news 2026/4/18 10:24:31

解决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/4/18 8:48:16

罗福莉的两个反共识 Hybrid Attention架构(混合注意力),其中,Hybrid Sliding Window Attention(混合滑动窗口注意力)和 Full Attention(全局

罗福莉的两个反共识 原创 邱晓芬 智能涌现 2025年12月17日 16:39 北京 在小说阅读器中沉浸阅读 1、AI的发展根基不稳固,像是空中楼阁;2、数据和算力,不是AI真正的护城河。 文|邱晓芬 编辑|苏建勋 官宣加入小…

作者头像 李华
网站建设 2026/4/18 8:08:30

EtherCAT 转 Modbus RTU 协议模块:实现基恩士 PLC 与捷顺 JS601 道闸控制器快速通行通讯

一、项目背景某大型工业制造园区为提升厂区车辆出入管控效率,推进园区智能化改造,需搭建生产区 - 仓储区 - 办公区多出入口车辆联动管理系统。系统核心需求为实现道闸设备与园区中央控制系统的数据互通,根据车辆授权信息自动完成道闸开合、通…

作者头像 李华
网站建设 2026/4/18 10:07:41

Astra Toolbox 三维重建完全指南:从入门到精通

Astra Toolbox 三维重建完全指南:从入门到精通 【免费下载链接】astra-toolbox ASTRA Tomography Toolbox 项目地址: https://gitcode.com/gh_mirrors/as/astra-toolbox 在医学影像、工业检测和科学研究领域,三维重建技术正发挥着越来越重要的作用…

作者头像 李华
网站建设 2026/4/18 10:05:15

边缘AI的测试验证挑战:从云到端的质量保障体系重构

随着AI模型从云端下沉至终端设备,软件测试正在面临前所未有的范式转移 1 边缘AI测试的独特性与复杂性 边缘AI将人工智能模型的推理能力部署在终端设备上,这种架构变革带来了显著的测试复杂度提升。与传统软件测试相比,边缘AI测试呈现出三个关…

作者头像 李华
网站建设 2026/4/18 6:29:09

终极指南:快速上手 node-qrcode 二维码生成神器

终极指南:快速上手 node-qrcode 二维码生成神器 【免费下载链接】node-qrcode qr code generator 项目地址: https://gitcode.com/gh_mirrors/no/node-qrcode 想要在 Node.js 项目中轻松生成专业二维码吗?node-qrcode 就是你的完美选择&#xff0…

作者头像 李华
网站建设 2026/4/18 6:28:15

LeetCode 面试经典 150_回溯_组合(99_77_C++_中等)

LeetCode 面试经典 150_回溯_组合(99_77_C_中等)题目描述:输入输出样例:题解:解题思路:思路一(回溯):代码实现代码实现(思路一(回溯)&…

作者头像 李华