1. Trang chủ
  2. » Công Nghệ Thông Tin

BC ABAP Programming PHẦN 10 pptx

161 291 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 161
Dung lượng 7,81 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Since subclasses contain all of thecomponents of all of their superclasses, and given that the interfaces of methods cannot bechanged, a reference variable defined with reference to a su

Trang 1

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 Theoriginal 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 thesuperclass 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 arenot visible You can declare private components in a subclass that have the same names asprivate 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 thesuperclass However, you can add new components to the subclass This allows you to turn thesubclass into a specialized version of the superclass If a subclass is itself the superclass offurther 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, whosedegree of specialization increases with each new hierarchical level you add Conversely, theclasses become more generalized until you reach the root node of the inheritance tree The rootnode of all inheritance trees in ABAP Objects is the predefined empty class OBJECT This is themost generalized class possible, since it contains neither attributes nor methods When youdefine a new class, you do not have to specify it explicitly as the superclass - the relationship isalways 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 superclassesand subclasses The component declarations in a subclass are distributed across all levels ofthe 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 theREDEFINITION addition in the METHODS statement to redefine an inherited public or protectedinstance method in a subclass and make its function more specialized When you redefine amethod, you cannot change its interface The method retains the same name and interface, buthas a new implementation

The method declaration and implementation in the superclass is not affected when you redefinethe method in a subclass The implementation of the redefinition in the subclass obscures theoriginal implementation in the superclass

Any reference that points to an object of the subclass uses the redefined method, even if thereference 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

Trang 2

Abstract and Final Methods and Classes

The ABSTRACT and FINAL additions to the METHODS and CLASS statements allow you todefine 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 Theyconclude 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 thecomponents of all of their superclasses, and given that the interfaces of methods cannot bechanged, 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, youcan define the target variable with reference to the generic class OBJECT

When you create an object using the CREATE OBJECT statement and a reference variabletyped with reference to a subclass, you can use the TYPE addition to create an instance of asubclass, 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 specializationimplemented in the subclass If you use a dynamic method call, you can address all components

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 protectedcomponents within an inheritance tree belong to the same namespace, and consequently musthave unique names The names of private components, on the other hand, must only be uniquewithin their class

When you redefine methods, the new implementation of the method obscures the method of thesuperclass with the same name However, the new definition replaces the previous methodimplementation, so the name is still unique You can use the pseudoreference SUPER-> toaccess a method definition in a superclass that has been obscured by a redefinition in a

subclass

Trang 3

Inheritance and Static Attributes

Like all components, static attributes only exist once in each inheritance tree A subclass canaccess 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 ofinheritance, static attributes are not assigned to a single class, but to a part of the inheritancetree You can change them from outside the class using the class component selector with anyclass name, or within any class in which they are shared They are visible in all classes in theinheritance 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 theclass selector This is particularly important when you call the static constructors of classes ininheritance Static constructors are executed the first time you address a class If you address astatic attribute declared in a superclass using the class name of a subclass, only the staticconstructor 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 rulethat states that component names within an inheritance tree must be unique However, theinstance constructors of the various classes in an inheritance tree are fully independent of oneanother You cannot redefine the instance constructor of a superclass in a subclass, neither canyou 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 usingCREATE 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 toensure that the instance constructors of all of its superclasses are also called To do this, theinstance constructor of each subclass must contain a CALL METHOD SUPER-

>CONSTRUCTOR statement The only exception to this rule are direct subclasses of the rootnode OBJECT

In superclasses without an explicitly-defined instance constructor, the implicit instance

constructor is called This automatically ensures that the instance constructor of the immediatesuperclass is called

When you call an instance constructor, you must supply values for all of its non-optional interfaceparameters There are various ways of doing this:

• Using CREATE OBJECT

If the class that you are instantiating has an instance constructor with an interface, youmust 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 mustlook in the inheritance tree for the next-highest superclass with an explicit instanceconstructor If this has an interface, you must supply values using EXPORTING

Otherwise, you do not have to pass any values

Trang 4

• Using CALL METHOD SUPER->CONSTRUCTOR

