Você está na página 1de 19

(OOP ABAP Topics)

o
o o

o o o o
o

OOP-Language Features: Encapsulation, Inheritance, Polymorphism, & Abstraction. Class, Reference Variable, and Object. Components of a class: Attributes, Methods, Interfaces & Events. Visibility sections in a class: Public, Protected and Private. A class with public attributes & methods CREATE OBJECT command. A class with Private attributes and Public methods. Constructor Inheritance (Super class and Sub class) Without Overriding With Overriding Containment Is-A & Has-A relationships between classes Functional & Non-functional methods. Different syntaxes to call Functional & Non-functional methods. Class memory & Instance/Object memory Static members (a.k.a Class members) & Non-Static members (a.k.a Instance members) Friends Interfaces Single interface inheritance Aliasing Multiple interface inheritance Global classes (SE24) Events Simple event Event that carries data OOP ALV Interactive OOP ALV (using OOP events)

o
o

o o o
o

o o
o

o o

CLASS: When we define a class, we are essentially defining a new data type. A data type defined as a class is known as Reference Type. When we define a structure, then too we are defining a new data type. A structure is a collection of related fields, but a structure has no processing logic of its own (processing logic: code to assign, read and/or manipulate data in the fields of that structure). A class is similar to a structure in a way that it can hold a set of related fields, but in addition to this it can have its own processing logic in the form of methods. Two types of classes in ABAP: Global Class: These types of classes are created using class builder tool and will be available globally for all ABAP programs. (Defined using T.Code SE24) Local Class: These types of classes are created in an ABAP program and will ba available only to that program where it defined. A class in ABAP OOP is coded in two parts: 1. Class Definition. 2. Class Implementation. A Class Definition contains declarations of fields and methods but not processing logic. The processing logic is provided in Class Implementation. Therefore, a Class Definition must precede Class Implementation, i.e., the definition has to be coded first and only then the implementation can be coded. Every class can have 3 sections: 1. Public 2. Protected 3. Private These sections determine the visibility (access) levels of the components of a class (* Visibility/access levels will be dealt with later.) Members / Components of a Class: The contents of a class are referred to as members of a class or components of a class, which include: 1. Attributes (also called as Data Components) 2. Methods 3. Interfaces and 4. Events. All the above mentioned components of a class (except for Interfaces) can be declared in any of the three sections (public, protected, & private sections) of a class. Interfaces can be placed only in public section. Attributes / Data Components: Attributes of a class are the data fields (variables & constants) declared in a class. Attributes of a class can be declared using the keywords DATA or CLASS-DATA. Methods: Methods are similar to subroutines or function modules in the sense that they can accept parameters, contain some code (processing logic) to act on the parameters, except for that they are contained in a class. Methods of a class can be declared using the keywords METHODS or CLASSMETHODS. Interfaces and Events will be dealt with later. Public Section: There are no restrictions on the visibility / access of the components placed in public section. That is the public components are visible / accessible: 1. Within class in which the components are declared, 2. In the subclasses, and 3. Outside the class inheritance hierarchy. Protected Section: There is one restriction on the components placed in protected section. The components of this section are visible / accessible:

1. Within class in which the components are declared, 2. In the subclasses, but 3. Not accessible outside the class inheritance hierarchy. Private Section: The visibility of the components placed in private section is highly restricted as they are visible only within the class and nowhere else. Example: The class LCL_EMP defined below has only one section (public) and has only attributes defined in it. Syntax for creating objects: CREATE OBJECT cref [TYPE class].
*----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i. METHODS: set_details IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i, show_details. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD set_details. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. ENDMETHOD. "set_details METHOD show_details. WRITE: / 'ENo:', eno, 'EName:', ename, 'DNo:', dno. ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp, e2 TYPE REF TO lcl_emp, e3 TYPE REF TO lcl_emp. START-OF-SELECTION. CREATE OBJECT: e1, e2. e1->set_details( EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 ).

e2->set_details( EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30 ). e1->show_details( ). e2->show_details( ). *& Bypassing the validation in set_details method *& for valid dept number, by directly accessing the *& the attribute 'dno' (To avoid this place the attributes *& in PRIVATE SECTION) e2->dno = 55. skip 2. e2->show_details( ). *& The below line of code if uncommented results in runtime *& error as the object for e3 is not yet created * e3->set_details( EXPORTING im_eno = 103 * im_ename = 'Vinay' * im_dno = 20 ). *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. METHODS: set_details IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i, show_details. PRIVATE SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD set_details. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. ENDMETHOD. "set_details METHOD show_details. WRITE: / 'ENo:', eno, 'EName:', ename, 'DNo:', dno. ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp, e2 TYPE REF TO lcl_emp. . START-OF-SELECTION.

