开发者社区> jerrywangsap> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

ABAP,Java和JavaScript类的构造函数使用的一些陷阱

简介: ABAP,Java和JavaScript类的构造函数使用的一些陷阱
+关注继续查看

This question draws my attention during a discussion with my colleague recently.

I will explain it in ABAP, Java and JavaScript.


In ABAP

I have a super class and a sub class.

Source code for super class:


image.png

In constructor of super class, method set_i is called to set the member attribute mv_super to 100.

And here is my sub class:

image.png

The redefinition of set_i is done in order to fill the member attribute mv_sub in sub class.

And now in report, use this line for test:

image.png

in constructor, the redefinition of set_i done by sub class is not called at all, since the technical type of me reference points to super class. This makes sense since the execution context is constructor and the initialization of super class is not finished yet.


image.png


In Java

Let’s now see what will happen in Java.

image.pngimage.png

The constructor execution behavior is completely different in Java: in constructor, the redefinition of setX done in sub class now gets chance to be called. However, it is overwritten to default value 1 later.

Use Javap to analyze the byte code.

The magic of initialization lays in line 59: invokespecial #18.


image.png


the byte code clearly shows the Java code new SubClass() will invoke SubClass.””.

The #18 represents the entry in constant pool with id 18, which is SubClass.:


image.png


So use Javap to review byte code of sub class again.


Here explains why in the last line of output, subX is reset to 1, since the initialization of it is done AFTER super class’ constructor call.



image.png

What logic is contained in SuperClass.””? Inspect SuperClass.”” again:


image.png


#12 points to SuperClass.setX:


image.png


Byte code of setX:

line 59: put this reference to stack

line 60: push parameter x of method setX to stack

line 61: put stack top element to variable #20, which is SuperClass.mSuperX:


image.png



The execution sequence analyzed so far:

SubClass.”” -> SuperClass.”” -> SubClass.setX(int) -> SuperClass.setX(int)

Which could clearly be observed in callstack:

image.png



In JavaScript

I write an example via JavaScript to illustrate the execution logic in above example.

Super class:

image.png

image.png

Let’s step into code of new SubClass():


(1) it will call SuperClass’ constructor:

image.png



(2) In Super class constructor, since now this points to SubClass, so the redefinition of setX is called:

image.png


(3) In redefined setX in SubClass, line 23 and line 24 will set attribute mSuperX in SuperClass and mSubX in Sub class accordingly.


image.png


(4) finally:



image.png

So we get the same output in Java:


image.png


Conclusion

If you call a non-final method in constructor and that method is redefined by sub class, be careful that your code might not work as you expect when you create a new instance of sub class:


(1) In ABAP, the redefinition in sub class will NOT be called in constructor.

(2) In Java, the constructor of super class is called BEFORE the initialization of sub class member attribute.


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
🍉JavaScript进阶——构造函数和原型
🍉JavaScript进阶——构造函数和原型
0 0
Javascript面向对象——创建对象、构造函数的原型
Javascript面向对象——创建对象、构造函数的原型
0 0
JavaScript中的构造函数与原型
JavaScript中的构造函数与原型
0 0
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话):   有人说拖延症是一个绝症,哎呀治不好了。先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的影响,单单是自己的念想受到了一定得局限,想法不能够像平地而起的高楼大厦建成一样。
808 0
Javascript函数、构造函数、原型、类和对象
函数 函数是JavaScript中特殊的对象,对函数执行typeof运算会返回字符串"function",因为函数也是对象,他们可以拥有属性和方法。 静态方法 函数在JS中定义了类的名字,任何添加到函数的属性都是类字段和类方法(类比Java中类的static variable 和 static method),下面代码起到Java里面static method的效果: va
1021 0
利用JavaScript实现二级联动
利用JavaScript实现二级联动 要实现JavaScript二级联动效果,首先要确定需要哪些技术: 二维数组 for in循环 new Option(text,value,true,true) add(option,null) onchange() 表单事件 HTML代码: <!-- <input type="text" id="text"> --> 请选择省份: <select name="" id="provinces"> <!-- <option value="江苏省">江苏省</option>
0 0
JavaScript函数柯里化的实现原理,进来教你完成一个自己的自动实现柯里化方法
JavaScript函数柯里化的实现原理,进来教你完成一个自己的自动实现柯里化方法
0 0
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
0 0
JS中实现或退出全屏
JS中实现或退出全屏
0 0
+关注
文章
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
Javascript中的对象
立即下载
Java基础入门(四)-泛型、反射、注解
立即下载
Javascript中的函数
立即下载