If the direct superclass has an instance constructor with an interface, you must passvalues to it using EXPORTING

If the direct superclass has an instance constructor without an interface, you do not passany parameters

If the direct superclass does not have an explicit instance constructor, you must look inthe 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 nothave to pass any values

In both CREATE OBJECT and CALL METHOD SUPER->CONSTRUCTOR, you must look at thenext-available explicit instance constructor and, if it has an interface, pass values to it The sameapplies to exception handling for instance constructors When you work with inheritance, youneed an precise knowledge of the entire inheritance tree When you instantiate a class at thebottom of the inheritance tree, you may need to pass parameters to the constructor of a classthat 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 astatic method, that is, it cannot access the instance attributes of its class You cannot addressinstance attributes until after the call Use the statements before the call to determine the actualparameters for the interface of the instance constructor of the superclass You can only usestatic attributes or local data to do this

When you instantiate a subclass, the instance constructors are called hierarchically The firstnesting level in which you can address instance attributes is the highest-level superclass Whenyou return to the constructors of the lower-level classes, you can also successively address theirinstance 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-referenceME->, 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 namespacewithin 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 havebeen executed A static constructor may only be called once per program Therefore, when youfirst 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]

Trang 5

Inheritance: Introductory Example [Page 1388]

Trang 6

Inheritance: Overview Graphic

Inheritance: Overview Graphic

Trang 7

Inheritance: Overview Graphic

inheritance tree in ABAP Objects

Inheritance and Reference Variables

n<class3>

class1

CREF3CREF2

Trang 8

Inheritance: Overview Graphic

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 CREF3can 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 definedwith reference to the superclass to address objects with different method implementations Whenyou address the superclass, the method has the original implementation, but when you addressthe subclass, the method has the new implementation Using a single reference variable to callidentically-named methods that behave differently is called polymorphism

Trang 9

Inheritance: Introductory Example

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 theexisting class counter

DATA modulo TYPE I.

CALL METHOD super->increment write / count.

modulo = count mod 10.

DATA: count TYPE REF TO counter,

number TYPE i VALUE 5.

START-OF-SELECTION.

CREATE OBJECT count TYPE counter_ten

Trang 10

Inheritance: Introductory Example CALL METHOD count->set EXPORTING set_value = number.

specialization of the inherited method

The example instantiates the subclass The reference variable pointing to it has thetype of the superclass When the INCREMENT method is called using the

superclass reference, the system executes the redefined method from the subclass

Trang 11

Interfaces

Einführendes Beispiel zu Interfaces [Page 1395]

Classes, their instances (objects), and access to objects using reference variables form thebasics 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 implementsolutions using ABAP Objects

However, it is often necessary for similar classes to provide similar functions that are codeddifferently in each class but which should provide a uniform point of contact for the user Forexample, you might have two similar classes, savings account and check account, both of whichhave a method for calculating end of year charges The interfaces and names of the methods arethe same, but the actual implementation is different The user of the classes and their instancesmust also be able to run the end of year method for all accounts, without having to worry aboutthe actual type of each individual account

ABAP Objects makes this possible by using interfaces Interfaces are independent structures thatyou can implement in a class to extend the scope of that class The class-specific scope of aclass is defined by its components and visibility sections For example, the public components of

a class define its public scope, since all of its attributes and method parameters can be

addressed by all users The protected components of a class define its scope with regard to itssubclasses (However, inheritance is not supported in Release 4.5B)

Interfaces extend the scope of a class by adding their own components to its public section Thisallows users to address different classes via a universal point of contact Interfaces, along withinheritance, provide one of the pillars of polymorphism, since they allow a single method within

an interface to behave differently in different classes

Defining Interfaces

Like classes, you can define interfaces either globally in the R/3 Repository or locally in an ABAPprogram For information about how to define local interfaces, refer to the Class Builder [Extern]section of the ABAP Workbench Tools documentation The definition of a local interface <intf> isenclosed in the statements:

Implementing Interfaces

Unlike classes, interfaces do not have instances Instead, interfaces are implemented by classes

To implement an interface in a class, use the statement