CREATE OBJECT: e1, e2. e1->set_details( EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 ). e2->set_details( EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30 ). e1->show_details( ). e2->show_details( ). *& As the attributes are placed in private section they *& cannot be accessed directly outside the class definition. *& The below line of code "e2->dno = 55." is a syntax error e2->dno = 55. SKIP 2. e2->show_details( ).

CONSTRUCTOR: Constructor is a special method that gets implicitly called immediately after an object is created using the CREATE OBJECT command. There is no need to explicitly call the constructor method; in fact, the constructor method cannot be called explicitly. The name of this method has to be CONSTRUCTOR. The CREATE OBJECT command performs 3 tasks: 1. Allocates the memory required for an objects attributes (* Instance attributes only) . 2. Calls the method with the name constructor of the corresponding class. Returns the reference of the object to the associated reference variable.
*----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i, show_details. PRIVATE SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD constructor. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. ENDMETHOD. "set_details

METHOD show_details. WRITE: / 'ENo:', eno, 'EName:', ename, 'DNo:', dno. ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp, e2 TYPE REF TO lcl_emp, e3 TYPE REF TO lcl_emp. START-OF-SELECTION. CREATE OBJECT: e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10, e2 EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30. e1->show_details( ). e2->show_details( ).

Inheritance: Inheritance is a relationship in which one class (the sub class) inherits all the main characteristics of another class (the Super class). The sub class can also add new components (attributes, methods and so on) and replace or extend inherited methods with its own implementations. The addition INHERITING FROM is used in the class definition to indicate from which other class the current class is inheriting from.
*----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i, show_details. PRIVATE SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD constructor. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. ENDMETHOD. "set_details METHOD show_details. WRITE: / 'ENo:', eno,

'EName:', ename, 'DNo:', dno. ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_hwage_emp DEFINITION INHERITING FROM lcl_emp. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_hwage TYPE i, show_hwage. PRIVATE SECTION. DATA: hwage TYPE i. ENDCLASS. "lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS lcl_hwage_emp IMPLEMENTATION. METHOD constructor. super->constructor( im_eno = im_eno im_ename = im_ename im_dno = im_dno ). hwage = im_hwage. ENDMETHOD. "constructor METHOD show_hwage. WRITE: 'HWage:', hwage. ENDMETHOD. "show_hwage ENDCLASS. "lcl_hwage_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp, he1 TYPE REF TO lcl_hwage_emp. START-OF-SELECTION. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10. e1->show_details( ). SKIP 2. CREATE OBJECT he1 EXPORTING im_eno = 108 im_ename = 'Suresh' im_dno = 20 im_hwage = 180. *& The show_details method is not defined in the class lcl_hwage_emp, *& it has been inherited from lcl_emp. he1->show_details( ). he1->show_hwage( ).

Method Overriding: Redefining a method in the subclass, which has been inherited from its superclass, is called method overriding. While redefining an inherited method the functionality defined in the superclass

can be retained as is and some more additional functionality can be provided, or the inherited functionality can be nullified and altogether a new functionality can be provided. NOTE: 1) While overriding a method in the subclass, the signature of the method has to exactly match with the signature of the corresponding method in the superclass. 2) In the subclass method, during redefinition (overriding), if we have to access the implementation of the corresponding method in the superclass we will have to use the keyword SUPER. The keyword SUPER is a reference (pseudo reference) to the current class superclass implementation. 3) While overriding a method if the keyword SUPER is not used, it means that the methods functionality as implemented in the superclass is being nullified in the subclass and an altogether new functionality is being provided. Method Signature: The signature of a method includes the number of parameters, type of parameters (importing, exporting and/or changing; and the data types), and the sequence of parameters.
*----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i, show_details. PRIVATE SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD constructor. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. ENDMETHOD. "set_details METHOD show_details. WRITE: / 'ENo:', eno, 'EName:', ename, 'DNo:', dno. ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_hwage_emp DEFINITION INHERITING FROM lcl_emp. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_hwage TYPE i, show_details REDEFINITION. PRIVATE SECTION.

