Lsky Pro Logo

Class 基础

抽象类(abstract class)

Abstract class指的是用关键字abstract修饰的类,叫做抽象类,是不允许实例化的类,不能直接创建对象,必须要通过子类创建才能使用abstract类的方法。

为什么需要抽象类?

  • 抽象类是一种模板模式。抽象类为其子类提供了一个通用模板,子类可在这个模板基础上进行扩展。
  • 设计者通过定义抽象类达到严格限制子类设计的目的,要求子类要么也定义为抽象类要么重写抽象方法。

获取当前运行Class名称

DATA(lv_name) = cl_abap_classdescr=>describe_by_object_ref( me )->get_relative_name( ).

动态创建对象及方法调用

DATA: lv_classname  TYPE seoclsname,
      ls_result     TYPE z_s_bpm_result,
      lo_class      TYPE REF TO cl_oo_class,
      lt_interfaces TYPE seo_relkeys,
      ls_interfaces TYPE seorelkey,
      lo_object     TYPE REF TO object,
      lv_method     TYPE string.

SELECT SINGLE classnmae,doc_type INTO (@lv_classname, @DATA(lv_doc_type)) FROM zconf WHERE bpm_id = @is_return-processdefid.

TRY.
        CREATE OBJECT lo_class
          EXPORTING
            clsname                   = lv_classname
            with_inherited_components = seox_true
            with_interface_components = seox_true.
      CATCH cx_class_not_existent .
        rs_message-msg_text = '无效的类,请联系SAP管理员更新审批流配置类'.
        rs_message-msg_type = 'E'.
        RETURN.
    ENDTRY.
    IF lo_class IS NOT INITIAL.
      CALL METHOD lo_class->get_implemented_interfaces
        RECEIVING
          result = lt_interfaces.
    ENDIF.
    IF lt_interfaces IS NOT INITIAL.
      READ TABLE lt_interfaces INTO ls_interfaces WITH KEY refclsname = 'Z_IF_BPM'.
      IF sy-subrc = 0.
        CREATE OBJECT lo_object TYPE (lv_classname).
        IF lo_object IS NOT INITIAL.

          lv_method =  'Z_IF_BPM~SET_RESULT'.
          ls_result-appdate = sy-datum.
          ls_result-appstatus = is_return-spzt.
          ls_result-doc_type = lv_doc_type.

          ls_result-zuuid = is_return-processinstid.
          ls_result-db_key = is_return-processbusinesskey.
          ls_result-vars = is_return-vars.
          TRY.
              CALL METHOD lo_object->(lv_method)
                EXPORTING
                  is_result  = ls_result
                IMPORTING
                  es_message = rs_message.
            CATCH cx_sy_dyn_call_error ."INTO exc_ref.
              rs_message-msg_text = '无效的类,请联系SAP开发更新审批配置类方法'.
              rs_message-msg_type = 'E'.
              RETURN.
          ENDTRY.
          IF rs_message IS INITIAL.
            rs_message-msg_text = '状态更新成功'.
            rs_message-msg_type = 'S'.
          ELSEIF rs_message-msg_type EQ 'E'.
            rs_message-msg_text = rs_message-msg_text.
            rs_message-msg_type = 'E'.
          ENDIF.
          CLEAR lv_method.
        ENDIF.
      ELSE.
        rs_message-msg_text = '无效的类,请联系SAP开发更新审批配置类'.
        rs_message-msg_type = 'E'.
        RETURN.
      ENDIF.
    ENDIF.