INTERFACES <intf>

Trang 12

The class must implement the methods of all interfaces implemented in it The implementationpart of the class must contain a method implementation for each interface method <imeth>:METHOD <intf~imeth>.

Interfaces allow you to use different classes in a uniform way using interface references

(polymorphism) For example, interfaces that are implemented in different classes extend thepublic scope of each class by the same set of components If a class does not have any class-specific public components, the interfaces define the entire public face of the class

Interface References

Reference variables allow you to access objects (refer to Working with Objects [Page 1360]).Instead of creating reference variables with reference to a class, you can also define them withreference to an interface This kind of reference variable can contain references to objects ofclasses that implement the corresponding interface

To define an interface reference, use the addition TYPE REF TO <intf> in the TYPES or DATAstatement <intf> must be an interface that has been declared to the program before the actualreference declaration occurs A reference variable with the type interface reference is called ainterface reference variable, or interface reference for short

An interface reference <iref> allows a user to use the form <iref>-><icomp> to address all visibleinterface components <icomp> of the object to which the object reference is pointing It allowsthe user to access all of the components of the object that were added to its definition by theimplementation of the interface

Addressing Objects Using Interface References

To create an object of the class <class>, you must first have declared a reference variable <cref>with reference to the class If the class <class> implements an interface <intf>, you can use thefollowing assignment between the class reference variable <cref> and an interface reference

<iref> to make the interface reference in <iref> point to the same object as the class reference in

<cref>:

<iref> = <cref>

If the interface <intf> contains an instance attribute <attr> and an instance method <meth>, youcan address the interface components as follows:

Using the class reference variable <cref>:

• To access an attribute <attr>: <cref>-><intf~attr>

• To call a method <meth>:

Trang 13

Using the interface reference variable <iref>:

• To access an attribute <attr>: <iref>-><attr>

• To call a method <meth>: CALL METHOD <iref>-><meth>

As far as the static components of interfaces are concerned, you can only use the interface name

to access constants:

Addressing a constant <const>: <intf>=><const>

For all other static components of an interface, you can only use object references or the class

<class> that implements the interface:

Addressing a static attribute <attr>: <class>=><intf~attr>

Calling a static method <meth>:CALL METHOD <class>=><intf~meth>

Assignment Using Interface References - Casting

Like class references, you can assign interface references to different reference variables Youcan also make assignments between class reference variables and interface reference variables.When you use the MOVE statement or the assignment operator (=) to assign reference variables,the system must be able to recognize in the syntax check whether an assignment is possible.Suppose we have a class reference <cref> and interface references <iref>, <iref1>, and <iref2>.The following assignments with interface references can be checked statically:

The class of <cref> must be the predefined empty class OBJECT

In all other cases, you would have to work with the statement MOVE ? TO or the casting operator (?=) The casting operator replaces the assignment operator (=) In the MOVE ? TO

statement, or when you use the casting operator, there is no static type check Instead, thesystem checks at runtime whether the object reference in the source variable points to an object

to which the object reference in the target variable can also point If the assignment is possible,the system makes it, otherwise, the catchable runtime error MOVE_CAST_ERROR occurs.You must always use casting for assigning an interface reference to a class reference if <cref>does not refer to the predefined empty class OBJECT:

Trang 14

Interfaces

Trang 15

Methodimplementations

Subclasses of C1

Protected components …

PubliccomponentsA1,

[Extern] [Extern]

The left-hand side of the diagram shows the definition of a local interface I1 and the declarationand implementation parts of a local class C1 that implements the interface I1 in its public

section The interface method I1~M1 is implemented in the class You cannot implement

interfaces in the other visibility sections

The right-hand side illustrates the structure of the class with the components in their respectivevisibility areas, and the implementation of the methods The interface components extend thepublic scope of the class All users can access the public components specific to the class andthose of the interface

Trang 16

Interfaces - Introductory Example

Interfaces - Introductory Example

The following simple example shows how you can use an interface to implement two countersthat are different, but can be addressed in the same way See also the example in the Classessection

Trang 17

Interfaces - Introductory Example

ENDMETHOD

