news 2026/4/16 12:58:59

解决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/15 3:46:24

简单快速实现工业质检:Ultralytics灰度检测终极方案

简单快速实现工业质检:Ultralytics灰度检测终极方案 【免费下载链接】ultralytics ultralytics - 提供 YOLOv8 模型,用于目标检测、图像分割、姿态估计和图像分类,适合机器学习和计算机视觉领域的开发者。 项目地址: https://gitcode.com/G…

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

智能测试的团队能力评估:迈向高效与自动化的关键

随着人工智能和机器学习技术的快速发展,软件测试领域正经历一场深刻的智能化变革。智能测试不仅提升了测试效率,还通过自动化脚本、预测性分析和自适应学习,改变了传统的测试模式。然而,这种变革对测试团队的能力提出了新的挑战&a…

作者头像 李华
网站建设 2026/4/15 13:47:38

企业微信会话存档终极指南:5步高效实现合规数据管理

企业微信会话存档终极指南:5步高效实现合规数据管理 【免费下载链接】WeWorkFinanceSDK 企业微信会话存档SDK(基于企业微信C版官方SDK封装) 项目地址: https://gitcode.com/gh_mirrors/we/WeWorkFinanceSDK 企业微信会话存档作为企业合…

作者头像 李华
网站建设 2026/4/16 11:07:25

Day 38

# DAY 38 Dataset 和 Dataloader 类知识点回顾: 1. Dataset 类的__getitem__和__len__方法(本质是 python 的特殊方法) 2. Dataloader 类 3. minist 手写数据集的了解 作业:了解下 cifar 数据集,尝试获取其中一张图片 # 1. 导…

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

python-flask-django校园二手交易系统的设计与开发_42194l18

文章目录系统截图项目技术简介可行性分析主要运用技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式!系统截图 Python-flask-django_94了8 校园二手交易系统的设计与开发 项目技术简介 Python版本&#x…

作者头像 李华