Documente Academic
Documente Profesional
Documente Cultură
ABAP Programming
Level 1
Level 1
ABAP 010
3 Days
Basics of
SAP Web AS
ABAP Workbench
basics and
concepts
Level 3
ABAP030
2 Days
ABAP Dictionary
ABAP 100
3 Days
ABAP Objects
Object Oriented
Programming
with SAP NetWeaver
ABAP110
2 Days
Development of
user dialogs
ABAP111 1 Days
Table Controls &
ALV Grids
Agenda
ABAP Objects
Advanced Business Application Programming
for
Object-Oriented Programming
????
Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects
1950
Assembler
1952
1954
Fortran
1956
Cobol
1958
LISP
Algol 60
1960
APL
Basic
1962
PL1
1964
1966 Prolog
Simula 67 Algol 68
Logo
1968
BCPL
1970
Smalltalk
Pascal
1972
1974
C
1976
APL2
1978 FP1
Modula-2
1980
Smalltalk-80
Ada
1982
1984
ABAP
Object Pascal
1986 Miranda
C++
1988
Modula-3
Eiffel
Oberon
1990
1992
Eiffel-3
Oberon-2
1994
Java
Ada-95
1995
1996
1997
ABAP Objects
1998
10
11
Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects
Daten
Daten
Daten
Daten
Daten
Funktion
Funktion
Funktion
12
Funktion
Funktion
Funktion
Funktion
Funktion
Data declarations
Main program
TYPES: ...
DATA: ...
...
Subroutine calls
PERFORM f1 ...
Definition of subroutines
* ----------------FORM f1 ...
...
ENDFORM.
13
Main program
Global data objects
Function groups
Global data
Function group 1
Function module 1
Function module 2
Function module ...
Global data
Function group 2
Subroutines
Interface
Interface
14
Function module 1
Function module 2
FUNCTION SET_COUNTER.
* Local interface IMPORTING VALUE(set_value)
count = set_value
ENDFUNCTION.
FUNCTION INCREMENT_COUNTER.
ADD 1 TO count.
ENDFUNCTION.
FUNCTION GET_COUNTER.
* Local interface EXPORTING VALUE(get_value)
get_value = count.
ENDFUNCTION.
15
16
1 counter
COUNTER
GET_COUNTER
17
n. Instance, Class 1
1. Instance, Class 1
SchnittDatea
stelle
Interface Data
n. Instance, Class m
1. Instance, Class m
SchnittDatea
stelle
Interface Data
Several instances
(Objects)
of the same Blueprint
(Type or better Class)
are a fundamental
property of objectoriented languages.
External Session
18
19
Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects
Real world
Encapsulation of data
and functions
Functions
Data
Data
Data
Daten
Functions
Functions
Data
Data
Kran
Functions
Daten
Functions
Functions
Functions
Functions
Functions
Functions
Functions
Functions Functions
Functions
Functions
Daten
Functions
Functions
20
Kran
21
Functions
Data
Functions
Father
Inheritance
Child 1
Child 2
Code 2
display_data
Code 1
Code 3
Code 4
22
Event controlling
Telephone
Telephone
myNumber
doCall
register
doCall
beCalled
myNumber
23
beCalled
register
Inheritance
Telephone
24
OfficePhone
CellPhone
myNumber
myContacts
mySMS
register
addContact
sendSMS
doCall
callContact
getSMS
beCalled
deleteContact
msg systems ag, 15.01.2013
beCalled
beCalled
beCalled
MP3
25
Event controlling
msg
register_me
Phone
Center
26
Methods
Data
call_it
Client
27
Methods
do_it
Data
Server
28
Compatibility:
As simple as possible
Modularization units
Internal session
Functions
Data
29
Requirement,
Idea
Test
Iteration
Analysis and
Design
30
Implementation
(ABAP Objects)
31
32
Classification of Objects
UML the Modeling Standard
Class diagrams
Sequence diagrams
lcl_vehicle
33
lcl_people
msg systems ag, 15.01.2013
Scrap-merchant
Car enthusiast
Price
price
Make
make
lcl_vehicle
- set
Configuration Model
configuration
- ...
- price
- configuration
Color
color
Weight
weight
Doors
model
- make
doors
Wheels
wheels
PS
lcl_vehicle
Scrap value
scrapvalue
- ...
- weight
- scrapvalue
HP
34
Class:
General description of
object (blueprint)
lcl_vehicle
lcl_vehicle
or
Object (Instance):
Representation of section
of the real world
Concrete instance of a
class
(5) lcl_vehicle
35
or
36
Classification of Objects
UML the Modeling Standard
Class diagrams
Sequence diagrams
is a worldwide standard
it is a language used for the specification, construction,
visualization, and documentation of models for software systems
Diagram types:
Class diagrams
Behavior diagrams, e.g. the Sequence diagram
Component diagrams
Deployment diagrams
...
37
lcl_vehicle
Class name
38
Attributes
(Properties)
make
model
.....
price
color
Methods
(Functions)
set_make()
display_attributes()
increases_speed()
Optional,
does not have
Assoziationsme
to be specified
Events +
Handlers
lcl_rental
0..*
lcl_booking
0..* 1
lcl_customer
1..*
lcl_vehicle
lcl_car
39
lcl_truck
1..*
lcl_wheel
lcl_bus
lcl_booking
A booking refers
to a single
customer
0..*
books
Common cardinalities
* or 0..*
1
1..*
0..1
40
lcl_customer
Description of the
association
any amount
exactly one
at least one
no more than one
Aggregation Symbol
Aggregation is a special
case of association,
a whole-part relationship.
1..1
lcl_vehicle
Composition is a special
case of aggregation, an
existence-dependent
a whole-part relationship.
lcl_wheel
Composition Symbol
1..1
lcl_vehicle
41
0..*
1..*
lcl_booking
lcl_car
lcl_truck
Generalization
Specialization
lcl_vehicle
lcl_vehicle
lcl_car
42
lcl_truck
Generalization
Specialization
or
Object
:Class
Create Object
Time
Process
description
(optional)
Object
:Class
1:Method(parameter)
Lifeline
of the object
Return value
Control focus
43
:lcl_driver
:lcl_car
:lcl_tank
1: get_fuel_level( )
2: get_fuel_level( )
re_level
re_level
re_level =
tank->get_fuel_level( ).
44
re_level =
fuel / fuel_max * 100.
45
Exercise
HOURS
MIN
Exercise
Analysis and Design
UML Class Diagrams
46
47
lcl_vehicle
Public
components
get_make()
Public access:
Methods
Events
set_make()
Private
components
make
model
Private Access:
Data types
Attributes
Methods:
Implementation
48
ABAP-Coding
CLASS lcl_periode DEFINITION.
ENDCLASS.
lcl_vehicle
Public
Components
Private
Components
ENDCLASS.
49
(5) lcl_car
C, N, I, P, ..., STRING
dictionary types
Public
Private
make TYPE STRING,
...
r_motor
TYPE REF TO ...
(5) lcl_rental
r_car
50
TYPE
TYPE
LIKE
TYPE
TYPE
TYPE
TYPE
<Type>,
<DDIC-Type>,
mv_var1,
<Type> VALUE <value>,
<Type> READ-ONLY,
REF TO <Class>,
REF TO <Interface>.
ENDCLASS.
51
Public attributes
generally visible
and changeable
direct access
Private attributes
only visible and
changeable within the
class
PUBLIC SECTION.
DATA: make TYPE string.
PRIVATE SECTION.
ENDCLASS.
PRIVATE SECTION.
DATA: make TYPE string.
ENDCLASS.
52
Client
(5) lcl_vehicle
Public
Private
make
r_vehicle
53
set_make()
get_make()
Instance attributes
PRIVATE SECTION.
DATA: make TYPE string,
...
54
Static attributes
Static attributes
exist only once
Static attribute
(1) lcl_vehicle
r_vehicle1
(2) lcl_vehicle
Instances /
objects
r_vehicle2
(3) lcl_vehicle
r_vehicle3
...
55
...
56
Methods can
have a signature
The method
implementation governs
the behavior of the
objects .
Client
(5) lcl_vehicle
Public
Private
make
...
r_vehicle
57
...
set_make()
init_make()
Public methods
can generally be called
Private methods
can only be called
within the class
58
Rules
Instance methods
access to static but also instance components is possible in
their implementation
can be invoked through an instance
Static method
can use only static components in their implementation
are addressed with their class name
59
60
lcl_vehicle
- make:string
- model:string
- n_o_vehicles:i
+ set_make(im_make:string)
- init_make()
+ get_count():i
Exercise
HOURS
MIN
Exercise
OOP Fundamentals
Creating a class
62
63
Rules
lcl_vehicle
-
make
model
ser_no
n_o_vehicles
+ set_make()
- init_make()
+ get_count()
64
(5) lcl_vehicle
Public
Private
set_make
get_count
make
modell
ser_no
...
r_vehicle
What do these
reference variables
point to?
r_vehicle1
r_vehicle2
START-OF_SELECTION.
...
65
?
?
msg systems ag, 15.01.2013
(3) lcl_vehicle
Public
Private
r_vehicle1
r_vehicle2
(4) lcl_vehicle
Public
66
Private
(3) lcl_vehicle
Public
Private
r_vehicle2 = r_vehicle2.
r_vehicle1
r_vehicle2
(4) lcl_vehicle
Public
67
Private
All independent references from the global memory are checked. The
references point to 'living' objects, that are marked internally.
The references from class- and instance attributes to further objects are
tracked. These objects are also marked.
Unmarked objects are removed from the main memory.
(4) lcl_object
CLEAR r_object
(2) lcl_object
(5) lcl_object
68
(2) lcl_object
(3) lcl_object
69
(5) lcl_vehicle
Public
(2) lcl_wheel
Private
(3) lcl_wheel
itab_wheels
(4) lcl_wheel
(5) lcl_wheel
70
Exercise
HOURS
MIN
Exercise
OOP Fundamentals
Object instantiation
71
72
Client
(5) lcl_vehicle
r_vehicle
r_vehicle->motor_on( )
Reference
Public
Private
motor_on()
Method
73
Method
IMPORTING
im_distance
ex_fuel
EXPORTING
Received values
Returned values
74
IMPORTING
EXPORTING
EXPORTING
CALL METHOD
EXPORTING
im_distance = 500
IMPORTING
ex_fuel = lv_fuel.
IMPORTING
Method
IMPORTING
im_distance
ex_fuel
EXPORTING
75
->
CALL METHOD <InstRef>-><InstMethod>
EXPORTING <InpParam>
= <Value>
IMPORTING <ExpParam>
= <Variable>
CHANGING
<ChgParam>
= <Variable>
RECEIVING <ReturnVal> = <Variable>
EXCEPTIONS <Exception> = <Nr>.
<InstRef>-><InstMethod>(
<Parameter> )
->
Shorter syntax:
(see online documentation)
No space before opening bracket
SPACE!
DATA: r_vehicle TYPE REF TO lcl_vehicle.
DATA: make_name TYPE string.
...
make_name = `the make of the car`.
CALL METHOD r_vehicle->set_make EXPORTING im_make = make_name.
* as of NW 6.1 keyword EXPORTING can be left out
r_vehicle->set_make
r_vehicle->set_make( make_name )
* now getting something back from method...
r_vehicle->get_make( IMPORTING ex_make = make_name ).
r_vehicle->get_make
76
Static methods:
=>
CALL METHOD <KlassenName>=><KlassenMethode>(
[Parameter] )
<KlassenName>=><KlassenMethode>(
[Parameter] )
=>
77
a = b +
Functional Method
Definition:
Precisely one RETURNING parameter
Otherwise, only IMPORTING parameters and exceptions are
possible
Call:
a) RECEIVING-Parameter, or ...
b) ... implicit calls possible in various expressions:
MOVE, CASE, LOOP
logical expressions (IF, ELSEIF, WHILE, CHECK, WAIT)
arithmetic and bit expressions (COMPUTE)
see example a = b + c
78
79
"just a demo
"just a demo
...
ENDCLASS.
...
(2) lcl_vehicle
make
...
START-OF-SELECTION.
*--------------------------------------* main program, think of a client !
CREATE OBJECT r_vehicle.
r_vehicle
make_name = r_vehicle->make
r_vehicle->make.
count
= lcl_vehicle=>n_o_vehicles
lcl_vehicle=>n_o_vehicles.
81
Exercise
HOURS
MIN
Exercise
Fundamentals of OOP
Method Calls
82
83
lcl_vehicle
- make
- model
- n_o_vehicles
+ constructor()
CREATE OBJECT
METHODS:
constructor
IMPORTING <InpParam>
EXCEPTIONS <Exception>
RAISING
<class-based exceptions>.
84
(3) lcl_vehicle
Public
Private
CLASS-METHODS: class_constructor.
class_constructor
PRIVATE SECTION.
86
Exercise
HOURS
MIN
Exercise
Fundamentals of OOP
Constructor
87
88
89
attributes
methods
events
types
constants
ALIAS names
lcl_vehicle
Public
components
Method
make
STOP
Private
components
Attribute
make
Method
implementation
lcl_vehicle
Public
components
Private
components
Attribute
mv_make
Method
implementation
me->make
me->mv_make
METHOD meth.
" inside class prefix 'me->' is not
" necessary for calling methods.
display_attributes( ).
ENDMETHOD.
ENDCLASS.
90
91
Exercise
HOURS
MIN
Exercise
Fundamentals of OOP
Calling a private method
92
5. Inheritance
93
5. Inheritance
UML Example
lcl_vehicle
-
make
model
ser_no
color
n_o_vehicles
+ constructor()
+ display_attributes()
+ get_count()
is a
relationship
lcl_car
94
lcl_truck
lcl_bus
- car_type
- max_seats
- acceleration
- max_cargo
- n_o_tanks
- n_o_wheels
- max_passengers
- lavatory
- television
+
+ get_type()
+
+ get_cargo()
+
+ get_passengers()
5. Inheritance
Multiple Inheritance?
lcl_super1
Generalization
lcl_super2
STOP
lcl_sub1
95
lcl_sub2
lcl_sub3
Specialization
no
multiple inheritance!
5. Inheritance
Relationships between Super- and Subclasses
lcl_super
new
components
96
Inherited
components
lcl_sub
5. Inheritance
Syntax
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: estimate_fuel
IMPORTING im_distance
TYPE s_distance
RETURNING value(re_fuel) TYPE ty_fuel.
PRIVATE SECTION.
DATA: make TYPE string,
...
ENDCLASS.
PRIVATE SECTION.
DATA: max_cargo TYPE ty_cargo.
...
ENDCLASS.
97
5. Inheritance
Redefining Methods
STOP
New implementation of
the inherited method
Refers to the superclass
component
5. Inheritance
Inheritance and Redefinition of the constructor
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING
im_make TYPE string
string.
ENDCLASS.
99
5. Inheritance
Rules for Calling the Constructor
DATA: r_2 TYPE REF TO lcl_2,
r_3 TYPE REF TO lcl_3.
lcl_1
a1:I
constructor( im_1:i )
lcl_2
Case 1
has constructor
Class of instance to be created has
supply its parameters.
Case 2
has no
no
Class of instance to be created has
constructor
Search in the inheritance tree for the next
highest superclass with a constructor and
supply its parameters.
100
lcl_3
a2:I
constructor( im_1:i
im_2:i )
msg systems ag, 15.01.2013
5. Inheritance
Inheritance and Visibility
Public components
101
Private components
PUBLIC SECTION.
...
direct access
Protected components
generally visible
SECTION.
PROTECTED SECTION
DATA tank TYPE REF TO lcl_tank.
PRIVATE SECTION.
DATA make TYPE string.
ENDCLASS.
lcl_vehicle
- make
- model
# tank
+ public
# protected
- private
+ constructor() static
+ display_attributes()
+ get_count()
5. Inheritance
Visibility of Protected Components
Addressable
for all clients
from the outside!
(5) lcl_bus
Public
Private
Geerbt
get_make()
set_make()
get_count()
display_attributes()
estimate_fuel()
Protected
Inherited
tank
Addressable
only inside the class
102
5. Inheritance
Rules for Redefinition of Methods
lcl_vehicle
+ estimate_fuel()
lcl_car
+ estimate_fuel()
lcl_truck
+ estimate_fuel()
lcl_bus
+ estimate_fuel()
5. Inheritance
Redefinition for Methods: Example
lcl_bus
- max_passengers
- max_cargo
+ constructor()
+ estimate_fuel()
+ constructor()
+ estimate_fuel()
METHOD estimate_fuel.
DATA: weight ...
weight = empty_load
average_weight.
+ max_passengers * average_weight
re_fuel = weight * im_distance
* factor.
ENDMETHOD.
104
lcl_truck
METHOD estimate_fuel.
DATA: weight ...
weight = empty_load + max_cargo
max_cargo.
re_fuel = weight * im_distance
* factor.
ENDMETHOD.
5. Inheritance Summary
105
Exercise
HOURS
MIN
Exercise
Inheritance
Class Hierarchies
106
6. Casting
107
Upcast
Downcast
Inheritance and Polymorphism
6. Casting
Types of Casts
lcl_vehicle
-
Upcast
lcl_car
108
make
model
ser_no
color
n_o_vehicles
+ constructor()
+ display_attributes()
+ get_count()
lcl_truck
Downcast
lcl_bus
- car_type
- max_seats
- acceleration
- max_cargo
- n_o_tanks
- n_o_wheels
- max_passengers
- lavatory
- television
+
+ get_type()
+
+ get_cargo()
+
+ get_passengers()
6. Casting
Upcast Principle
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_truck
TYPE REF TO lcl_truck.
(5) lcl_truck
Public
r_truck
r_vehicle
Inherited
Geerbt
get_make()
get_count()
Redefined
Redefiniert
* Upcast
r_vehicle = r_truck.
r_truck
r_vehicle
109
display_attributes()
set_attributes()
estimate_fuel()
get_cargo()
6. Casting
Compatibility und Upcast
DATA: r_vehicle
r_truck
level
110
6. Casting
Generic Access?
Client
1
1..*
lcl_rental
(1) lcl_rental
Public
(1) lcl_car
lcl_vehicles
lcl_truck
lcl_bus
Private
itab
(4) lcl_bus
(5) lcl_truck
(2) lcl_car
111
6. Casting
Preparation for Generic Access (1)
Client
(2) lcl_bus
(1) lcl_rental
Public
Private
vehicle_list
(3) lcl_truck
add_vehicle()
(4) lcl_car
Up Cast when
handing over!
...
METHODS: add_vehicle
IMPORTING
im_vehicle TYPE REF TO lcl_vehicle.
...
112
(5) lcl_car
6. Casting
Preparation for Generic Access(2)
(2) lcl_bus
vehicle_list
METHOD calc_estimated_fuel.
DATA:
r_vehicle TYPE REF TO lcl_vehicle.
re_fuel = 0.
LOOP AT vehicle_list
INTO r_vehicle.
re_fuel = re_fuel
+ r_vehicle->estimate_fuel(
im_distance ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
113
(3) lcl_bus
lcl_bus
estimate_fuel()
(4) lcl_truck
(5) lcl_truck
lcl_truck
estimate_fuel()
6. Casting
Polymorphism Generic Access
METHOD calc_estimated_fuel.
DATA: r_vehicle TYPE REF TO lcl_vehicle.
LOOP AT vehicle_list INTO r_vehicle.
ADD r_vehicle->estimate_fuel
r_vehicle->estimate_fuel( im_distance ) TO re_fuel.
ENDLOOP.
ENDMETHOD.
(5) lcl_truck
Public
Inherited/ redefined
estimate_fuel()
vehicle_list
(2) lcl_bus
Public
Inherited / redefined
estimate_fuel()
114
METHOD estimate_fuel
estimate_fuel.
total_weight = max_cargo + weight.
re_fuel = total_weight
* im_distance * factor.
ENDMETHOD.
METHOD estimate_fuel
estimate_fuel.
total_weight = max_passengers
* average_weight + weight.
re_fuel = total_weight
* im_distance * factor.
ENDMETHOD.
msg systems ag, 15.01.2013
6. Casting
Static and dynamic Type of a Reference
(5) lcl_truck
Public
IInherited /
redefinied
estimate_fuel()
Private
estimate_fuel()
(2) lcl_bus
Public
Inherited /
redefinied
estimate_fuel()
Private
r_vehicle
estimate_fuel()
115
6. Casting
Comparison to Procedural Programming
vehicle_list
name category
Object Oriented
truck1
bus2
TYPE TABLE OF vehicle_list_type,
truck2
TYPE vehicle_list_type, ...
truck3
DATA: vehicle_list
vehicle
...
van1
LOOP AT vehicle_list INTO vehicle.
CASE vehicle-category.
WHEN 'TRUCK'.
PERFORM estimate_fuel_truck ...
WHEN 'BUS'.
ADD r_vehicle->estimate_fuel( )
PERFORM estimate_fuel_bus ...
TO needed_fuel.
WHEN 'VAN'.
estimate_fuel_van...
PERFORM estimate_fuel_van
...
ENDCASE.
ADD fuel TO needed_fuel.
ENDLOOP.
116
6. Casting
117
Upcast
Downcast
Inheritance and Polymorphism
6. Casting
Motivation for Downcast
Client
(2) lcl_bus
(1) lcl_rental
Public
(3) lcl_truck
max_cargo
= 30
Private
vehicle_list
get_max_cargo()
(4) lcl_car
(5) lcl_truck
max_cargo
= 50
METHOD get_max_cargo.
DATA: r_vehicle TYPE REF TO lcl_vehicle.
LOOP AT vehicle_list INTO r_vehicle.
" Problem: is this really a truck?
" Determine the max cargo
ENDLOOP.
ENDMETHOD.
118
Truck or other
vehicle?
6. Casting
Example for Downcast
(2) lcl_bus
(1) lcl_rental
Public
Private
vehicle_
list
(3) lcl_truck
max_cargo
= 30
get_max_cargo()
(4) lcl_car
(5) lcl_truck
METHOD get_max_cargo.
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_truck TYPE REF TO lcl_truck.
LOOP AT vehicle_list INTO r_vehicle.
TRY.
r_truck ?= r_vehicle.
" put max cargo in variable re_cargo...
CATCH cx_sy_move_cast_error
cx_sy_move_cast_error.
" react on that cast error
ENDTRY.
ENDLOOP.
ENDMETHOD.
119
max_cargo
= 50
6. Casting
120
Upcast
Downcast
Inheritance and Polymorphism
6. Casting
Semantic and Use of Inheritance
Rules
121
Use of inheritance:
6. Casting
Semantic and Use of Inheritance
Rules
car_red
car_blue
122
change_height
Subclass
square
msg systems ag, 15.01.2013
6. Casting Summary
123
Exercise
HOURS
MIN
Exercise
Casting
Polymorphism (Part 1)
124
Exercise
HOURS
MIN
Exercise
Casting
Polymorphism (Part 2)
125
7. Interfaces
126
Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism
7. Interfaces
Motivation for Interfaces
Service 1
Service 2
Service 3
...
Interface
lcl_hotel
lcl_rental
lcl_airplane
...
127
lcl_vehicles
...
...
...
7. Interfaces
Services of the Interfaces
lcl_travel_agency
display_partner
check_availability
...
Interface
describes and
uses
lcl_hotel
lcl_rental
lcl_airplane
...
128
lcl_vehicles
...
...
...
7. Interfaces
UML Example
lcl_travel_agency
Client
uses
Implementation
Implementationof
more
than one
mehrerer
Interface possible!
Interfaces
mglich!
interface
interface
lif_partners
lif_license
implements
lcl_carrier
lcl_hotel
lcl_rental
lcl_airplane
...
129
lcl_vehicles
...
...
...
7. Interfaces
Definition and Implementation of an Interface
130
Implementation is done in
classes
No visibility layers in
Interfaces; all
components are public
lif_partners
INTERFACE lif_partners.
METHODS: display_partner.
ENDINTERFACE.
7. Interfaces
Characteristics of Interfaces
Polymorphism
Code 2
display_partner
Code 1
Abstraction
Code 3
Code 4
super1
131
interf
sub2
Exercise
HOURS
MIN
Exercise
Interfaces
Definition of an Interface
132
7. Interfaces
133
Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism
7. Interfaces
Working with Interface Components
CLASS lcl_rental IMPLEMENTATION.
...
METHOD lif_partners~display_partner.
"special coding to display partners or
"just calling existing method that fits
display_attributes( ).
ENDMETHOD.
interface
lif_partners
display_partner()
check_availability()
ENDCLASS.
implements
DATA: r_rental TYPE REF TO lcl_rental.
...
CREATE OBJECT r_rental.
* syntactically possible but not useful:
~
r_rental->lif_partners~display_partner(
).
134
lcl_rental
- name
- vehicle_list
+ add_vehicle()
+ display_attributes()
7. Interfaces
Upcast
interface
Upcast
lif_partners
Downcast
lcl_carrier
lcl_hotel
lcl_rental
(1) lcl_rental
ffentlich
r_rental
r_lif
* Up Cast
r_lif = r_rental
Private
Interface
components
display_partner
check_availability
...
r_lif
135
7. Interfaces
Use of Interfaces
(9) lcl_travel_agency
Public
(7) lcl_hotel
Private
partner_list
add_partner()
(1) lcl_rental
(6) lcl_carrier
Up Cast at
handing over!
...
lif_partner
METHODS: add_partner IMPORTING im_partner TYPE REF TO lif_partner.
...
136
7. Interfaces
Use of Interfaces
METHOD display_agency_partners.
DATA: r_partner TYPE REF TO lif_partners.
LOOP AT partner_list INTO r_partner.
r_partner->display_partner( ).
ENDLOOP.
ENDMETHOD.
(7) lcl_hotel
partner_list
(1) lcl_rental
(6) lcl_carrier
137
METHOD ...~display_partner
display_partner .
display_hotel_attributes( ). ...
ENDMETHOD.
METHOD ...~display_partner
display_partner .
display_rental_data( ). ...
ENDMETHOD.
display_partner
METHOD ...~display_partner.
display_attributes( ). ...
ENDMETHOD.
msg systems ag, 15.01.2013
7. Interfaces
Interface References: Down Cast
(3) lcl_travel_agency
book_flight()
partner_list
r_carrier
(7) lcl_hotel
METHOD book_flight.
DATA: r_carrier TYPE REF TO lcl_carrier,
(1) lcl_rental
(6) lcl_carrier
7. Interfaces
139
Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism
7. Interfaces
Compound Interfaces
Problem:
Extension of
the Interface?
interface
lif_partners
book_room()
implements
lcl_hotel
140
lcl_carrier
lcl_rental
7. Interfaces
Compound Interfaces
Solution:
Compound
Interfaces
interface
lif_partners
implements
interfaces:
interface
lif_room_booking
implements
lcl_hotel
141
lcl_carrier
lcl_rental
7. Interfaces
Compound Interfaces: Example
INTERFACE lif_partners.
METHODS display_partner.
ENDINTERFACE.
INTERFACE lif_room_booking.
INTERFACES lif_partners.
METHODS book_room.
ENDINTERFACE.
DATA: i_partner
i_room_book
...
i_partner = i_room_book.
"Narrowing Cast
i_room_book->lif_partner~display_partner( ).
* i_partner->display_partner( ) also possible
i_room_book ?= i_partner.
142
"Widening Cast
msg systems ag, 15.01.2013
7. Interfaces Summary
143
Exercise
HOURS
MIN
Exercise
Interfaces
Use of Interfaces
144
8. Events
145
8. Events Overview
Recipient / Handler
* main program
CREATE OBJECT r_car EXPORTING
Car rental company
Auto
Registration office
Sender
Event: vehicle_created
146
...
8. Events
Properties
Application
GUI implementation
Conformity to other object models:
COM, ActiveX Controls, OpenDoc ...
147
8. Events
Triggering and Handling Events: Overview
Rules
148
Trigger events
Handle events
8. Events
Defining and Triggering Events: Syntax
CLASS <classname> DEFINITION.
EVENTS: <event> EXPORTING VALUE(<ex_par>)
EVENTS
VALUE(
) TYPE <type>.
CLASS <classname> IMPLEMENTATION.
Typing of all parameters
METHOD <methodname>.
RAISE EVENT <event> EXPORTING <ex_par> = <act_par>.
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING ... .
1.
EVENTS vehicle_created.
...
ENDCLASS.
car
vehicle_created
8. Events
Event handling and registering
Sender
car1
Handler
rental
vehicle_created
registration
office
car2
vehicle_created
carrier
truck
150
vehicle_created
8. Events
Handling method
CLASS <class_handle> DEFINITION.
No Typing, only enumeration
METHODS: <on_event> FOR EVENT <event>
METHODS
OF <class/interfacename>
IMPORTING <ex_par1> ... <ex_parN> SENDER.
SENDER
CLASS lcl_rental DEFINITION.
...
PRIVATE SECTION.
METHODS: add_vehicle FOR EVENT vehicle_created OF lcl_vehicle
3.
IMPORTING SENDER.
SENDER
ENDCLASS.
rental
car1
add_vehicle
vehicle_created
151
8. Events
Registering Event Handling: Syntax
SET HANDLER <ref_handle>-><on_event>
FOR <ref_sender> | FOR ALL INSTANCES
[ACTIVATION <var>].
car1
SET HANDLER
rental
add_vehicle
vehicle_created
152
8. Events
Registration/Deregistration: Handler Tables
car2
handling
method
add_vehicle
reg_vehicle
registered
object
registration
office
car1
153
handling
method
add_vehicle
reg_vehicle
rental
registered
object
registration
office
8. Events
Event Handling: Properties
Event
Public
Private
154
Handler
Private
Public
8. Events
Summary
155
Exercise
HOURS
MIN
Exercise
Event Triggering
and Handling
156
9. Global Classes/Interfaces
157
9. Global Classes/Interfaces
Rewind: Local Classes/Interfaces
Program-1
Program-2
REPORT prog1.
REPORT prog2.
DATA: r_airplane
TYPE REF TO lcl_airplane
lcl_airplane.
...
158
Local classes are visible only in the program, in which they are defined.
No global access possible
Not created in Repository, no Where-Used List etc.
9. Global Classes/Interfaces
159
9. Global Classes/Interfaces
Global Classes in SE80 Created in the Object Navigator
160
9. Global Classes/Interfaces
Class Builder: Attributes
161
9. Global Classes/Interfaces
Class Builder: Methods
Redefinition
Undo Redefinition
Set the
signature parameter of the
selected method,
Here: CONSTRUCTOR
162
9. Global Classes/Interfaces
Class Builder: Definition of Method Signatures
Set the
signature parameter
163
9. Global Classes/Interfaces
Class Builder: Exception Interface
Set the
possible exceptions
164
9. Global Classes/Interfaces
Class Builder: Defining Interfaces
1) Declaring the global
interfaces in a global
class
2)
165
Implementation of methods
from a global class
9. Global Classes/Interfaces
Class Builder: Implementation of Methods
Signature displayed to help
with the implementation of the
method
166
9. Global Classes/Interfaces
Class Builder: Inheritance
Make
superclass
known
167
9. Global Classes/Interfaces
Class Builder: Testing Environment
Test
class
Create
instance
Call
methods
168
9. Global Classes/Interfaces
169
9. Global Classes/Interfaces
Global Classes in the Object Navigator
170
9. Global Classes/Interfaces
Class Builder: CREATE OBJECT
9. Global Classes/Interfaces
Class Builder: CALL METHOD
Drag&Drop of a method
generates a method call.
172
9. Global Classes/Interfaces
173
9. Global Classes/Interfaces
The ALV Grid Control
Details
Sort
Filter
Search
Sum
Chart
Export
Views
(Table, Grid, Excel)
CL_GUI_ALV_GRID
174
9. Global Classes/Interfaces
Integrating a Grid-Control instance in a dialog program
For using the ALV Grid
Control, two classes are
needed.
Dynpro
Area
(1) cl_gui_custom_container
Public
Custom
Container
Control
Private
(3) cl_gui_alv_grid
Public
Private
ALV
Grid
Control
175
9. Global Classes/Interfaces
Displaying data in an ALV-Grid
...
cl_gui_custom_container
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_grid
TYPE REF TO cl_gui_alv_grid .
...
CREATE OBJECT go_container
EXPORTING container_name = 'CONTAINER_1'
...
CREATE OBJECT go_grid
EXPORTING i_parent = go_container
...
(1) cl_gui_custom...
Public
Private
(1) cl_gui_alv_grid
Public
Private
go_grid->set_table_for_first_display(
set_table_for_first_display
i_structure_name = 'SPFLI'
it_outtab
= gt_flights).
...
176
9. Global Classes/Interfaces
Summary
177
Exercise
HOURS
MIN
Exercise
Global Classes
Creating a Class
178
179
Class cannot be
instantiated
Method is not
implemented in this
class
180
lcl_vehicle
lcl_truck
...
lcl_bus
...
...
181
182
Function 1
Function 3
(1) lcl_daten
Public
Private
Change
STOP
(2) lcl_daten
Change
Function 2
183
Public
Private
Function 4
CREATE PUBLIC
Each user (Client) can create class instances.
(Default)
CREATE PROTECTED
Only the class itself and all its subclasses can create instances of this
class.
CREATE PRIVATE
Only the class itself can create instances of itself..
184
Possible use:
Singleton design pattern
(1) lcl_singleton
Public
Private
mo_singl
get_instance
Client
lazy creation
Insantiating on first access
eager creation
Generation in the class constructor
lcl_singleton
lcl_singleton
- mo_single:lcl_singleton
+ get_instance():lcl_singleton
- mo_single:lcl_singleton
+ get_instance():lcl_singleton
+ class_constructor()
METHOD get_instance.
IF mo_single IS INITIAL.
CREATE OBJECT mo_single.
ENDIF.
ro_single = mo_single.
ENDMETHOD.
185
METHOD class_constructor.
CREATE OBJECT mo_single.
ENDMETHOD.
METHOD get_instance.
ro_single = mo_single.
ENDMETHOD.
Exercise
HOURS
MIN
Exercise
Programming Techniques
Singleton
186
187
188
!'.
!`.
" No truncation
lv_str = lv_str && lv_str.
" Pretty: *
"
*
"
*
"
*
"
CALL METHOD
|Today is
localization,
space before '.',
short,
directly usable
in method call
lo_meth(
{ sy-datum
DATE = USER }.| ).
189
Package
Friend
Grants friendship
(5) lcl_flight_data
Private
flight_list
190
(3) lcl_flight_factory
Public
Friend
(4) lcl_flight_planer
Public
create_flight()
get_flight()
plan_flight()
delete_flight()
schedule()
191
ABAP Program
Transient Objects
Persistence Service
Read / write objects
Database
Persistent data in
transparent tables
192
193
Within the Persistence Services, the Class Agent takes over the
management of the persistent objects.
The actor provides several services (method calls), for managing the objects and
the encapsulated object data
The class actor is a singleton instance, addressed with the public static attribute
agent.
...
DATA: r_carrier TYPE REF TO cl_carrier,
r_agent
TYPE REF TO ca_carrier,
carrname TYPE
s_carrname.
TRY.
agent
r_agent = ca_carrier=>agent.
r_carrier = r_agent->get_persistent( i_carrid = 'LH' ).
carrname
= r_carrier->get_carrname( ).
WRITE: 'LH: ', carrname.
CATCH cx_os_object_not_found.
ENDTRY.
194
195
Exercise
HOURS
MIN
Exercise
Programming Techniques
Friendship
196
197
:lcl_driver
:lcl_car
:lcl_tank
get_fuel_level( )
get_fuel_level( )
break
[Fill level
sensor
failed ]
exception
sensor_failed
alert( )
re_level
re_level
198
:lcl_driver
:lcl_car
:lcl_tank
get_fuel_level( )
get_fuel_level( )
break
[Fill level
sensor
failed ]
exception
sensor_failed
pit_stop( )
re_level
re_level
199
Exception class
(predefined or
your own)
ABAP Program
...
Exception raised
using the ABAP
statement or by the
runtime environment
msg systems ag, 15.01.2013
cx_...
...
[INTO lx_exc1].
...
CATCH cx_...
...
Behandler
Handlers fr
fordie
the
angegebenen
specified exception
Ausnahmeklassen
classes and their
und
deren
subclasses
Unterklassen
CLEANUP.
...
ENDTRY.
201
cx_root
- textid
- kernel_errid
...
+ get_text()
+ get_source_position()
cx_no_check
cx_dynamic_check
cx_sy_arithmetic_error
cx_static_check
cx_sy_move_cast_error
cx_sy_arithmetic_overflow
202
TYPE i,
TYPE i.
...
TRY.
result = int1 * int2.
WRITE result.
CATCH cx_sy_arithmetic_overflow INTO lx_exc.
MESSAGE lx_exc TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
...
203
205
206
Example:
im_type = 727-301
TRY.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx_wrong_planetype
EXPORTING planetype = im_type.
ENDIF.
" Operate on valid values
" in ex_weight and ex_tankcap
CATCH zcx_wrong_planetype INTO lx_exc.
text = lx_exc->get_text( ).
MESSAGE text TYPE 'I'.
ENDTRY.
ENDMETHOD.
207
<im_parameter>
<ex_parameter>
cx_exception
cx_exception.
REPORT propagate_exceptions.
...
DATA r_obj TYPE REF TO class.
...
TRY.
r_obj->meth( EXPORTING ...
IMPORTING ... ).
CATCH cx_exception .
...
ENDTRY.
208
209
:lcl_driver
:lcl_car
:lcl_tank
get_fuel_level( )
get_fuel_level( )
break
[ Fill level
sensor
failed ]
re_level
210
resumable_exception
sensor_failed
RESUME ...
re_level
METHOD display_attributes.
DATA: lx_exc TYPE REF TO cx_root,
text
TYPE string.
...
TRY.
get_technical_attributes(
EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: /'Gewicht:'(003), weight,
'Tankkapazitt:'(004), cap.
CATCH zcx_wrong_planetype INTO lx_exc
lx_exc.
MESSAGE lx_exc TYPE 'I'.
ENDTRY.
ENDMETHOD.
211
METHOD get_technical_attributes.
RAISE EXCEPTION
TYPE zcx_wrong_planetype
im_type
EXPORTING pl_type = im_type.
ENDIF.
ENDMETHOD.
msg systems ag, 15.01.2013
cx_root
cx_no_check
212
cx_dynamic_check
cx_static_check
213
Exercise
HOURS
MIN
Exercise
Exception Handling
Class-based Exceptions
214
215
Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming
TYPE
<Structure>,
TYPE TABLE OF <Structure>,
TYPE
ANY,
TYPE
ANY TABLE.
Field symbols
216
are "assigned".
resp.
<fsob>
Index
Name
Fname
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.800
Berg
Lisa
35
2.400
<fs2>
Rei
Tim
24
1.800
<fs3>
<fs1>
217
Index
Name
Fname
Huber
Lotte
Maier
Klaus
Index
Name
Fname
Berg
Lisa
<fs>
2
Kraus
Rei
Karl
Tim
Index
Name
Fname
Kraus
Karl
Moser
Hans
In addition, he casting
type can be explicitly
specified.
* Option 2: Explicit
FIELD-SYMBOLS <fs> TYPE ANY
ANY.
ASSIGN sy-datum TO <fs>
CASTING TYPE st_date.
20120221
* Access after cast:
WRITE: / <fs>-year, <fs>-month, <fs>-day.
<fs>
218
219
Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming
lv_tcode = 'T_CODE'.
CALL TRANSACTION lv_tcode
lv_tcode.
lv_report_name = 'REPORT'.
(lv_report_name)
SUBMIT (lv_report_name).
220
TYPE-POOLS: abap.
Transfer type
ptab
NAME
KIND
Exception
name
etab
221
Reference to the
actual parameter
TYPE REF TO
Formal
parameter
name
NAME
VALUE
sy-subrc
value
VALUE
222
Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming
DATA
ref
Any completely
specified type
Generic type
lr_ref
lv_num
15
lr_ref
lv_num
15
Get reference to a
data object
223
abap_parmbind_tab,
LINE OF ptab,
abap_methname,
string VALUE `VOLVO`.
wa_ptab-name = 'EX_MAKE'.
GET REFERENCE OF gd_make
INTO wa_ptab-value = ref.
INSERT wa_ptab INTO TABLE ptab.
ptab
meth = 'GET_MAKE'.
CALL METHOD r_truck->(meth)
PARAMETER-TABLE ptab.
gd_make
NAME
KIND
VALUE
EX_MAKE
`VOLVO`
WRITE gd_make.
224
DATA ref
{ TYPE REF TO type_name | LIKE REF TO var
CREATE DATA ref.
DATA ref
CREATE DATA ref
}.
PARAMETERS pa_tab
TYPE dd021-tabname.
DATA
ref_itab TYPE REF TO data.
ref_itab
PARAMETERS pa_tab
DATA
ref_itab
TYPE dd021-tabname.
TYPE REF TO data.
ref_itab
fs_itab
226
1.
2.
3.
4.
5.
227
6.
7.
228
Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming
Number of rows in
the internal table
Number of initial
lines
Table kind
table_kind
Constants for
evaluating the
return values
229
TYPE-POOL: sydes.
sydes_kind-undefined
sydes_kind-standard
sydes_kind-sorted
sydes_kind-hashed
msg systems ag, 15.01.2013
Rules
CL_ABAP_DATADESCR
CL_ABAP_ELEMDESCR
CL_ABAP_REFDESCR
CL_ABAP_COMPLEXDESCR
CL_ABAP_STRUCTDESCR
CL_ABAP_TABLEDESCR
CL_ABAP_OBJECTDESCR
CL_ABAP_CLASSDESCR
CL_ABAP_INTFDESCR
230
START-OF-SELECTION.
...
"get reference to type description object by widening cast:
descr_ref ?= cl_abap_typedescr=>describe_by_data( <fs_wa> ).
...
TOP-OF-PAGE.
LOOP AT descr_ref->components INTO wa_comp.
WRITE wa_comp-name.
ENDLOOP.
231
232
Field Symbol
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming
wa-wage
= '1.900'.
MODIFY itab FROM wa.
ENDIF.
233
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.800
Berg
Lisa
35
2.400
Rei
Tim
27
2.000
Index
Name
FName
Age
Wage
Maier
Klaus
24
1.900
<fs>-wage = '1.900'.
ENDIF.
234
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.900
1.800
Berg
Lisa
35
2.400
Rei
Tim
27
2.000
<fs>
= '1.900'.
ENDLOOP.
235
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.800
Berg
Lisa
35
2.400
Rei
Tim
24
2.000
Index
Name
FName
Age
Wage
Maier
Klaus
24
1.900
Index
Name
FName
Age
Wage
Rei
Tim
24
1.900
ENDLOOP.
236
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.900
1.800
Berg
Lisa
35
2.400
Rei
Tim
24
1.900
<fs>
ENDLOOP.
LOOP AT itab2 INTO wa2.
wa2-kz = 'U'.
MODIFY itab FROM wa1.
wa2.
ENDLOOP.
237
100% dynamic.
Easy to extend
Very complex!
ENDIF.
ENDLOOP.
238
= 0.
IF sy-subrc
<fs> IS ASSIGNED.
239
<fs>-wage = '1.400'.
ENDIF.
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
1.800
Berg
Lisa
35
2.400
Rei
Tim
27
2.000
<fs>
240
Index
Name
FName
Age
Wage
Huber
Lotte
18
1.200
Maier
Klaus
24
Berg
Lisa
35
1.900
1.800
1.400
2.400
Rei
Tim
27
2.000
<fs>
241
Bibliography
242
Thank
you for
Vielen Dank
fryour
Ihreattention!
Aufmerksamkeit
Hubert Englmaier
Senior IT Consultant
Insurance
www.msg-systems.com
243