ENDCLASS

The interface I_COUNTER contains three methods SET_COUNTER,

INCREMENT_COUNTER, and GET_COUNTER The classes C_COUNTER1 andC_COUNTER2 implement the interface in the public section Both classes mustimplement the three interface methods in their implementation part C_COUNTER1

is a class for counters that can have any starting value and are then increased byone C_COUNTER2 is a class for counters that can only be increased in steps of 10.Both classes have an identical outward face It is fully defined by the interface in bothcases

The following sections explain how a user can use an interface reference to addressthe objects of both classes:

[Extern]

Trang 18

Triggering and Handling Events

Triggering and Handling Events

Übersichtsgrafiken zu Ereignissen [Page 1400]

Einführendes Beispiel zu Ereignissen [Page 1403]

Komplexes Beispiel zu Ereignissen [Page 1405]

In ABAP Objects, triggering and handling an event means that certain methods act as triggers

and trigger events, to which other methods - the handlers - react This means that the handler

methods are executed when the event occurs

This section contains explains how to work with events in ABAP Objects For precise details ofthe relevant ABAP statements, refer to the corresponding keyword documentation in the ABAPEditor

Triggering Events

To trigger an event, a class must

• Declare the event in its declaration part

• Trigger the event in one of its methods

Declaring Events

You declare events in the declaration part of a class or in an interface To declare instanceevents, use the following statement:

EVENTS <evt> EXPORTING VALUE(<ei>) TYPE type [OPTIONAL]

To declare static events, use the following statement:

CLASS-EVENTS <evt>

Both statements have the same syntax

When you declare an event, you can use the EXPORTING addition to specify parameters thatare passed to the event handler The parameters are always passed by value Instance eventsalways contain the implicit parameter SENDER, which has the type of a reference to the type orthe interface in which the event is declared

Triggering Events

An instance event in a class can be triggered by any method in the class Static events can betriggered by any static method To trigger an event in a method, use the following statement:RAISE EVENT <evt> EXPORTING <ei> = <fi>

For each formal parameter <ei> that is not defined as optional, you must pass a correspondingactual parameter <fi> in the EXPORTING addition The self-reference ME is automaticallypassed to the implicit parameter SENDER

Handling Events

Events are handled using special methods To handle an event, a method must

• be defined as an event handler method for that event

Trang 19

Triggering and Handling Events

Declaring Event Handler Methods

Any class can contain event handler methods for events from other classes You can, of course,also define event handler methods in the same class as the event itself To declare an eventhandler method, use the following statement:

METHODS <meth> FOR EVENT <evt> OF <cif> IMPORTING <ei>

for an instance method For a static method, use CLASS-METHODS instead of METHODS

<evt> is an event declared in the class or interface <cif>

The interface of an event handler method may only contain formal parameters defined in thedeclaration of the event <evt> The attributes of the parameter are also adopted by the event.The event handler method does not have to use all of the parameters passed in the RAISEEVENT statement If you want the implicit parameter SENDER to be used as well, you must list

it in the interface This parameter allows an instance event handler to access the trigger, forexample, to allow it to return results

If you declare an event handler method in a class, it means that the instances of the class or theclass itself are, in principle, able to handle an event <evt> triggered in a method

Registering Event Handler Methods

To allow an event handler method to react to an event, you must determine at runtime the trigger

to which it is to react You can do this with the following statement:

SET HANDLER <hi> [FOR]

It links a list of handler methods with corresponding trigger methods There are four differenttypes of event

It can be

• An instance event declared in a class

• An instance event declared in an interface

• A static event declared in a class

• A static event declared in an interface

The syntax and effect of the SET HANDLER depends on which of the four cases listed aboveapplies

For an instance event, you must use the FOR addition to specify the instance for which you want

to register the handler You can either specify a single instance as the trigger, using a referencevariable <ref>:

SET HANDLER <hi> FOR <ref>

or you can register the handler for all instances that can trigger the event:

SET HANDLER <hi> FOR ALL INSTANCES

The registration then applies even to triggering instances that have not yet been created whenyou register the handler

You cannot use the FOR addition for static events:

