adt abap class coding examples in abap adt btp









T ABAP ADT coding examples


D ABAP ADT coding examples
& Help ABAP ADT coding examples

Quick fix functionality to generate source code automatically



Place cursor in class name and Click Ctrl + 1
adt abap class coding examples step 1


Full code list of global class and local class


Global Class code listing


CLASS zcl_myglobal DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.

INTERFACES if_oo_adt_classrun .

"Should not have parameters as will be triggered at various points
CLASS-METHODS class_constructor.


" If you add importing parameters to constructor you then have to pass these when using NEW #( )
" exporting parameters not allowed
METHODS constructor.

PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.



CLASS zcl_myglobal IMPLEMENTATION.

METHOD class_constructor.
"static constructor trigger once per execution when class is first accessed/referenced
ENDMETHOD.

METHOD constructor.
" code that is executed whenever the class is instantiated
ENDMETHOD.

METHOD if_oo_adt_classrun~main.
DATA connection TYPE REF TO lcl_myclass.
DATA connection2 TYPE REF TO lcl_myclass.

"reference static methods using =>( )
lcl_myclass=>my_static( ).

"Create new instance of local class
connection = NEW #( ).

connection2 = connection.

" Use if constructor has importing parameters
"connection = NEW #( i_carrier_id = 'LH' i_connection_id = '0400' ).

"would fail as field is reado only from outside local class
" connection->carrier_id = 'LH'.

" this would be ok as not read only or private
" connection->connection_id = '0400'.
TRY.
connection->set_attributes( i_carrier_id = 'LH' i_connection_id = '0400' ).
CATCH cx_abap_invalid_value.
out->write( 'Please enter a connection ID' ).
ENDTRY.

TRY.
connection->set_attributes( i_carrier_id = 'LH' ).

CATCH cx_abap_invalid_value.
out->write( 'Please enter a connection ID' ).
ENDTRY.
out->write( 'Hello World' ).

"you can also call a static method using the - instance syntax
connection->my_static( ).

ENDMETHOD.
ENDCLASS.

Locall Class code listing


Rememeber go to the local types tab to access the local class code section
adt abap class coding examples step 2



*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations

"type lcl and press ctrl+space, then select the lcl class
CLASS lcl_myclass DEFINITION." CREATE PRIVATE.

PUBLIC SECTION.
DATA carrier_id TYPE /dmo/carrier_id READ-ONLY. "read only from outside the class
DATA connection_id TYPE /dmo/connection_id.

CLASS-DATA: conn_counter TYPE i,
n_o_connections TYPE i,
my_val TYPE i.

class-METHODS my_static. " static method use =>( )

METHODS set_attributes
IMPORTING
i_carrier_id TYPE /dmo/carrier_id OPTIONAL
i_connection_id TYPE /dmo/connection_id OPTIONAL
RAISING
cx_abap_invalid_value.

METHODS get_attributes
EXPORTING
e_carrier_id TYPE /dmo/carrier_id
e_connection_id TYPE /dmo/connection_id.

" Functional Method
METHODS get_output
RETURNING VALUE(r_output) TYPE string_table.
PROTECTED SECTION.
PRIVATE SECTION.
DATA private_id TYPE /dmo/carrier_id. "private means read and write access not aloud outside of class

ENDCLASS.

CLASS lcl_myclass IMPLEMENTATION.

METHOD set_attributes.
IF i_connection_id IS INITIAL.
RAISE EXCEPTION TYPE cx_abap_invalid_value.
ENDIF.

carrier_id = i_carrier_id.
connection_id = i_connection_id.
n_o_connections = n_o_connections + 1.

ENDMETHOD.

METHOD get_attributes.
e_carrier_id = carrier_id.
e_connection_id = connection_id.
ENDMETHOD.

METHOD get_output.
APPEND |------------------------------| TO r_output.
APPEND |Carrier: { carrier_id }| TO r_output.
APPEND |Connection: { connection_id }| TO r_output.

ENDMETHOD.

METHOD my_static.
my_val = 1.
ENDMETHOD.

ENDCLASS.