DATA: hwage TYPE i. ENDCLASS. "lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_hwage_emp IMPLEMENTATION. METHOD constructor. super->constructor( im_eno = im_eno im_ename = im_ename im_dno = im_dno ). hwage = im_hwage. ENDMETHOD. "constructor METHOD show_details. super->show_details( ). WRITE: 'HWage:', hwage. ENDMETHOD. "show_hwage ENDCLASS. "lcl_hwage_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp, he1 TYPE REF TO lcl_hwage_emp. START-OF-SELECTION. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10. e1->show_details( ). SKIP 2. CREATE OBJECT he1 EXPORTING im_eno = 108 im_ename = 'Suresh' im_dno = 20 im_hwage = 180. he1->show_details( ).

Has-A Relationship between classes: If a class X has an attribute which is a reference type of class Y, then it is said that X contains a reference to Y. The relation between Class X and Class Y is known as Has-A relation (X has-a reference to Y).
*----------------------------------------------------------------------* * CLASS lcl_address DEFINITION *----------------------------------------------------------------------* CLASS lcl_address DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING im_ad_line1 TYPE string im_city TYPE string im_state TYPE string, show_address. PRIVATE SECTION. DATA: ad_line1 TYPE string, city TYPE string, state TYPE string. ENDCLASS. "lcl_address DEFINITION *----------------------------------------------------------------------* * CLASS lcl_address IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_address IMPLEMENTATION.

METHOD constructor. ad_line1 = im_ad_line1. city = im_city. state = im_state. ENDMETHOD. "constructor METHOD show_address. WRITE: / 'Address', ad_line1, city, state. ENDMETHOD. "show_address ENDCLASS. "lcl_address IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_address type ref to lcl_address, show_details. PRIVATE SECTION. DATA: eno TYPE i, ename TYPE string, dno TYPE i, address type ref to lcl_address. ENDCLASS. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. METHOD constructor. eno = im_eno. ename = im_ename. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. dno = im_dno. ENDIF. address = im_address. ENDMETHOD. "set_details METHOD show_details. WRITE: / 'ENo:', eno, 'EName:', ename, 'DNo:', dno. address->show_address( ). ENDMETHOD. "show_details ENDCLASS. "lcl_emp IMPLEMENTATION ************************** DATA: a1 type ref to lcl_address, e1 TYPE REF TO lcl_emp. START-OF-SELECTION. create object a1 exporting im_ad_line1 = '#12/A Road No:12' im_city = 'Hyderabad' im_state = 'AP'. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 im_address = a1.

e1->show_details( ).

Functional Methods: Methods with RETURNING parameters are known as functional methods (and those without RETURNING parameters are known as non-functional methods.) A method defined with RETURNING parameters cannot have EXPORTING & CHANGING parameters. RETURNING parameters are always passed by value and not by reference. Functional methods can be called in assignment operation, and also as part of an expression (logical expressions & arithmetic expressions), which is not possible with non-functional methods. However, a functional method cannot be called as part of WRITE statement.
CLASS lcl_arithmetic DEFINITION. PUBLIC SECTION. METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i EXPORTING ex_sum TYPE i, get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. ENDCLASS. "lcl_arithmetic DEFINITION CLASS lcl_arithmetic IMPLEMENTATION. METHOD get_sum. ex_sum = im_p1 + im_p2. ENDMETHOD. "get_sum METHOD get_diff. re_diff = im_p1 - im_p2. ENDMETHOD. "get_diff ENDCLASS. "lcl_arithmetic IMPLEMENTATION *********************************** DATA: arith TYPE REF TO lcl_arithmetic, sum TYPE i, diff TYPE i. PARAMETERS: p1 TYPE i, p2 TYPE i. START-OF-SELECTION. CREATE OBJECT arith. *& Calling a non-functional method arith->get_sum( EXPORTING im_p1 = p1 im_p2 = p2 IMPORTING ex_sum = sum ). *& Calling a functional method diff = arith->get_diff( im_p1 = p1 im_p2 = p2 ). WRITE: / 'Sum:', sum, / 'Difference:', diff.

NOTE: There are other valid syntaxes to call methods.


*& Calling a non-functional method CALL METHOD arith->get_sum EXPORTING im_p1 = p1 im_p2 = p2 IMPORTING ex_sum = sum. *& Calling a functional method CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. *& Calling a functional method arith->get_diff( EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff ).

CLASS-DATA & CLASS-METHODS Attributes and methods declared CLASS-DATA and CLASS-METHODS, respectively, are known as class components or static components, while those declared DATA and METHODS are known as instance components of non-static components. Notes about class & instance components: 1. A class member can be accessed in two ways: Through the object reference (only when the reference variable points to an object of the class). ref_variable->static-member Through the class name (can be accessed before and also after an object of the class has been created). class_name=>static_member