SET HANDLER <hi>

Trang 20

Triggering and Handling Events

The registration applies automatically to the whole class, or to all of the classes that implementthe interface containing the static event In the case of interfaces, the registration also applies toclasses that are not loaded until after the handler has been registered

Timing of Event Handling

After the RAISE EVENT statement, all registered handler methods are executed before the nextstatement is processed (synchronous event handling) If a handler method itself triggers events,its handler methods are executed before the original handler method continues To avoid thepossibility of endless recursion, events may currently only be nested 64 deep

Handler methods are executed in the order in which they were registered Since event handlersare registered dynamically, you should not assume that they will be processed in a particularorder Instead, you should program as though all event handlers will be executed simultaneously

Trang 21

Event trigger Event handler

The class C1 contains an event E1, which is triggered by the method M1 Class C2 contains amethod M2, which can handle event E1 of class C1

The following diagram illustrates handler registration:

Trang 22

DATA: R1 TYPE REF TO C1,

H1 TYPE REF TO C2, H2 TYPE REF TO C2.

CREATE OBJECT: R1,

H1, H2.

The program creates an instance of the class C1 and two instances of the class C2 The values

of the reference variables R1, H1, and H2 point to these instances

The SET HANDLER statement creates a handler table, invisible to the user, for each event forwhich a handler method has been registered

The handler table contains the names of the handler methods and references to the registered

instances The entries in the table are administered dynamically by the SET HANDLER

statement A reference to an instance in a handler table is like a reference in a reference

Trang 24

Events: Introductory Example

Events: Introductory Example

The following simple example shows the principle of events within ABAP Objects It is based onthe Simple Introduction to Classes [Page 1359] An event critical_value is declared and triggered

DATA: count TYPE i,

threshold TYPE i VALUE 10.

diff = count - threshold.

RAISE EVENT critical_value EXPORTING excess = diff.

DATA: r1 TYPE REF TO counter,

h1 TYPE REF TO handler.

Trang 25

Events: Introductory Example

The class COUNTER implements a counter It triggers the event CRITICAL_VALUEwhen a threshold value is exceeded, and displays the difference HANDLER canhandle the exception in COUNTER At runtime, the handler is registered for allreference variables that point to the object

Trang 26

Events in ABAP Objects - Example

Events in ABAP Objects - Example

The following example shows how to declare, call, and handle events in ABAP Objects

Overview

This object works with the interactive list displayed below Each user interaction triggers an event

in ABAP Objects The list and its data is created in the class C_LIST There is a class STATUSfor processing user actions It triggers an event BUTTON_CLICKED in the AT USER-COMMANDevent The event is handled in the class C_LIST It contains an object of the class C_SHIP orC_TRUCK for each line of the list Both of these classes implement the interface I_VEHICLE.Whenever the speed of one of these objects changes, the event SPEED_CHANGE is triggered.The class C_LIST reacts to this and updates the list

Trang 27

Events in ABAP Objects - Example

This example is implemented using local interfaces and classes Below are the declarations of

the interfaces and classes:

*****************************************************************

* Interface and Class declarations

*****************************************************************

INTERFACE I_VEHICLE.

DATA MAX_SPEED TYPE I.

EVENTS SPEED_CHANGE EXPORTING VALUE(NEW_SPEED) TYPE I.

ALIASES MAX FOR I_VEHICLE~MAX_SPEED.

DATA SHIP_SPEED TYPE I.

ALIASES MAX FOR I_VEHICLE~MAX_SPEED.

DATA TRUCK_SPEED TYPE I.

Trang 28

* -Events in ABAP Objects - Example CLASS C_LIST DEFINITION.

PUBLIC SECTION.

METHODS: FCODE_HANDLER FOR EVENT BUTTON_CLICKED OF STATUS

IMPORTING FCODE, LIST_CHANGE FOR EVENT SPEED_CHANGE OF I_VEHICLE

IMPORTING NEW_SPEED,

LIST_OUTPUT.

PRIVATE SECTION.

DATA: ID TYPE I,

REF_SHIP TYPE REF TO C_SHIP,

