Saturday, August 8, 2020

Parameters to instance constructors in abap (class 11)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL11
*& Parameters to Instance Constructors
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL11.
CLASS LCL_CUSTOMER DEFINITION.
  PUBLIC SECTION.
  METHODS CONSTRUCTOR IMPORTING IM_KUNNR TYPE KUNNR IM_NAME TYPE KNA1-NAME1,
            DISPLAY .
  PROTECTED SECTION.
  DATA KUNNR TYPE KUNNR,
         NAME1 TYPE NAME1.

  ENDCLASS.
CLASS LCL_CUSTOMER IMPLEMENTATION.
 METHOD DISPLAY.
   WRITE :/ KUNNRNAME1.
   ENDMETHOD.


  METHOD CONSTRUCTOR.
    KUNNR IM_KUNNR.
    NAME1 IM_NAME.

    ENDMETHOD.

  ENDCLASS.

PARAMETERS P_KUNNR TYPE KUNNR.

START-OF-SELECTION.
DATA OB TYPE REF TO LCL_CUSTOMER.


SELECT SINGLE NAME1 FROM KNA1 INTO @DATA(LV_NAMEWHERE KUNNR @P_KUNNR .
  IF SY-SUBRC IS INITIAL.
    CREATE OBJECT OB
    EXPORTING
      IM_KUNNR P_KUNNR
      IM_NAME LV_NAME.

  ELSE.
    MESSAGE' Customer not found' type 'I'.
    ENDIF.