技术开发 频道

ABAP类的基础知识

【IT168 技术文章】

    一、Class Define
    1) Basis Structure of Class
    CLASS c1 DEFINITION.
    PUBLIC SECTION.
    DATA: a1 …
    METHODS: m1 …
    EVENTS: e1 …
    PROTECTED SECTION.
    DATA: a2 …
    METHODS: m2 …
    EVENTS: e2 …
    PRIVATE SECTION.
    DATA: a3 …
    METHODS: m3 …
    EVENTS: e3 …
    ENDCLASS.

    CLASS c1 IMPLEMENTATION.
    METHOD m1. …
    ENDMETHOD.
    METHOD m2. …
    ENDMETHOD.
    METHOD m3. …
    ENDMETHOD.
    ENCLASS.

    2) Components of Classes: Attributes' Type
    CLASS ... DEFINITION
    ....
    ...SECTION.
    DATA... TYPE ... [READ-ONLY] ...
    CLASS-DATA... TYPE ... [READ-ONLY] ...
    CONSTANTS... TYPE ... VALUE ...
    ...
    ENDCLASS.
    DATA:                   Instance attributes
    CLASS-DATA:    Staticattributes
    CONSTANTS:    Constants

    3)Components of Classes:Methods' type
    CLASS ... DEFINITION
    ....
    ...SECTION.
    METHODS... IMPORTING [VALUE] ... TYPE ... [OPTIONAL]
    EXPORTING [VALUE] ... TYPE ...
    CHANGING [VALUE] ... TYPE ... [OPTIONAL]
    RETURNING VALUE(...) TYPE ...
    EXCEPTIONS ...
    CLASS-METHODS...
    ...
    ENDCLASS.
    METHODS:                Instance methods
    CLASS-METHODS: Staticmethods


    4)Constructors' type
    CLASS ... DEFINITION
    ....
    PUBLIC SECTION.
    METHODS CONSTRUCTOR
    [IMPORTING arg1 TYPEtype... ].
    CLASS-METHODS CLASS_CONSTRUCTOR.
    ...
    ENDCLASS.

    二、Using Object Step
    1)Declaring referencevariables
    DATA: ref1 TYPE REF TO class,
    ref2 TYPE REFTO  class.

    2)Creating objects
    DATA: CREATE OBJECT: ref1,

    3)Accessing attributes and methods
    ref2.x = ref1->attr+ ref2->attr.
    CALL METHOD ref1->methodEXPORTING ...

    三、Accessing the components of classes
    1)Instance components:           ref–>comp
    Instance attribute:                   ref->attr
    Instance method:                   call method ref->meth

    2)Static components                 class=>comp
    Static attribute:                        class=>attr
    Static method:                         call method class=>meth

    3)Special references in methods
    Self reference:               ME->comp
    Pseudo reference         SUPER->comp

    四、Class Inhireritance(ABAP只支持单继承)
    1)Inheritance-Overview
    CLASS c1 DEFINITION INHERITING FROM ...
    ...
    ENDCLASS.

    2)Redefining Methods
    CLASS ... DEFINITION INHERITING FROM ...
    ... SECTION.
    METHODS ... REDEFINITON ...
    ......
    ENDCLASS.

0
相关文章