REF_TRUCK TYPE REF TO C_TRUCK,

The following events are declared in the example:

• The instance event SPEED_CHANGE in the interface I_VEHICLE

• The static event BUTTON_CLICKED in the class STATUS

The class C_LIST contains event handler methods for both events

Note that the class STATUS does not have any attributes, and therefore only works with staticmethods and events

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = SHIP_SPEED.

ENDMETHOD.

Trang 29

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 = SHIP_SPEED.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = TRUCK_SPEED.

ENDMETHOD.

METHOD I_VEHICLE~STOP.

CHECK TRUCK_SPEED > 0.

TRUCK_SPEED = 0.

RAISE EVENT I_VEHICLE~SPEED_CHANGE

EXPORTING NEW_SPEED = TRUCK_SPEED.

SET PF-STATUS 'VEHICLE'.

WRITE 'Click a button!'.

Trang 30

Events in ABAP Objects - Example LINE-IREF = REF_SHIP.

APPEND LINE TO LIST.

READ TABLE LIST INDEX SY-LILLI INTO LINE.

CALL METHOD LINE-IREF->DRIVE.

WHEN 'STOP'.

LOOP AT LIST INTO LINE.

CALL METHOD LINE-IREF->STOP.

SET TITLEBAR 'TIT'.

LOOP AT LIST INTO LINE.

The static method USER_ACTION of the class STATUS triggers the static event

BUTTON_CLICKED The instance methods I_VEHICLE~DRIVE and I_VEHICLE~STOP triggerthe instance event I_VEHICLE~SPEED_CHANGE in the classes C_SHIP and C_TRUCK

Using the Classes in a Program

The following program uses the above classes:

REPORT OO_EVENTS_DEMO NO STANDARD PAGE HEADING.

Trang 31

Events in ABAP Objects - Example

CREATE OBJECT LIST.

SET HANDLER: LIST->FCODE_HANDLER,

LIST->LIST_CHANGE FOR ALL INSTANCES.

Trang 32

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/3Repository or locally in an ABAP program If you define classes and interfaces globally, they arestored in special ABAP programs called class pools (type K) or interface pools (type J), whichserve as containers for the respective object types Each class or interface pool contains thedefinition of a single class or interface The programs are automatically generated by the ClassBuilder when you create a class or interface

A class pool is comparable to a module pool or function group It contains both declarative andexecutable ABAP statements, but cannot be started on its own Instead, the system can onlyexecute the statements in the class pool on request, that is, when the CREATE OBJECT

statement occurs to create instances of the class

Interface pools do not contain any executable statements Instead, they are used as containersfor interface definitions When you implement an interface in a class, the interface definition isimplicitly included in the class definition

Structure of a Class Pool

Class pools are structured as follows:

Trang 33

Declaration part of class

CLASS … DEFINITION PUBLIC.

Class pools contain a definition part for type declarations, and the declaration and

implementation parts of the class

Differences From Other ABAP Programs

Class pools are different from other ABAP programs for the following reasons:

• ABAP programs such as executable programs, module pools, or function modules,usually have a declaration part in which the global data for the program is defined Thisdata is visible in all of the processing blocks in the program Class pools, on the otherhand, have a definition part, in which you can define data and object types, but no data

objects or field symbols The types that you define in a class pool are only visible in the

implementation part of the global class

• The only processing blocks that you can use are the declaration part and implementationpart of the global class The implementation part may only implement the methodsdeclared in the global class You 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

Trang 34

Local Classes in Class Pools

The classes and interfaces that you define in the definition part of a class pool are not visibleexternally Within the class pool, they have a similar function to local classes and interfaces inother ABAP programs Local classes can only be instantiated in the methods of the global class.Since subroutines are not allowed in class pools, local classes are the only possible

modularization unit in global classes Local classes have roughly the same function for globalclasses as subroutines in function groups, but with the significant exception that they are notvisible externally

Trang 35

Programs, Screens, and Processing Blocks [Page 1415]

ABAP Statement Overview [Page 1437]

Statements that Introduce Programs [Page 1419]

Syntax Conventions [Page 1540]

