访问ABAP中友元类的静态方法和属性

时间:2016-07-17 07:01:55

标签: class oop sap encapsulation abap

这是我的两个全局类的样子:

CLASS zcl_singleton_class DEFINITION CREATE PRIVATE friends ZCL_FLINFO 
PUBLIC SECTION. 

CLASS-METHODS: 
CLASS_CONSTRUCTOR,
get_instance 
    RETURNING VALUE(r_instance) TYPE REF TO zcl_singleton_class. 

PRIVATE SECTION. 
types:
TY_CONNECTION_LIST TYPE STANDARD TABLE OF SPFLI WITH  KEY carrid connid.

class-data instance type ref to zcl_singleton_class .
class-data CONNECTION_LIST type TY_CONNECTION_LIST . 
ENDCLASS.

CLASS zcl_singleton_class IMPLEMENTATION.

method CLASS_CONSTRUCTOR.
instance = instance.
SELECT * FROM SPFLI INTO TABLE CONNECTION_LIST.
endmethod.

METHOD get_instance. 
r_instance = instance. 
ENDMETHOD.  
ENDCLASS.


CLASS ZCL_FLINFO DEFINITION. 

PUBLIC SECTION. 
CLASS-METHODS: 
CLASS_CONSTRUCTOR,
get_connection
    IMPORTING im_carrid type S_CARR_ID 
        RETURNING VALUE(re_connection) TYPE.
ENDCLASS.


CLASS ZCL_FLINFO IMPLEMENTATION.  

 METHOD get_connection. 
LOOP at CONNECTION_LIST TRANSPORTING NO FIELDS WHERE carrid = im_carrid.
  re_connection = re_connection + 1.
ENDLOOP. 
ENDMETHOD.  
ENDCLASS.

如何实施get_connection ZCL_FLINFO方法,以便遍历CONNECTION_LIST的内部表zcl_singleton_class,以计算给定航空公司的连接数并在参数?

中返回它

1 个答案:

答案 0 :(得分:0)

在我的案例中,我发现了一些正常的工作。如果类A(zcl_singleton_class)为另一个类B(ZCL_FLINFO)提供友谊,则B可以访问A的私有组件。因此,我只需通过在循环中调用它来访问内部表(C​​ONNECTION_LIST)。

method GET_N_O_CONNECTIONS.
 LOOP AT zcl_singleton_class=>CONNECTION_LIST  TRANSPORTING NO FIELDS  WHERE  CARRID = IM_CARRID.
  RE_N_O_CONNECTIONS =  RE_N_O_CONNECTIONS + 1.
ENDLOOP.
endmethod.