上面是实例代码 首先定义一个父类
zcl_super_class_mat
记住不能让他 final,
final 根本完犊子了,继承不了,能够了解为爸爸变成太监了。
那么咱们看看继承代码
zcl_sub_class_mat_raw 继承自 zcl_super_class_mat
咱们能够从新定义 zcl_super_class_mat 的办法,这是什么意思?就是说我尽管来源于你,然而我能够在我的办法里干别的事件。
我还能够干别的事件(本人的办法),当然 zcl_sub_class_mat_raw 如果不定义成 final 我感觉还能够继承上来
始终继承到脑瓜子嗡嗡的。
还有一个知识点就是父亲的公有办法是不能继承的。
子类继承父类后从新实现父类的办法,
写的是在子类办法结尾加上 REDEFINITION(重定义),
从新定义方法时,办法的参数是不能扭转的,
如果肯定要扭转设定新的输入输出参数,能够思考减少子类的新的属性,而后用从新定义的办法拜访这些新的属性作为参数。
继承父类的结构器是不能从新定义的
。
子类是能够拜访父类的
例如父类有个办法 method01.
在重定义的子类办法 method01 外面采纳
call method super->method01
这个还必须是 在子类的 method01 外面 不能在子类的 method02 外面。
可能集体了解的不对,请多多指教。
CLASS zcl_super_class_mat DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
DATA:mv_material_id TYPE mara-matnr.
METHODS set_material_id
IMPORTING
!iv_material_id TYPE mara-matnr.
PROTECTED SECTION.
DATA mv_material_name TYPE makt-maktx.
METHODS get_material_name
RETURNING VALUE(rv_material_name) TYPE makt-maktx.
PRIVATE SECTION.
DATA mv_material_desc TYPE string.
METHODS get_material_desc
RETURNING VALUE(rv_material_desc) TYPE string.
ENDCLASS.
CLASS zcl_super_class_mat IMPLEMENTATION.
METHOD get_material_desc.
rv_material_desc = mv_material_desc.
ENDMETHOD.
METHOD get_material_name.
rv_material_name = mv_material_desc.
ENDMETHOD.
METHOD set_material_id.
mv_material_id = iv_material_id.
WRITE: / 'Material:',mv_material_id.
ENDMETHOD.
ENDCLASS.
CLASS zcl_sub_class_mat_raw DEFINITION
PUBLIC
INHERITING FROM zcl_super_class_mat
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS get_material_type
IMPORTING
!iv_material_id TYPE mara-matnr
RETURNING VALUE(rv_material_type) TYPE mara-mtart.
METHODS set_material_id
REDEFINITION .
PROTECTED SECTION.
METHODS get_material_name
REDEFINITION .
PRIVATE SECTION.
DATA mv_material_type TYPE mara-mtart.
ENDCLASS.
CLASS zcl_sub_class_mat_raw IMPLEMENTATION.
METHOD get_material_name.
*CALL METHOD SUPER->GET_MATERIAL_NAME
* RECEIVING
* RV_MATERIAL_NAME =
* .
* mv_material_id = iv_material_id.
WRITE: / 'RAW Material:',mv_material_id.
ENDMETHOD.
METHOD get_material_type.
* mv_material_id = iv_material_id.
* WRITE: / 'RAW material:',mv_material_id.
DATA:ls_mara TYPE mara.
SELECT SINGLE mtart FROM mara INTO (ls_mara-mtart)
WHERE matnr = iv_material_id.
IF sy-subrc = 0.
rv_material_type = ls_mara-mtart.
ELSE.
rv_material_type = ''.
ENDIF.
WRITE:/ 'material_type:',rv_material_type.
ENDMETHOD.
METHOD set_material_id.
*CALL METHOD SUPER->SET_MATERIAL_ID
* EXPORTING
* IV_MATERIAL_ID =
* .
mv_material_id = iv_material_id.
WRITE: / 'Raw Material:', mv_material_id.
ENDMETHOD.
ENDCLASS.