Trang 36

Programs, Screens, and Processing Blocks

Programs, Screens, and Processing Blocks

This section contains a summary of possible ABAP programs, their screens, and their processingblocks

Overview

ABAP program with particular type

ABAP runtime environment

Processingblock

Processingblock Processingblock

Processingblock Processingblocks

Processingblocks

• Function Group

Type F; introduced with the FUNCTION-POOL statement; non-executable; container forfunction modules; can have its own screens

Trang 37

Programs, Screens, and Processing Blocks

Type S; introduced with the PROGRAM statement; non-executable; container for

subroutines; cannot have its own screens

programs in which the corresponding include statement occurs

• Selection Screen

Defined within an ABAP program; called by the runtime environment or using the CALLSELECTION-SCREEN statement; processed in event blocks of the corresponding ABAPprogram

• Lists

Defined within an ABAP program; called by the runtime environment; processed in eventblocks of the corresponding ABAP program

Class definitions (type K programs) do not yet support screens Subroutine pools (type S

programs) cannot contain their own screens

Processing Blocks

All ABAP programs are made up of processing blocks You cannot nest processing blocks.When a program is executed, its processing blocks are called All of the statements in an ABAPprogram, apart from its global data declarations, belong to a processing block

ABAP contains the following processing blocks:

Trang 38

Programs, Screens, and Processing Blocks

Dialog Module

Defined between the MODULE ENDMODULE statements in type 1, M, and F programs; has nolocal data area and no parameter interface; called using the MODULE statement in screen flowlogic; processes screens

Event Block

Defined by one of the event key words; no local data area and no parameter interface; reacts toevents in the ABAP runtime environment (Exceptions: AT SELECTION-SCREEN and GET areimplemented internally using subroutines, and have a local data area)

AT SELECTION-SCREEN ON VALUE REQUEST

AT SELECTION-SCREEN ON HELP REQUEST

AT SELECTION-SCREEN ON <f>

AT SELECTION-SCREEN ON BLOCK

AT SELECTION-SCREEN ON RADIOBUTTON GROUP

AT SELECTION SCREEN

AT SELECTION SCREEN ON END OF <f>

Called by the ABAP runtime environment following a user action on a selection screen in

a type 1, M, or F program; process selection screens

Trang 39

Programs, Screens, and Processing Blocks

Defined by FUNCTION ENDFUNCTION in type F programs; called using CALL

FUNCTION from any ABAP program

Trang 40

Introductory Statements for Programs

Introductory Statements for Programs

Each ABAP program type has a statement that introduces programs of that type:

Program type Introductory statement

The following sections describe the function of introductory statements:

REPORT and PROGRAM

The REPORT and PROGRAM statements currently have the same function They allow you tospecify the message class of the program and the formatting options for its default list Whether

a program is executable or can only be started using a transaction code depends exclusively onthe program type and not on the statement that introduces it However, executable programsshould always begin with a REPORT statement, and module pools always with a PROGRAMstatement Subroutine pools (type S programs) should also always begin with a PROGRAMstatement

FUNCTION-POOL

The introductory statement FUNCTION-POOL declares a program in which you can definefunction modules At runtime, function pool programs are loaded in to a new program group[Page 498] with their own user dialogs and their own shared data areas in the internal session ofthe calling program For this reason, function groups (type F programs) must always begin with aFUNCTION-POOL statement This is usually generated by the Function Builder Type 1, M, or Sprograms should not begin with a FUNCTION-POOL statement, since they would then not sharecommon data areas with the caller However, in exceptional cases, you can introduce a type 1 ortype M program with FUNCTION-POOL to ensure that externally-called subroutines can processtheir own screens As in the REPORT and PROGRAM statements, you can specify the messageclass and standard list formatting options of the program in the FUNCTION-POOL statement

CLASS-POOL

The introductory statement CLASS-POOL can only be used for class or interface definitions (type

K or J programs) A program introduced with the CLASS-POOL statement can only containglobal type definitions and definitions of classes and interfaces The CLASS-POOL statement is

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

TỪ KHÓA LIÊN QUAN