BC ABAP Programming PHẦN 10 pptx

161 289 0
BC ABAP Programming PHẦN 10 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

BC - ABAP Programming SAP AG Inheritance 1380 December 1999 Inheritance Inheritance allows you to derive a new class from an existing class. You do this using the INHERITING FROM addition in the CLASS <subclass> DEFINITION INHERITING FROM <superclass>. statement. The new class <subclass> inherits all of the components of the existing class <superclass>. The new class is called the subclass of the class from which it is derived. The original class is called the superclass of the new class. If you do not add any new declarations to the subclass, it contains the same components as the superclass. However, only the public and protected components of the superclass are visible in the subclass. Although the private components of the superclass exist in the subclass, they are not visible. You can declare private components in a subclass that have the same names as private components of the superclass. Each class works with its own private components. Methods that a subclass inherits from a superclass use the private attributes of the superclass, and not any private components of the subclass with the same names. If the superclass does not have a private visibility section, the subclass is an exact replica of the superclass. However, you can add new components to the subclass. This allows you to turn the subclass into a specialized version of the superclass. If a subclass is itself the superclass of further classes, you introduce a new level of specialization. A class can have more than one direct subclass, but it may only have one direct superclass. This is called single inheritance. When subclasses inherit from superclasses and the superclass is itself the subclass of another class, all of the classes involved form an inheritance tree, whose degree of specialization increases with each new hierarchical level you add. Conversely, the classes become more generalized until you reach the root node of the inheritance tree. The root node of all inheritance trees in ABAP Objects is the predefined empty class OBJECT. This is the most generalized class possible, since it contains neither attributes nor methods. When you define a new class, you do not have to specify it explicitly as the superclass - the relationship is always implicitly defined. Within an inheritance tree, two adjacent nodes are the direct superclass or direct subclass of one another. Other related nodes are referred to as superclasses and subclasses. The component declarations in a subclass are distributed across all levels of the inheritance tree. Redefining Methods All subclasses contain the components of all classes between themselves and the root node in an inheritance tree. The visibility of a component cannot be changed. However, you can use the REDEFINITION addition in the METHODS statement to redefine an inherited public or protected instance method in a subclass and make its function more specialized. When you redefine a method, you cannot change its interface. The method retains the same name and interface, but has a new implementation. The method declaration and implementation in the superclass is not affected when you redefine the method in a subclass. The implementation of the redefinition in the subclass obscures the original implementation in the superclass. Any reference that points to an object of the subclass uses the redefined method, even if the reference was defined with reference to the superclass. This particularly applies to the self- reference ME->. If, for example, a superclass method M1 contains a call CALL METHOD [ME- >]M2, and M2 is redefined in a subclass, calling M1 from an instance of the subclass will cause SAP AG BC - ABAP Programming Inheritance December 1999 1381 the original method M2 to be called, and calling M1 from an instance of the subclass will cause the redefined method M2 to be called. Within a redefine method, you can use the pseudoreference SUPER-> to access the obscured method. This enables you to use the existing function of the method in the superclass without having to recode it in the subclass. Abstract and Final Methods and Classes The ABSTRACT and FINAL additions to the METHODS and CLASS statements allow you to define abstract and final methods or classes. An abstract method is defined in an abstract class and cannot be implemented in that class. Instead, it is implemented in a subclass of the class. Abstract classes cannot be instantiated. A final method cannot be redefined in a subclass. Final classes cannot have subclasses. They conclude an inheritance tree. References to Subclasses and Polymorphism Reference variables defined with reference to a superclass or an interface defined with reference to it can also contain references to any of its subclasses. Since subclasses contain all of the components of all of their superclasses, and given that the interfaces of methods cannot be changed, a reference variable defined with reference to a superclass or an interface implemented by a superclass can contain references to instances of any of its subclasses. In particular, you can define the target variable with reference to the generic class OBJECT. When you create an object using the CREATE OBJECT statement and a reference variable typed with reference to a subclass, you can use the TYPE addition to create an instance of a subclass, to which the reference in the reference variable will then point. A static user can use a reference variable to address the components visible to it in the superclass to which the reference variable refers. However, it cannot address any specialization implemented in the subclass. If you use a dynamic method call, you can address all components of the class. If you redefine an instance method in one or more subclasses, you can use a single reference variable to call different implementations of the method, depending on the position in the inheritance tree at which the referenced object occurs. This concept that different classes can have the same interface and therefore be addressed using reference variables with a single type is called polymorphism. Namespace for Components Subclasses contain all of the components of all of their superclasses within the inheritance tree. Of these components, only the public and protected ones are visible. All public and protected components within an inheritance tree belong to the same namespace, and consequently must have unique names. The names of private components, on the other hand, must only be unique within their class. When you redefine methods, the new implementation of the method obscures the method of the superclass with the same name. However, the new definition replaces the previous method implementation, so the name is still unique. You can use the pseudoreference SUPER-> to access a method definition in a superclass that has been obscured by a redefinition in a subclass. BC - ABAP Programming SAP AG Inheritance 1382 December 1999 Inheritance and Static Attributes Like all components, static attributes only exist once in each inheritance tree. A subclass can access the public and protected static attributes of all of its superclasses. Conversely, a superclass shares its public and protected static attributes with all of its subclasses. In terms of inheritance, static attributes are not assigned to a single class, but to a part of the inheritance tree. You can change them from outside the class using the class component selector with any class name, or within any class in which they are shared. They are visible in all classes in the inheritance tree. When you address a static attribute that belongs to part of an inheritance tree, you always address the class in which the attribute is declared, irrespective of the class you specify in the class selector. This is particularly important when you call the static constructors of classes in inheritance. Static constructors are executed the first time you address a class. If you address a static attribute declared in a superclass using the class name of a subclass, only the static constructor of the superclass is executed. Inheritance and Constructors There are special rules governing constructors in inheritance. Instance Constructors Every class has an instance constructor called CONSTRUCTOR. This is an exception to the rule that states that component names within an inheritance tree must be unique. However, the instance constructors of the various classes in an inheritance tree are fully independent of one another. You cannot redefine the instance constructor of a superclass in a subclass, neither can you call one specifically using the statement CALL METHOD CONSTRUCTOR. Consequently, no naming conflicts can occur. The instance constructor of a class is called by the system when you instantiate the class using CREATE OBJECT. Since a subclass contains all of the visible attributes of its superclasses, which can also be set by instance constructors, the instance constructor of a subclass has to ensure that the instance constructors of all of its superclasses are also called. To do this, the instance constructor of each subclass must contain a CALL METHOD SUPER- >CONSTRUCTOR statement. The only exception to this rule are direct subclasses of the root node OBJECT. In superclasses without an explicitly-defined instance constructor, the implicit instance constructor is called. This automatically ensures that the instance constructor of the immediate superclass is called. When you call an instance constructor, you must supply values for all of its non-optional interface parameters. There are various ways of doing this: • Using CREATE OBJECT If the class that you are instantiating has an instance constructor with an interface, you must pass values to it using EXPORTING. If the class that you are instantiating has an instance constructor without an interface, you do not pass any parameters. If the class you are instantiating does not have an explicit instance constructor, you must look in the inheritance tree for the next-highest superclass with an explicit instance constructor. If this has an interface, you must supply values using EXPORTING. Otherwise, you do not have to pass any values. SAP AG BC - ABAP Programming Inheritance December 1999 1383 • Using CALL METHOD SUPER->CONSTRUCTOR If the direct superclass has an instance constructor with an interface, you must pass values to it using EXPORTING. If the direct superclass has an instance constructor without an interface, you do not pass any parameters. If the direct superclass does not have an explicit instance constructor, you must look in the inheritance tree for the next-highest superclass with an explicit instance constructor. If this has an interface, you must supply values using EXPORTING. Otherwise, you do not have to pass any values. In both CREATE OBJECT and CALL METHOD SUPER->CONSTRUCTOR, you must look at the next-available explicit instance constructor and, if it has an interface, pass values to it. The same applies to exception handling for instance constructors. When you work with inheritance, you need an precise knowledge of the entire inheritance tree. When you instantiate a class at the bottom of the inheritance tree, you may need to pass parameters to the constructor of a class that is much nearer the root node. The instance constructor of a subclass is divided into two parts by the CALL METHOD SUPER- >CONSTRUCTOR statement. In the statements before the call, the constructor behaves like a static method, that is, it cannot access the instance attributes of its class. You cannot address instance attributes until after the call. Use the statements before the call to determine the actual parameters for the interface of the instance constructor of the superclass. You can only use static attributes or local data to do this. When you instantiate a subclass, the instance constructors are called hierarchically. The first nesting level in which you can address instance attributes is the highest-level superclass. When you return to the constructors of the lower-level classes, you can also successively address their instance attributes. In a constructor method, the methods of the subclasses of the class are not visible. If an instance constructor calls an instance method of the same class using the implicit self-reference ME->, the method is called as it is implemented in the class of the instance constructor, and not in any redefined form that may occur in the subclass you want to instantiate. This is an exception to the rule that states that when you call instance methods, the system always calls the method as it is implemented in the class to whose instance the reference is pointing. Static Constructors Every class has a static constructor called CLASS_CONSTRUCTOR. As far as the namespace within an inheritance tree, the same applies to static constructors as to instance constructors. The first time you address a subclass in a program, its static constructor is executed. However, before it can be executed, the static constructors of all of its superclasses must already have been executed. A static constructor may only be called once per program. Therefore, when you first address a subclass, the system looks for the next-highest superclass whose static constructor has not yet been executed. It executes the static constructor of that class, followed by those of all classes between that class and the subclass you addressed. See also: Overview Graphics [Page 1385] BC - ABAP Programming SAP AG Inheritance 1384 December 1999 Inheritance: Introductory Example [Page 1388] SAP AG BC - ABAP Programming Inheritance: Overview Graphic December 1999 1385 Inheritance: Overview Graphic Inheritance: Overview Class c1 CLASS c1 DEFINITION INHERITING FROM ENDCLASS. CLASS c1 IMPLEMENTATION. ENDCLASS. CLASS c2 DEFINITION INHERITING FROM c1. ENDCLASS. CLASS c2 IMPLEMENTATION. ENDCLASS. Class c2 Class OBJECT Class The left-hand part of the graphic shows how you can derive a subclass c2 from a superclass c1 using the INHERTING FROM addition in the CLASS statement. The right-hand part of the graphic shows the distribution of the subclass in the inheritance tree, which stretches back to the default empty class OBJECT. A subclass contains all of the components declared above it in the inheritance tree, and can address all of them that are declared public or protected. BC - ABAP Programming SAP AG Inheritance: Overview Graphic 1386 December 1999 Single Inheritance C1 OBJECT C2 This graphic illustrates single inheritance. A class may only have one direct superclass, but it can have more than one direct subclass. The empty class OBJECT is the root node of every inheritance tree in ABAP Objects. Inheritance and Reference Variables n<class3> class1 CREF3 CREF2 CREF1 class2 class3 This graphic shows how reference variables defined with reference to a superclass can point to objects of subclasses. The object on the right is an instance of the class class3. The class reference variables CREF1, CREF2, and CREF3 are typed with reference to class1, class2, and SAP AG BC - ABAP Programming Inheritance: Overview Graphic December 1999 1387 class3. All three can point to the object. However, CREF1 can only address the public components of class1. CREF2 can address the public components of class1 and class2. CREF3 can address the public components of all of the classes. If you redefine a method of a superclass in a subclass, you can use a reference variable defined with reference to the superclass to address objects with different method implementations. When you address the superclass, the method has the original implementation, but when you address the subclass, the method has the new implementation. Using a single reference variable to call identically-named methods that behave differently is called polymorphism. BC - ABAP Programming SAP AG Inheritance: Introductory Example 1388 December 1999 Inheritance: Introductory Example The following simple example shows the principle of inheritance within ABAP Objects. It is based on the Simple Introduction to Classes [Page 1359]. A new class counter_ten inherits from the existing class counter. REPORT demo_inheritance. CLASS counter DEFINITION. PUBLIC SECTION. METHODS: set IMPORTING value(set_value) TYPE i, increment, get EXPORTING value(get_value) TYPE i. PROTECTED SECTION. DATA count TYPE i. ENDCLASS. CLASS counter IMPLEMENTATION. METHOD set. count = set_value. ENDMETHOD. METHOD increment. ADD 1 TO count. ENDMETHOD. METHOD get. get_value = count. ENDMETHOD. ENDCLASS. CLASS counter_ten DEFINITION INHERITING FROM counter. PUBLIC SECTION. METHODS increment REDEFINITION. DATA count_ten. ENDCLASS. CLASS counter_ten IMPLEMENTATION. METHOD increment. DATA modulo TYPE I. CALL METHOD super->increment. write / count. modulo = count mod 10. IF modulo = 0. count_ten = count_ten + 1. write count_ten. ENDIF. ENDMETHOD. ENDCLASS. DATA: count TYPE REF TO counter, number TYPE i VALUE 5. START-OF-SELECTION. CREATE OBJECT count TYPE counter_ten. SAP AG BC - ABAP Programming Inheritance: Introductory Example December 1999 1389 CALL METHOD count->set EXPORTING set_value = number. DO 20 TIMES. CALL METHOD count->increment. ENDDO. The class COUNTER_TEN is derived from COUNTER. It redefines the method INCREMENT. To do this, you must change the visibility of the COUNT attribute from PRIVATE to PROTECTED. The redefined method calls the obscured method of the superclass using the pseudoreference SUPER->. The redefined method is a specialization of the inherited method. The example instantiates the subclass. The reference variable pointing to it has the type of the superclass. When the INCREMENT method is called using the superclass reference, the system executes the redefined method from the subclass. [...]... externally December 1999 1413 BC - ABAP Programming SAP AG Appendix Appendix Übersicht über ABAP- Aufrufe [Page 1421] ABAP- Systemfelder [Page 1498] ABAP- Glossar [Page 1522] Programs, Screens, and Processing Blocks [Page 1415] ABAP Statement Overview [Page 1437] Statements that Introduce Programs [Page 1419] Syntax Conventions [Page 1540] 1414 December 1999 SAP AG BC - ABAP Programming Programs, Screens,... 1403 BC - ABAP Programming SAP AG Events: Introductory Example The class COUNTER implements a counter It triggers the event CRITICAL_VALUE when a threshold value is exceeded, and displays the difference HANDLER can handle the exception in COUNTER At runtime, the handler is registered for all reference variables that point to the object 1404 December 1999 SAP AG BC - ABAP Programming Events in ABAP. .. TYPE I ENDCLASS CLASS C_COUNTER2 IMPLEMENTATION METHOD I_COUNTER~SET_COUNTER COUNT = ( SET_VALUE / 10) * 10 ENDMETHOD METHOD I_COUNTER~INCREMENT_COUNTER IF COUNT GE 100 MESSAGE I042(00) COUNT = 0 ELSE ADD 10 TO COUNT ENDIF ENDMETHOD METHOD I_COUNTER~GET_COUNTER GET_VALUE = COUNT December 1999 1395 BC - ABAP Programming SAP AG Interfaces - Introductory Example ENDMETHOD ENDCLASS The interface I_COUNTER... I_VEHICLE 1 410 December 1999 SAP AG BC - ABAP Programming Class Pools Class Pools This section discusses the structure and special features of class pools Global Classes and Interfaces Classes and interfaces are both object types You can define them either globally in the R/3 Repository or locally in an ABAP program If you define classes and interfaces globally, they are stored in special ABAP programs... cannot use any of the other ABAP processing blocks (dialog modules, event blocks, subroutines, function modules) • The processing blocks of class pools are not controlled by the ABAP runtime environment No events occur, and you cannot call any dialog modules or procedures 1412 December 1999 SAP AG BC - ABAP Programming Class Pools Class pools serve exclusively for class programming You can only access... object to which points must be an object of the same class as the type of the class variable See also: Overview Graphics [Page 1394] 1392 December 1999 SAP AG BC - ABAP Programming Interfaces December 1999 1393 BC - ABAP Programming SAP AG Overview Graphics Overview Graphics Interfaces INTERFACE I1 DATA: A1 … METHODS: M1 … EVENTS: E1 … ENDINTERFACE Class C1 Public components A1, CLASS C1... Blocks This section contains a summary of possible ABAP programs, their screens, and their processing blocks Overview Screens ABAP runtime environment ABAP runtime environment Processing Processing block block Processing Processing block block Processing Processing blocks blocks ABAP program with particular type ABAP program with particular type ABAP programs consist of processing blocks, and can.. .BC - ABAP Programming SAP AG Interfaces Interfaces Einführendes Beispiel zu Interfaces [Page 1395] Classes, their instances (objects), and access to objects using reference variables form the basics of ABAP Objects These means already allow you to model typical business applications, such as customers, orders, order items, invoices, and so on, using objects, and to implement solutions using ABAP. .. C_SHIP IMPLEMENTATION METHOD CONSTRUCTOR MAX = 30 ENDMETHOD METHOD I_VEHICLE~DRIVE CHECK SHIP_SPEED < MAX SHIP_SPEED = SHIP_SPEED + 10 RAISE EVENT I_VEHICLE~SPEED_CHANGE EXPORTING NEW_SPEED = SHIP_SPEED ENDMETHOD December 1999 1407 BC - ABAP Programming SAP AG Events in ABAP Objects - Example METHOD I_VEHICLE~STOP CHECK SHIP_SPEED > 0 SHIP_SPEED = 0 RAISE EVENT I_VEHICLE~SPEED_CHANGE EXPORTING NEW_SPEED... -CLASS C_LIST IMPLEMENTATION METHOD FCODE_HANDLER CLEAR LINE CASE FCODE WHEN 'CREA_SHIP' ID = ID + 1 CREATE OBJECT REF_SHIP LINE-ID = ID LINE-FLAG = 'C' 1408 December 1999 SAP AG BC - ABAP Programming Events in ABAP Objects - Example LINE-IREF = REF_SHIP APPEND LINE TO LIST WHEN 'CREA_TRUCK' ID = ID + 1 CREATE OBJECT REF_TRUCK LINE-ID = ID LINE-FLAG = 'T' LINE-IREF = REF_TRUCK APPEND LINE TO LIST . subclass you addressed. See also: Overview Graphics [Page 1385] BC - ABAP Programming SAP AG Inheritance 1384 December 1999 Inheritance: Introductory Example [Page 1388] SAP AG BC - ABAP Programming Inheritance:. variable <cref>. See also: Overview Graphics [Page 1394] SAP AG BC - ABAP Programming Interfaces December 1999 1393 BC - ABAP Programming SAP AG Overview Graphics 1394 December 1999 Overview. call CALL METHOD [ME- >]M2, and M2 is redefined in a subclass, calling M1 from an instance of the subclass will cause SAP AG BC - ABAP Programming Inheritance December 1999 1381 the original

Ngày đăng: 09/08/2014, 14:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan