文章目录
一 事物代码 SE24
二 步骤
2.1 Create class
2.1.1.ZCL_CLASS_MATL
2.1.2 save button
实例化类型
public:该类可以在任何地方进行初始化
Protected:只能在自身和或类的子类中进行初始化
Private: 只能自身的类中进行初始化
Abstract: 不可以创建实例。
2.1.3 Create local object ( learning not production)
2.2 Creat attributes
In the fact attributes is the place of variable definition
2.2.1 Choose attributes
level
instance attribute : 相当于用data申明的变量
static attribute : 这个属性所有的对象实例都是共享的,这个静态属性由类的所有对象共有,(对象访问使用-> ,通过类名访问=>).
constant : (常量的属性)
visibility
public :公共可见
protected: (本身,子类,友元类)
PRIVATE (本身,友元类)
Read-only: 该属性不可以在类外被直接修改。
typing
like: 已经实例化的数据对象
type:没有实例化的类型
type ref to :后面跟类
Type boxed :结构嵌套的定义(数据值相同时,可以记录一条数据)
2.2.2(自定义类型如下)
2.2.3 代码定义三部 (结构体,内表,工作区)
2.2.4 四部区(结构休,表类型,内表, 工作区)
It is advice that four step is ok .
以下定义出来就这样。
我加了一个mv_name
2.3 Creat method
2.3.1 方法 建立
level : instance method :实例方法必须通过类的实例来使用
static Method :只能操作静态对象.
2.3.2 add parameter
IMORTING: 输入值
EXPORTING: 输出值
CHNAGING: 输出值
Returning: 输出一个参数
PASS VALUE: :是值传,还是传引用。
2.3.3 enter code
2.3.4 enter test_parameter
Now Please active it .
2.3.5 test code
REPORT zjgltest01.
DATA :go_class_matll TYPE REF TO zcl_class_matl.
DATA : gv_i_name TYPE char30 VALUE 'importing into class'.
DATA : gv_e_name TYPE char30 VALUE 'exporting from class'.
DATA : gv_c_name TYPE char30 VALUE 'changing in class'.
DATA : gv_r_name TYPE char30 VALUE 'returning from class'.
CREATE OBJECT go_class_matll.
CALL METHOD go_class_matll->test_parameter
EXPORTING
iv_name = gv_i_name " method imporing parameter
IMPORTING
ev_name = gv_e_name
CHANGING
cv_name = gv_c_name
RECEIVING
rv_name = gv_r_name.
WRITE:/ 'gv_i_name',gv_i_name.
WRITE:/ 'gv_e_name',gv_e_name.
WRITE:/ 'gv_c_name',gv_c_name.
WRITE:/ 'gv_r_name',gv_r_name.
The result
2.4 event
2.4.1 Create event
2.4.2 create method
2.4.3 SET METHOD and enter code and create new method and new parameter.
2.4.3 enter code
METHOD check_material.
SELECT matnr
mtart
FROM mara
INTO CORRESPONDING FIELDS OF TABLE et_material
WHERE matnr eq IV_MATERIAL_ID.
IF sy-subrc NE 0.
RAISE EVENT material_not_found.
ENDIF.
ENDMETHOD.
2.4.3 DEMO
REPORT zjgltest01.
DATA : gv_matnr TYPE mara-matnr.
DATA : gt_mara TYPE TABLE OF mara .
DATA : gs_mara TYPE mara.
DATA : go_inventory TYPE REF TO zcl_inventory.
CREATE OBJECT go_inventory.
PARAMETERS : p_mtart TYPE mara-matnr.
"Register event handler method
SET HANDLER go_inventory->handle_material_not_found FOR go_inventory.
START-OF-SELECTION.
CALL METHOD go_inventory->check_material
EXPORTING
iv_material_id = p_mtart
IMPORTING
et_material = gt_mara.
LOOP AT gt_mara INTO gs_mara.
WRITE :/ gs_mara-matnr, gs_mara-mtart.
ENDLOOP.
2.5. Class constructor
2.5.1 Create calss
2.5.2 ADD attributes
demo
REPORT zjgltest01.
START-OF-SELECTION.
DATA go_constructor TYPE REF TO zcl_constructor.
CREATE OBJECT go_constructor
EXPORTING
iv_value = 'CONSTRUCTOR'.
WRITE:/ 'CONSTRUCTOR TEST. '.
2.6. Class exception.
Demo
REPORT zjgltest01.
PARAMETERS : p_ivalue TYPE i.
DATA: lo_cx_root TYPE REF TO cx_root.
DATA: go_exception TYPE REF TO zcl_exception.
CREATE OBJECT go_exception.
*--------------------------------------------------------------
* Try and Catch method Exception
*--------------------------------------------------------------
TRY.
CALL METHOD go_exception->test_exception
CHANGING
cv_value = p_ivalue.
CATCH cx_root INTO lo_cx_root.
MESSAGE lo_cx_root->get_text( ) TYPE 'I'.
ENDTRY.
*--------------------------------------------------------------
* Call method and return Exception
*--------------------------------------------------------------
CALL METHOD go_exception->raise_exception
CHANGING
cv_value = p_ivalue
EXCEPTIONS
action_not_supported = 1
cancelled = 2
failed = 3
OTHERS = 4. "GET_RESULT_FAILED
IF sy-subrc EQ 1.
MESSAGE 'action_not_supported' TYPE 'I'.
ELSEIF sy-subrc EQ 2.
MESSAGE 'cancelled' TYPE 'I'.
ELSEIF sy-subrc EQ 3.
MESSAGE 'failed' TYPE 'I'.
ELSEIF sy-subrc EQ 4.
MESSAGE 'GET_RESULT_FAILED' TYPE 'I'.
ENDIF.
2.7 Friends Class
Friends class is not each other . it is single sided.
2.8 how to call class
The passage introduces that how to call class.
PROCEDURE
1.Define class with REF TO
2. CREATE OBJECT OR GO_MASTER = NEW ZCL_MASTER().
3. CALL METHOD (->)
REPORT zjgltest01.
DATA : GO_MASTER TYPE REF TO ZCL_MASTER.
DATA : GO_MASTER_NEW TYPE REF TO ZCL_MASTER.
CREATE OBJECT GO_MASTER.
GO_MASTER->DISPLAY_PUBLIC( ).
When in zcl_master we enter calling class code. With ref to class . you don’t call it directly with private and protected.
REPORT zjgltest01.
"定义调用类,定义类静态方法
CLASS zcl_main_report DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: run.
ENDCLASS.
"实现调用类方法,定义类静态方法
CLASS zcl_main_report IMPLEMENTATION.
METHOD run.
"创建对象 调用对象的方法
NEW zcl_master( )->display_public( ).
ENDMETHOD.
ENDCLASS.
"定义程序可执行块调用对象的静态方法
START-OF-SELECTION.
zcl_main_report=>run( ).
2.9 Free class
Free memory (Clear and free).
When I create class. it takes up memory. and when you don’t use it .you will free it as internal table.
Demo.
REPORT zjgltest01.
DATA:
go_obj TYPE REF TO cl_material_details_list.
"打印程序初始内存使用量
cl_abap_memory_utilities=>get_total_used_size( IMPORTING size = DATA(lv_init_size) ).
WRITE: / |内存初始用量: { lv_init_size } { 'Bytes' }| COLOR COL_POSITIVE.
DO 1000 TIMES.
CREATE OBJECT go_obj.
FREE go_obj.
ENDDO.
cl_abap_memory_utilities=>get_total_used_size( IMPORTING size = DATA(lv_before_size) ).
WRITE: / |创建并FREE类变量后内存用量: { lv_before_size } { 'Bytes' }| COLOR COL_GROUP.
cl_abap_memory_utilities=>do_garbage_collection( ).
cl_abap_memory_utilities=>get_total_used_size( IMPORTING size = DATA(lv_after_size) ).
WRITE: / |使用垃圾回收后内存用量: { lv_after_size } { 'Bytes' }| COLOR COL_TOTAL.
三 Summary
Class is not difficult. Perhaps we use method and attributes frequently.