几种流行的面向对象编程语言里构造函数 construct 的使用辨析
【摘要】 如果constructor里调用了一个成员方法,这个方法被子类override了,当初始化一个子类实例时,父类的构造函数被的调用,此时父类构造函数的上下文里调用的成员方法,是父类的实现还是子类的实现?你能不用运行代码,就能准确说出这些语句会打印什么出来呢? ABAPclass ZCL_SUPER definition public create public .public sectio...
如果constructor里调用了一个成员方法,这个方法被子类override了,当初始化一个子类实例时,父类的构造函数被的调用,此时父类构造函数的上下文里调用的成员方法,是父类的实现还是子类的实现?
你能不用运行代码,就能准确说出这些语句会打印什么出来呢?
ABAP
class ZCL_SUPER definition
public
create public .
public section.
methods CONSTRUCTOR .
methods SET_I
importing
!IV_I type INT4 .
protected section.
private section.
data MV_SUPER type INT4 .
ENDCLASS.
CLASS ZCL_SUPER IMPLEMENTATION.
method CONSTRUCTOR.
me->set_i( 100 ).
endmethod.
method SET_I.
me->mv_super = iv_i.
endmethod.
ENDCLASS.
class ZCL_SUB definition
public
inheriting from ZCL_SUPER
final
create public .
public section.
methods PRINT .
methods SET_I
redefinition .
protected section.
private section.
data MV_SUB type I value 1 ##NO_TEXT.
ENDCLASS.
CLASS ZCL_SUB IMPLEMENTATION.
method PRINT.
WRITE: / ' sub:' , mv_sub.
endmethod.
METHOD set_i.
super->set_i( iv_i = iv_i ).
me->mv_sub = iv_i.
WRITE: / 'mv_sub assigned by: ' , iv_i.
ENDMETHOD.
ENDCLASS.
测试:
NEW zcl_sub( )->print( ).
测试结果:sub: 1
Java
public class SuperClass {
private int mSuperX;
public SuperClass() {
setX(99);
}
public void setX(int x) {
mSuperX = x;
}
}
public class SubClass extends SuperClass {
private int mSubX = 1;
public SubClass() {}
@Override
public void setX(int x) {
super.setX(x);
mSubX = x;
System.out.println("SubX is assigned " + x);
}
public void printX() {
System.out.println("SubX = " + mSubX);
}
}
test:
public static void main(String[] args) {
SubClass sc = new SubClass();
sc.printX();
}
测试结果:
JavaScript
function SuperClass(){
this.setX(99);
}
SuperClass.prototype = {
mSuperX : 0,
setX : function(x){
this.mSuperX = x;
}
};
function SubClass(){
SuperClass.call(this);
this.mSubX = 1;
}
SubClass.prototype = new SuperClass();
SubClass.prototype.setX = function(x){
SuperClass.prototype.setX(x);
this.mSubX = x;
console.log("SubX is assigned " + x);
};
SubClass.prototype.print = function(){
console.log("SubX: " + this.mSubX);
}
test:
var sub = new SubClass();
sub.print();
测试结果:
详细分析在我的SAP community博客里: A reminder for using constructor in OO world
ABAP 的一大特色
report z.
INCLUDE ole2incl.
DATA: ole TYPE ole2_object,
voice TYPE ole2_object,
text TYPE string.
text = 'With the advent of ES6 (referred to as ES2015 from here on), which not only made promises native to the language without requiring one of the countless available libraries,'
&& 'we also got generators. Generators have the ability to pause execution' &&
'within a function, which means that by wrapping them in a utility function, ' &&
'we have the ability to wait for an asynchronous operation to finish before' &&
' moving on to the next line of code. Suddenly your asynchronous code could' &&
' start to look synchronous!'.
DATA: it_tline TYPE STANDARD TABLE OF tline.
CREATE OBJECT voice 'SAPI.SpVoice'.
CALL METHOD OF voice 'Speak' = ole
EXPORTING #1 = text.
*
用的也是很老的OLE技术:
report代码直接call的MS的sound engine,通过sapi.dll暴露出来,
这个report只是call了dll里其中一个speak方法:
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)