2. The implementation of a class is loaded only once in the memory, which is referred to as ClassMemory; while for each object of a given class that has been created with CREATE OBJECT command, memory is allocated separately; which is referred to as Instance-Memory. 3. Each class attribute occupies space in the class memory and hence there can be only one copy of each class attribute; and the same copy is shared between the class implementation and all the objects of that class. 4. The class implementation is loaded into the memory only once and that too when the class is referred to for the first time. For this reason the class components can be accessed even before an object of that class is created, and this can be done by accessing the class components through the class name. class_name=>static_member 5. Class methods can access other class components of the class but cannot access the instance components. Which means a method defined as CLASS-METHOD can access only the attributes that have been defined as CLASS-DATA and the methods that have defined as CLASSMETHODS, but not those defined as DATA and METHODS. On the other hand, instance methods can access both class components as well as instance components of the class.

CLASS lcl_student DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING imc_id TYPE i imc_name TYPE string, show_details. CLASS-METHODS: get_count RETURNING value(cnt) TYPE i. PRIVATE SECTION. DATA: id TYPE i, name TYPE string. CLASS-DATA: count TYPE i. "Count of student objects created ENDCLASS. "lcl_student DEFINITION CLASS lcl_student IMPLEMENTATION. METHOD constructor. ADD 1 TO count. id = imc_id. name = imc_name. ENDMETHOD. "constructor METHOD show_details. WRITE: /3 'ID:', id, 'Name:', name. ENDMETHOD. METHOD get_count. cnt = count. ENDMETHOD. ENDCLASS.

"show_details

"get_count "lcl_student IMPLEMENTATION

************************************************ START-OF-SELECTION. DATA: stu_count TYPE i. stu_count = lcl_student=>get_count( ). WRITE: /3 'Count before creating any objects:', stu_count. DATA: s1 type ref to lcl_student, s2 type ref to lcl_student. CREATE OBJECT: s1 exporting imc_id = 101 imc_name = 'Vijay Anand'. stu_count = lcl_student=>get_count( ). WRITE: /3 'Count after creating first object:', stu_count. CREATE OBJECT: s2 exporting imc_id = 102 imc_name = 'Swaroop Kumar'. stu_count = lcl_student=>get_count( ). WRITE: /3 'Count after creating second object:', stu_count. skip 2. stu_count WRITE: /3 stu_count WRITE: /3 stu_count WRITE: /3 = s1->get_count( ). 'Count value thru s1->get_count( ):', stu_count. = s2->get_count( ). 'Count value thru s2->get_count( ):', stu_count. = lcl_student=>get_count( ). 'Count value thru lcl_student=>get_count( ):', stu_count.

Friend Classes: If a class X defines another class Y as its friend, then the class Y will have access to the private members of class X; irrespective of whether there is any inheritance relationship between the two. The friendship is unidirectional, i.e. in the above example only class Y will have access to private members of class X, but

class X will not have access to the private members of class Y. A given class can have multiple other classes defined as its friends.
CLASS lcl_b DEFINITION DEFERRED. CLASS lcl_a DEFINITION FRIENDS lcl_b. PRIVATE SECTION. METHODS: pvt_method. ENDCLASS. CLASS lcl_a IMPLEMENTATION. METHOD pvt_method. WRITE: /3 'From PRIVATE method of class lcl_a'. ENDMETHOD. "pvt_method ENDCLASS. CLASS lcl_b DEFINITION. PUBLIC SECTION. METHODS: meth_b. ENDCLASS. CLASS lcl_b IMPLEMENTATION. METHOD meth_b. WRITE: /3 'From public method of class lcl_b.'. DATA: a TYPE REF TO lcl_a. CREATE OBJECT a. *& The current code in the class lcl_b can invoke private methods of *& the class lcl_a, as the class lcl_a has defined the current class *& lcl_b as its friend a->pvt_method( ). ENDMETHOD. ENDCLASS. ******************************************** START-OF-SELECTION. DATA b TYPE REF TO lcl_b. CREATE OBJECT b. b->meth_b( ).

Interfaces: An interface is like a class definition but without any implementation of its own. All the components of an interface are public by default. Public is the only visibility scope that is applicable to interface components; for this reason visibility specification is not allowed in an interface definition. The implementation can be provided in the classes that inherit from the interface. Classes that inherit from an interface and provide the implementation are known as the implementation classes of the interface. A class can inherit from multiple interfaces. We can declare reference variables of an interface, but we cannot create an object of an interface. The reference variable of an interface can hold the reference of the object of any of the classes that inherit from the interface and provide implementation to the interface methods.
*----------------------------------------------------------------------* * INTERFACE lif_add *----------------------------------------------------------------------* INTERFACE lif_add. METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_sum) TYPE i. ENDINTERFACE. "lif_add

