业务背景:
对参考变量赋值的研究。
- 解决步骤
逻辑验证代码如下:
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 |
CLASS c1 DEFINITION INHERITING FROM object. ENDCLASS. CLASS c2 DEFINITION INHERITING FROM c1. ENDCLASS. CLASS c3 DEFINITION INHERITING FROM c2. ENDCLASS. DATA oref TYPE REF TO object. DATA oref1 TYPE REF TO c1. DATA oref2 TYPE REF TO c2. DATA oref3 TYPE REF TO c3. * 向上转换,c2是oref1的子类 oref1 = NEW c2( ). * 向上转换,oref1是oref的子类 oref = oref1. * 向下转换,oref是oref2的父类 * 能转成功oref指向的是NEW c2()定义的类 IF oref IS INSTANCE OF c2. oref2 ?= oref. ENDIF. TRY. * 向下转换,oref2是oref3的父类 * 不能转成功是因为oref3是c3而oref2是c2的类 oref3 ?= oref2. CATCH cx_sy_move_cast_error INTO DATA(cx_error). DATA(msg) = cx_error->get_text( ). WRITE: msg. ENDTRY. WRITE: / '结论:', / '子类向父类的向上转换是可行的', / '父类向子类的向下转换是要看实例是不是子类的实例'. |
以上。
发表评论