In this exercise, you create and manage instances of your local class.
Task 1: Copy Template (Optional)
Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_LOCAL_CLASS or your class ZCL_##_INSTANCES.
Steps
Open the source code of global class/LRN/CL_S4D400_CLS_INSTANCES in the ABAP editor.
In the Eclipse toolbar, choose Open ABAP Development Object. Alternatively, press Ctrl + Shift + A.
Enter /LRN/CL_S4D400_CLS as search string.
From the list of development objects choose /LRN/CL_S4D400_CLS_INSTANCES, then choose OK.
Link the Project Explorer view with the editor.
In the Project Explorer toolbar, find the Link with Editor button. If it is not yet pressed, choose it. As a result, the development object, that is open in the editor, should be highlighted in the Project Explorer.
Copy class /LRN/CL_S4D400_CLS_INSTANCES to a class in your own package (suggested name: ZCL_##_METHODS, where ## stands for your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D400_CLS_INSTANCES to open the content menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_METHODS, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Task 2: Define Methods
In your local class lcl_connection, define instance methods get_output and set_attributes.
Steps
Navigate to the definition of the local class lcl_connection.
In the global class, choose Local Types.
Add a method get_output to the public section of the class. It should have one returning parameter r_outputof global table typeSTRING_TABLE.
Note
Remember to surround the name of the returning parameter with VALUE( )
.Adjust the code as follows:
1234567891011121314
PUBLIC SECTION.
DATA carrier_id TYPE /dmo/carrier_id.
DATA connection_id TYPE /DMO/Connection_id.
CLASS-DATA conn_counter TYPE i.
METHODS get_output
returning
value(r_output) type string_table.
PROTECTED SECTION.
Note
For the moment, ignore the syntax error Implementation missing for method "GET_OUTPUT".
Add a method set_attributes to the public section of the class. It should have one importing parameter for each instance attribute of the class. Use the same types for the parameters that you used to type the attributes. To distinguish the parameters from the attributes, add prefix i_ to the parameter names. In addition, the method should raise exception CX_ABAP_INVALID_VALUE.
Adjust the code as follows:
123456789101112131415161718192021
PUBLIC SECTION.
DATA carrier_id TYPE /dmo/carrier_id.
DATA connection_id TYPE /DMO/Connection_id.
CLASS-DATA conn_counter TYPE i.
METHODS set_attributes
IMPORTING
i_carrier_id TYPE /dmo/carrier_id
i_connection_id TYPE /dmo/connection_id
RAISING
cx_abap_invalid_value.
METHODS get_output
returning
value(r_output) type string_table.
PROTECTED SECTION.
Note
For the moment, ignore the syntax error Implementation missing for method "SET_ATTRIBUTES".
Task 3: Implement Methods
Steps
Use a quick fix to add the method implementations to the class.
Position the cursor on the name of one of the methods in the editor and press Ctrl + 1.
Double-click the suggestion Add 2 unimplemented methods.
Implement the method get_output. Append some string templates to returning parameter r_output. In the string templates, use embedded expressions to add the values of attributes carrier_id and connection_id.
In the local class add the following code between the statements METHOD get_output. and ENDMETHOD.
12345
APPEND |------------------------------| TO r_output.
APPEND |Carrier: { carrier_id }| TO r_output.
APPEND |Connection: { connection_id }| TO r_output.
Implement the method set_attributes. If either of the two importing parameters is empty (IS INITIAL), raise exception CX_ABAP_INVALID_VALUE. Otherwise, fill the two instance attributes with the values of the importing parameters.
Adjust the code as follows:
123456789101112
METHOD set_attributes.
IF i_carrier_id IS INITIAL OR i_connection_id IS INITIAL.
RAISE EXCEPTION TYPE cx_abap_invalid_value.
ENDIF.
carrier_id = i_carrier_id.
connection_id = i_connection_id.
ENDMETHOD.
Activate your class.
Choose Ctrl + F3 to activate the class.
Task 4: Call a Functional Method
In the main method of your global class, call the functional method get_output.
Steps
Switch to the implementation of method if_oo_adt_classrun~main in the global class.
In the global class, choose Global Class scroll down to the implementation of method if_oo_adt_classrun~main.
At the end of the method, add a loop over the internal table connections with reference variable connection as a work area.
Add the following code before ENDMETHOD..
12345
LOOP AT connections INTO connection.
ENDLOOP.
In the loop, call functional method get_output for each instance in turn and write the result to the console.
Hint
You can use the call of method get_output directly as input for out->write( ).Adjust the code as follows:
1234567
LOOP AT connections INTO connection.
out->write( connection->get_output( ) ).
ENDLOOP.
Activate the class. Execute it as a console app and analyze the console output.
Press Ctrl + F3 to activate the class.
Press F9 to run the class.
Analyze the output on the Console window.
Task 5: Use Code Completion and Handle Exceptions
Use code completion to call the method set_attributes and handle the exception the method raises.
Steps
For the first instance of class lcl_connection, replace the direct access to attributes carrier_id and connection_id with a call of the instance method set_attributes( ). Use code completion to insert the full signature of the method.
Hint
Press Ctrl + Space after you typed the component select (->) to display a list of available attributes and methods. Choose the method and then press Shift + Enter to insert the full signature of the method.Place the cursor in the next line after the first connection = NEW #( )..
Type connection-> and press Ctrl + Space.
Choose set_attributes and press Shift + Enter.
Pass values to the importing parameters. Use the same literals that you used previously to set the attributes of this instance.
Remove or comment the direct access to the instance attributes for this instance.
Handle the exception. Surround the method call with TRY. and ENDTRY.. Add a CATCH-Block to handle exception CX_ABAP_INVALID_VALUE. Make sure the new instance is only added to internal table connections if the method call was successful.
Uncomment the generated code line CATCH cx_abap_invalid_value. .
Add TRY. before the method call .
Add ENDTRY. after the CATCH statement.
Between the CATCH statement and the ENDTRY statement add out->write( `Method call failed`Â ).
Move the APPEND statement up to between the method call and CATCH statement.
The complete method call should now look like this:
1234567891011121314151617
TRY.
connection->set_attributes(
EXPORTING
i_carrier_id = 'LH'
i_connection_id = '0400'
).
* connection->carrier_id = 'LH'.
* connection->connection_id = '0400'.
APPEND connection TO connections.
CATCH cx_abap_invalid_value.
out->write( `Method call failed` ).
ENDTRY.
Repeat the previous step for the other two instances of class lcl_connection.
After this step, the implementation of method if_oo_adt_classrun~main should look similar to this:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
METHOD if_oo_adt_classrun~main.
DATA connection TYPE REF TO lcl_connection.
DATA connections TYPE TABLE OF REF TO lcl_connection.
* First Instance
**********************************************************************
connection = NEW #( ).
TRY.
connection->set_attributes(
EXPORTING
i_carrier_id = 'LH'
i_connection_id = '0400'
).
* connection->carrier_id = 'LH'.
* connection->connection_id = '0400'.
APPEND connection TO connections.
CATCH cx_abap_invalid_value.
out->write( `Method call failed` ).
ENDTRY.
* Second instance
**********************************************************************
connection = NEW #( ).
TRY.
connection->set_attributes(
EXPORTING
i_carrier_id = 'AA'
i_connection_id = '0017'
).
* connection->carrier_id = 'AA'.
* connection->connection_id = '0017'.
APPEND connection TO connections.
CATCH cx_abap_invalid_value.
out->write( `Method call failed` ).
ENDTRY.
* Third instance
**********************************************************************
connection = NEW #( ).
TRY.
connection->set_attributes(
EXPORTING
i_carrier_id = 'SQ'
i_connection_id = '0001'
).
* connection->carrier_id = 'SQ'.
* connection->connection_id = '0001'.
APPEND connection TO connections.
CATCH cx_abap_invalid_value.
out->write( `Method call failed` ).
ENDTRY.
* Output
**********************************************************************
LOOP AT connections INTO connection.
out->write( connection->get_output( ) ).
ENDLOOP.
ENDMETHOD.
Activate the class. Execute it and debug the method calls.
Press Ctrl + F3 to activate the class.
Press F9 to run the class.