*----------------------------------------------------------------------* * CLASS lcl_arithmetic DEFINITION *----------------------------------------------------------------------* CLASS lcl_arithmetic DEFINITION. PUBLIC SECTION. METHODS: get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. INTERFACES: lif_add. ENDCLASS. "lcl_arithmetic DEFINITION *----------------------------------------------------------------------* * CLASS lcl_arithmetic IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_arithmetic IMPLEMENTATION. METHOD get_diff. re_diff = im_p1 - im_p2. ENDMETHOD. "get_diff METHOD lif_add~get_sum. re_sum = im_p1 + im_p2. ENDMETHOD. "lif_add~get_sum ENDCLASS. "lcl_arithmetic IMPLEMENTATION **************************************************** DATA: arith TYPE REF TO lcl_arithmetic, if_add TYPE REF TO lif_add, sum TYPE i, diff TYPE i. PARAMETERS: p1 TYPE i, p2 TYPE i. START-OF-SELECTION. CREATE OBJECT arith. CALL METHOD arith->lif_add~get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_sum = sum. CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. WRITE: / 'Sum:', sum, / 'Difference:', diff. if_add = arith. CALL METHOD if_add->get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING

re_sum = sum. * CALL METHOD if_add->get_diff * EXPORTING * im_p1 = p1 * im_p2 = p2 * RECEIVING * re_diff = diff.

Multiple interface inheritane & Aliasing: INTERFACE lif_add. METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_sum) TYPE i. ENDINTERFACE. "lif_add INTERFACE lif_div. METHODS: get_div IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_div) TYPE i. ENDINTERFACE. "lif_div

CLASS lcl_arithmetic DEFINITION. PUBLIC SECTION. METHODS: get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. INTERFACES: lif_add, lif_div. ALIASES: get_sum FOR lif_add~get_sum, get_div FOR lif_div~get_div. ENDCLASS. "lcl_arithmetic DEFINITION CLASS lcl_arithmetic IMPLEMENTATION. METHOD get_diff. re_diff = im_p1 - im_p2. ENDMETHOD. "get_diff METHOD lif_add~get_sum. re_sum = im_p1 + im_p2. ENDMETHOD. "lif_add~get_sum METHOD lif_div~get_div. IF im_p2 <> 0. re_div = im_p1 / im_p2. ENDIF. ENDMETHOD. "lif_div~get_div ENDCLASS. "lcl_arithmetic IMPLEMENTATION **************************************************** DATA: arith TYPE REF TO lcl_arithmetic, if_add TYPE REF TO lif_add, sum TYPE i, diff TYPE i, div type i.

PARAMETERS: p1 TYPE i, p2 TYPE i. START-OF-SELECTION. CREATE OBJECT arith. CALL METHOD arith->get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_sum = sum. CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. CALL METHOD arith->get_div EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_div = div. WRITE: / 'Sum:', sum, / 'Difference:', diff, / 'Division:', div.

"(OR) arith->lif_add~get_sum

"(OR) arith->lif_add~get_div

Events:

An event is a mechanism set within a class which can help a class to trigger methods of another class. Steps to code an application using events: 1) Declare an event in a class. 2) Define and implement a method that raises (triggers) the event. The method that raises the event should be in the same class in which the event has been declared (or in any of its subclasses where the event has been inherited.) 3) Define another class with a method that is associated with the event of the former class. (Whenever the method in the former class raises the event, the method of current class will react to it; in other words, the method handles the event. For this reason the method that handles the event is called event-handler-method, and the class containing this method as handler-class.) 4) Create objects of the class that raises the event and the handler class. 5) Register the event-handler-method with the object of the class that raises the event. 6) Call the method that raises the event.

Syntax to declare an event: EVENTS <event_name>. (Events can be declared in any of the 3 visibility sections of a class, or in an interface too.) Syntax to raise (trigger) an event: RAISE EVENT <event_name>. Syntax for associating a method in the handler class with an event: METHODS: <method_name> FOR EVENT <event_name> OF <*class_name>. *class_name = Name of the class that contains the event and raises the event. Syntax to register event-handler-method with the object of the class that raises the event: SET HANDLER <event-handler-method> FOR <*object_ref> *object ref = reference variable pointing to the object of the class that raises it.

Você também pode gostar