当前位置: 首页 > news >正文

解决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);

}

结果也可以看到父类信息

总结

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

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

http://www.cnnetsun.cn/news/134935.html

相关文章:

  • Langflow第三方组件完全指南:从基础使用到高级集成
  • 【值得收藏】360大模型安全白皮书深度解析:五大风险+双轨防御+实战案例
  • 模型漂移的检测与应对:软件测试者的实战手册
  • 金融科技的智能风控测试
  • Open WebUI重排序终极指南:三步提升搜索精准度90%
  • 测试预算的动态优化:从静态规划到敏捷响应
  • 【树莓派pico/pico2】在pico-sdk中自定义板子
  • 【Java + Elasticsearch全量 增量同步实战】
  • 科研数据探索新维度:NSFC项目智能分析系统深度体验
  • 7、利用CardSpace和Windows Communication Foundation开发软件+服务
  • Scada-LTS开源项目完整使用指南:从零开始构建工业监控系统
  • 浏览器插件跨平台兼容性终极指南:5个核心技巧解决Chrome/Edge/Firefox差异
  • Godot-MCP革命:用AI对话创造你的梦想游戏世界
  • 大明开国勋臣的三重贡献:李善长、胡惟庸与蓝玉的历史功绩再审视
  • Python GUI终极指南:5步掌握DearPyGui的完整开发流程
  • Heroicons 2.1.5版本实战指南:23个新图标如何提升你的开发效率
  • python-flask-django学习课程辅助系统设计与实现_s01d6vz0
  • FLORIS风电场仿真实战:从入门到精通的终极指南
  • 机器学习图表设计专家:快速创建专业级科研可视化
  • 惠普游戏本终极性能控制指南:OmenSuperHub完全实战教程
  • 煤矿高压电缆绝缘监测技术深度解析:从局部放电到智能预警的科技防线
  • 收藏必备!LangGraph核心概念详解:从思维链到多智能体,一文掌握大模型应用架构
  • python-flask-django大学生健康管理系统_35l867i9
  • python-flask-django宠物商城 论坛领养系统_07ggc7q2
  • 46、《Linux使用技巧与技术综合指南》
  • SSLUnpinning_Xposed:Android安全测试终极指南
  • Kotaemon本地化部署方案:满足数据不出境要求
  • Blynk物联网开发完全指南:从零到一的智能硬件实战教程
  • 终极指南:如何彻底卸载Windows 10中的OneDrive
  • 这个”AI超级工程师“,已经帮2000多家企业省了27亿度电了!