ABAP学习之路 - Global Class

程序功能:根据航空公司编号(scarr-carrid)查找航空公司(scarr表)信息

学习点:Global Class, Static Method, Exception Handling, Structure

源代码:

1. Global Class(公用类)提供静态方法 GET_CARRIER, 以根据传入参数 IV-CARRID(航空公司编号)来查询航空公司信息

 1 class ZHRX_CLS_FLIGHTINFO definition

 2   public

 3   final

 4   create public .

 5 

 6 public section.

 7 

 8   class-methods GET_CARRIER

 9     importing

10       value(IV_CARRID) type SCARR-CARRID

11     exporting

12       !EV_CARRIER type SCARR

13     exceptions

14       EX_INVALID_INPUT

15       EX_ENTRY_NOT_FOUND .

16 protected section.

17 private section.

18 ENDCLASS.

19 

20 

21 

22 CLASS ZHRX_CLS_FLIGHTINFO IMPLEMENTATION.

23 

24 

25 * <SIGNATURE>---------------------------------------------------------------------------------------+

26 * | Static Public Method ZHRX_CLS_FLIGHTINFO=>GET_CARRIER

27 * +-------------------------------------------------------------------------------------------------+

28 * | [--->] IV_CARRID                      TYPE        SCARR-CARRID

29 * | [<---] EV_CARRIER                     TYPE        SCARR

30 * | [EXC!] EX_INVALID_INPUT

31 * | [EXC!] EX_ENTRY_NOT_FOUND

32 * +--------------------------------------------------------------------------------------</SIGNATURE>

33 method GET_CARRIER.

34   if ( iv_carrid is INITIAL ).

35     raise ex_invalid_input.

36   ELSE.

37     select single * from scarr

38       into ev_carrier

39       where carrid = iv_carrid.

40     if ( sy-subrc <> 0 ).

41       raise ex_entry_not_found.

42     endif.

43   endif.

44 endmethod.

45 ENDCLASS.

 

2. 提供用户输入和展示查询结果的可执行 Program

 1 *&---------------------------------------------------------------------*

 2 *& Report  ZHRX_PROG_CALL_CLS_METHOD

 3 *&

 4 *&---------------------------------------------------------------------*

 5 *& Get airline info by airline code, use global class static method

 6 *& by Rixin Hu

 7 *& 2014-12-04

 8 *&---------------------------------------------------------------------*

 9 

10 REPORT  ZHRX_PROG_CALL_CLS_METHOD.

11 

12 PARAMETERS pa_carr TYPE scarr-carrid.

13 

14 DATA: gs_carrier type scarr.

15 

16 CALL METHOD ZHRX_CLS_FLIGHTINFO=>GET_CARRIER

17   EXPORTING

18     IV_CARRID          = pa_carr

19   IMPORTING

20     EV_CARRIER         = gs_carrier

21   EXCEPTIONS

22     EX_INVALID_INPUT   = 1

23     EX_ENTRY_NOT_FOUND = 2

24 *    others             = 3

25         .

26 *IF SY-SUBRC <> 0.

27 * Implement suitable error handling here

28 case sy-subrc.

29   WHEN 0.

30     write: / gs_carrier-mandt,

31            / gs_carrier-carrid,

32            / gs_carrier-carrname,

33            / gs_carrier-currcode,

34            / gs_carrier-url.

35   when 1.

36     write: / 'Please enter valid airline code'.

37   when 2.

38     write: / 'Entry not found'.

39   ENDCASE.

40 *ENDIF.

 

你可能感兴趣的:(global)