技术开发 频道

ABAP类的方法

【IT168 技术文章】

    方法参数传递:
    REPORT z_mehtod_para.
    *---------------------------------------------------------------------*
    *        CLASS vehicle DEFINITION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS vehicle DEFINITION.
    PUBLIC SECTION.
    METHODS: accelerate IMPORTING rate TYPE i,
    show_speed.
    PRIVATE SECTION.
    DATA speed TYPE i VALUE 0.
    ENDCLASS.
    *---------------------------------------------------------------------*
    *        CLASS vehicle IMPLEMENTATION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS vehicle IMPLEMENTATION.
    METHOD accelerate.
    speed = speed + rate.
    ENDMETHOD.
    METHOD show_speed.
    WRITE: / 'Speed:',speed.
    ENDMETHOD.
    ENDCLASS.
    DATA o_vehicle TYPE REF TO vehicle.
    START-OF-SELECTION.
    CREATE OBJECT o_vehicle.
    CALL METHOD o_vehicle->accelerate EXPORTING rate = 2.
    CALL METHOD o_vehicle->accelerate EXPORTING rate = 2.
    CALL METHOD o_vehicle->show_speed.
    该示例输出如下:
    4
    方法还可以进行动态调用,即通过变量对方法进行指定,例如下列语句
    meth = acclerate.
    call method o_vehicle->(meth).
    函数方法:
    REPORT z_funtional_method.
    *---------------------------------------------------------------------*
    *        CLASS circle DEFINITION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS circle DEFINITION.
    PUBLIC SECTION.
    METHODS get_area IMPORTING value(i_radius) TYPE i
    RETURNING value(r_size)    TYPE f.
    PRIVATE SECTION.
    CONSTANTS pi TYPE f VALUE '3.14159265'.
    ENDCLASS.
    *---------------------------------------------------------------------*
    *        CLASS circle IMPLEMENTATION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS circle IMPLEMENTATION.
    METHOD get_area.
    r_size = i_radius ** 2 * pi.
    ENDMETHOD.
    ENDCLASS.
    PARAMETERS radius TYPE i.
    DATA: o_circle TYPE REF TO circle,
    area TYPE f.
    START-OF-SELECTION.
    CREATE OBJECT o_circle.
    CALL METHOD o_circle->get_area
    EXPORTING i_radius = radius
    RECEIVING r_size = area.
    WRITE: / area.
    area = o_circle->get_area( radius ).
    WRITE: / area.
    构造方法和析构方法
    1. 构造方法
    构造方法是运行时环境自动调用的一种方法,用于为对象设定一个初始化状态,不能在程序运行过程中由程序代码调用.与其他方法类似构造方法也存在静态和实例两种形式.构造方法不一定需要在类中定
    义,系统通常会自动生成一个默认的构造方法,将对象状态设置为初始值.
    构造方法中不包含任何形式的输出参数,其功能只是定义对象状态,而不是对其进行改变.构造方法在create object语句中被调用,因此根据实例构造方法的定义,该语句本身也可以包含输出参数exporting和
    返回值exception选项.如果在对象创建过程中出现异常,则对象被删除,引用变量设为初始值.
    类构造方法属于静态方法,只能在程序中被调用一次,即第一次使用该类时调用,该方法也必须在公有部分声明,其名称必须为class_constructor.
    class-methods class_constructor.
    类构造方法不包含接口,而且在其中只能访问类的静态属性.
    构造方法:
    REPORT z_method_constructor.
    *---------------------------------------------------------------------*
    *        CLASS vehicle DEFINITION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS vehicle DEFINITION.
    PUBLIC SECTION.
    METHODS: accelerate IMPORTING rate TYPE i,
    constructor IMPORTING i_speed TYPE i,
    show_speed.
    PRIVATE SECTION.
    DATA speed TYPE i VALUE 0.
    ENDCLASS.
    *---------------------------------------------------------------------*
    *        CLASS vehicle IMPLEMENTATION
    *---------------------------------------------------------------------*
    *        ........                                                       *
    *---------------------------------------------------------------------*
    CLASS vehicle IMPLEMENTATION.
    METHOD accelerate.
    speed = speed + rate.
    ENDMETHOD.
    METHOD show_speed.
    WRITE speed.
    ENDMETHOD.
    METHOD constructor.
    speed = i_speed.
    ENDMETHOD.
    ENDCLASS.
    DATA o_vehicle TYPE REF TO vehicle.
    START-OF-SELECTION.
    CREATE OBJECT o_vehicle EXPORTING i_speed = 4.
    CALL METHOD o_vehicle->accelerate EXPORTING rate = 2.
    CALL METHOD o_vehicle->accelerate EXPORTING rate = 2.
    CALL METHOD o_vehicle->show_speed.
    该示例输出如下:
    8
    在对象创建过程中,通过构造方法传递了一个速度初始值作为其初始状态.
    2. 析构方法
    在某些面向对象语言中还存在析构方法的概念,该方法在对象删除过程中被调用,目前ABAP对象中尚未提供该方法的实现形式.

0
相关文章