Beruflich Dokumente
Kultur Dokumente
ABAP OBJECTS
Agenda
Day 1 Introduction to OOPS OOPS Principles ABAP Objects Inheritance Exercise Programs Day 2 Event Handling Global classes Advanced Concepts Exercise Programs
Introduction
Contents:
Procedural programming Object-oriented programming Aims of the ABAP Objects programming language
C++
ABAP
Introduction : Overview
Procedural Programming
Functions are defined independently of data structures Direct access to data
Data
Data
Data
Data
Data
Function Function
Function Function
Function Function
Function Function
Data declaration
...
Function module A3
1 counter
Introduction : Overview
Procedural Programming
Object-Oriented Programming
Date
Why OOPs
Extensibility
Re-usability
Date
OBJECTS
House
Data
Data
Crane
Method Method Method
Data
Boat
Data
Objects are an abstraction of the real world Objects are units made up of data and of the functions belonging to that data
Extensibility through
Polymorphism Inheritance
Extensibility
Re-usability through
Re-usability
Objects -> objects Their state -> attributes Their functions -> methods
Longer development phase before first results ready Paradigm break between object-oriented programs and Procedural Programming
Outlook
OO application
OO application BOR
ABAP Objects
True, compatible extension of ABAP ABAP Objects statements can be used in conventional ABAP programs ABAP statements can be used in ABAP Objects programs
* ABAP Program CLASS lcl _airplane DEFINITION. ... ENDCLASS. ... TYPES: ... DATA: ... ...
* ABAP Objects Program DATA: counter TYPE i. ... CREATE OBJECT ... counter = counter + 1. ...
Date
Questions ?
OOPS Principles
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Principles : Overview-1
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Object-1
Example: airplane
Attributes Attributes Plane ID: ID0057231 Weight: 30,000 kg Length: 70 m Methods Events Events landed Name: LH Berlin Methods land fly
Principles : Overview-2
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Definition part The class components (for example, attributes and methods) are defined in this part. Implementation part This part only contains the method implementations.
Principles : Overview-3
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Attributes
Attribute types can have any kind of data type:
Elementary types:
lcl _tank
Attributes : Syntax
CLASS CLASS < <classname classname> > DEFINITION. DEFINITION. ... ... TYPES: TYPES: < < normale normale Typdefinition Typdefinition >. >. CONSTANTS: CONSTANTS: constant constant TYPE TYPE <type> <type> VALUE VALUE <value>. <value>. DATA: DATA: variable1 variable1 variable2 variable2 variable3 variable3 variable4 variable4 variable5 variable5 variable6 variable6 variable7 variable7 CLASS-DATA: CLASS-DATA: ... ... ENDCLASS. ENDCLASS. TYPE TYPE <type>, <type>, TYPE TYPE < <ddic ddic_type>, _type>, LIKE LIKE variable1, variable1, TYPE TYPE <type> <type> VALUE VALUE <value>, <value>, TYPE TYPE <type> <type> READ-ONLY READ-ONLY, , TYPE TYPE REF REF TO TO < <classname classname>, >, TYPE TYPE REF REF TO TO <interface>. <interface>.
Attributes : Visibility
CLASS CLASS lcl lcl_airplane _airplane DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. DATA DATA: : name name TYPE TYPE string. string. PRIVATE PRIVATE SECTION. SECTION. DATA: DATA: weight weight TYPE TYPE saplane saplane-weight. -weight. ENDCLASS. ENDCLASS. CLASS CLASS lcl lcl_airplane _airplane DEFINITION. DEFINITION.
Public attributes
Can be viewed and changed by all users and in all methods Direct access
Private attributes
Can only be viewed and changed from within the class No direct access from outside the class
better
PUBLIC PUBLIC SECTION. SECTION. ... ... PRIVATE PRIVATE DATA DATA: : SECTION. SECTION. weight TYPE weight TYPE saplane saplane-weight, -weight, name TYPE string. name TYPE string.
ENDCLASS. ENDCLASS.
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. PRIVATE SECTION. DATA : weight TYPE saplane -weight, name TYPE string.
Static attributes
count: 2
Main memory
Static attributes for class LCL_AIRPLANE
Principles : Overview-4
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Methods
fly land
Methods : Syntax
CLASS < classname > DEFINITION . ... METHODS : <method_name> [ IMPORTING < im _var > TYPE <type> EXPORTING <ex_ var > TYPE <type> CHANGING < ch _var > TYPE <type> RETURNING VALUE( <re_ var >) TYPE <type> EXCEPTIONS <exception> ]. ENDCLASS.
CLASS < classname > IMPLEMENTATION . METHOD <method_name>. ... ENDMETHOD . ENDCLASS.
Methods : Visibility
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS : set_name importing im _name TYPE string. PRIVATE SECTION. METHODS : init _name. DATA: name TYPE string. ENDCLASS. CLASS lcl _airplane IMPLEMENTATION. METHOD init _name. name = No Name. ENDMETHOD . METHOD set_name. IF im _name IS INITIAL. * Calling init _name ... ELSE. name = im _name. ENDIF. ENDMETHOD . ENDCLASS.
Public methods
Private methods
Can use both static and instance components in the implementation part Can be called using the instance name
Static methods
Can only use static components in the implementation part Can be called using the class name
Principles : Overview-5
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Creating Objects
CREATE OBJECT
Reference Variables
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. ... PRIVATE SECTION. ... ENDCLASS.
CLASS lcl _airplane IMPLEMENTATION. ... ENDCLASS. DATA: airplane1 TYPE REF TO cl _airplane, airplane2 TYPE REF TO cl _airplane.
airplane1
airplane1
name: weight: 0
airplane2
Assigning References
... DATA: airplane1 TYPE REF TO airplane2 TYPE REF TO CREATE OBJECT airplane1. CREATE OBJECT airplane2. airplane1 = airplane2. lcl _airplane, lcl _airplane.
airplane1
name: weight: 0
airplane2
Garbage Collector
... DATA: airplane1 TYPE REF TO lcl _airplane, airplane2 TYPE REF TO lcl _airplane. CREATE OBJECT airplane1 EXPORTING ... . CREATE OBJECT airplane2 EXPORTING ... . airplane1 = airplane2.
airplane1
airplane2
Principles : Overview-6
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
References : Comparison
DATA: airplane1 TYPE REF TO airplane2 TYPE REF TO lcl _airplane, lcl _airplane.
CREATE OBJECT airplane1 EXPORTING im _name = LH Berlin ... CREATE OBJECT airplane2 EXPORTING im _name = LH Berlin ... IF airplane1 = airplane2 . ... ENDIF. not equal to
Buffering References -1
DATA: airplane airplane_table TYPE REF TO cl _airplane, TYPE TABLE OF REF TO cl_airplane.
airplane airplane_table
Main memory
Main memory
Buffering References -2
LOOP AT TO airplane_table INTO airplane. * work with the current instance
ENDLOOP.
airplane airplane_table
Main memory
lcl_airplane
lcl_wings
orientat .: left length: 15 m name: LH Berlin weight: 30,000 kg left_wing: right_wing: orientat .: right length: 15 m
name: LH Berlin
Calling Methods
Run Data
Do_it Data
O1
O2
Static methods:
DATA: airplane TYPE REF TO lcl _airplane. DATA: name TYPE string. DATA: count_planes TYPE I. CREATE OBJECT airplane. CALL METHOD airplane->set_name EXPORTING im _name = name. CALL METHOD l cl_airplane=>get_count RECEIVING re_count = count_planes.
Constructor
Special method for creating objects with defined initial state Only has IMPORTING parameters and EXCEPTIONS Exactly one constructor is defined per class (explicitly or implicitly) Is executed exactly once per instance
METHODS CONSTRUCTOR IMPORTING <im _parameter> EXCEPTIONS <exception>. name: LH Berlin weight: 30,000 kg lcl _airplane name weight count constructor
CREATE OBJECT
Constructor : Example
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS CONSTRUCTOR IMPORTING im _name TYPE string im _weight TYPE I.
PRIVATE SECTION. DATA: name TYPE string, weight TYPE I. CLASS-DATA count TYPE I. ENDCLASS. CLASS lcl _airplane IMPLEMENTATION. METHOD CONSTRUCTOR. name = im _name. weight = im _weight. count = count + 1. ENDMETHOD. ENDCLASS. DATA airplane TYPE REF TO lcl _airplane. name: LH Berlin ... weight: 30,000 kg CREATE OBJECT airplane EXPORTING im _name = `LH Berlin` im _weight = 30000.
Static Constructor
CLASS <classname > DEFINITION. PUBLIC SECTION. CLASS-METHODS CLASS_CONSTRUCTOR . ENDCLASS. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. CLASS-METHODS: CLASS_CONSTRUCTOR, get_count RETURNING VALUE(re_count) TYPE I. CLASS-DATA: count TYPE I. ENDCLASS. CLASS lcl_airplane IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR . ... ENDMETHOD. ... ENDCLASS.
Principles Overview -7
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles
Encapsulation
Class as capsule for functions Defined responsibilities within a capsule (class) Defined interfaces using
Data
Run
Client
Server
lcl _airplane lcl _client tank : lcl _tank get_fuel_level () : re_level fuel : i
re_level = tank->get_fuel_level ( ).
Attribute names Method names Event names Type names Constant names ALIAS names
Namespace : Example
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS CONSTRUCTOR IMPORTING im _name TYPE string im _weight TYPE I. PRIVATE SECTION. DATA name TYPE string. DATA weight TYPE I. ENDCLASS. CLASS cl _airplane IMPLEMENTATION. METHOD CONSTRUCTOR. DATA name TYPE string VALUE `-airplane`. CONCATENATE weight = ENDMETHOD. ENDCLASS. im _name name INTO ME->name .
im _weight.
Date
Questions ?