Você está na página 1de 51

Global network

of innovation

Principles
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

Global network
of innovation

Principles : Overview-1
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

Global network
of innovation

Object-1
Example: airplane

Attributes
Plane ID: ID0057231
Weight: 30,000 kg
Length: 70 m

Attributes
Name: LH Berlin
Methods
land

fly

Methods
Events

Private access
Encapsulation
As a rule, attributes

Events
landed

Public access
Interface

As
a rule,methods, events

Global network
of innovation

Object-2
What characterizes an object?

Identity

Status (quantity of attributes)

Behavior (quantity of methods and events)

What synonyms are used for objects?

Object

Instance

Global network
of innovation

Example of Objects

Eberwein
Plane ticket
1
.
Mr
Departure: Berlin: 06:10 p.m.
Name:
Name: Mr Gore
Departure: Mumbai: 06:10 p.m.

Mllerschn
2
2Plane ticket
Mr.
Name:
Name:Mr Dongare
Departure: Pune : 9:15 p.m.

Global network
of innovation

Classification

Plane

Plane ticket

Mllerschn
Mllerschn
Plane
ticket
Plane ticket.. 22
Mr
Mr
Departure:
Departure: Munich:
Munich: 11:25
11:25 a.m.
a.m.
Name:
Name:

Eberwein
Eberwein
Plane
Plane ticket
ticket.. 11
Mr
Mr
Departure:
Departure: Berlin:
Berlin: 06:10
06:10 p.m.
p.m.
Name:
Name:

Global network
of innovation

Principles : Overview-2
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

Global network
of innovation

Class : Blueprint of Object


CLASS < classname > DEFINITION.
ENDCLASS.

CLASS < classname > IMPLEMENTATION.


ENDCLASS.

Definition part
The class components (for
example, attributes and methods)
are defined in this part.
Implementation part
This part only contains the method
implementations.

Global network
of innovation

Important Components in a Class


Attributes

Data

Determine the state of the object

Methods

Executable coding

Determine the behavior of the object

Global network
of innovation

Principles : Overview-3
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

10

Global network
of innovation

Attributes
Attribute types can have any kind of data type:

Elementary types:

C, I, P, STRING

TYPE REF TO (References to objects/interfaces)

Define your own types

lcl_airplane
name: LH
weight: 30 000
Berlin
kg
tank:

lcl_tank

11

Global network
of innovation

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

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

CLASS-DATA:
CLASS-DATA: ...
...
ENDCLASS.
ENDCLASS.

12

Global network
of innovation

Attributes : Visibility
CLASS
CLASS lcl
lcl_airplane
_airplane DEFINITION.
DEFINITION.
PUBLIC
PUBLIC SECTION.
SECTION.
DATA
DATA:
: name
name TYPE
TYPE string.
string.

Public attributes

Can be viewed and


changed by all users and
in all methods

Direct access

PRIVATE
PRIVATE SECTION.
SECTION.
DATA:
DATA: weight
weight TYPE
TYPE saplane
saplane-weight.
-weight.
ENDCLASS.
ENDCLASS.
CLASS
CLASS lcl
lcl_airplane
_airplane DEFINITION.
DEFINITION.

Private attributes

Can only be viewed and


changed from within the
class

PUBLIC
PUBLIC SECTION.
SECTION.
...
...

PRIVATE
PRIVATE SECTION.
SECTION.
DATA
:
weight
TYPE
DATA : weight
TYPE saplane
saplane-weight,
-weight,
better
name
TYPE
string.
name TYPE string.

No direct access
from outside the class

ENDCLASS.
ENDCLASS.

13

Global network
of innovation

Attributes : Instance and Static-1


Instance attributes

One per instance

Statement: DATA

CLASS lcl _airplane DEFINITION.


PUBLIC SECTION.
PRIVATE SECTION.
DATA : weight TYPE saplane -weight,
name
TYPE string.

Static attributes
CLASS-DATA : count TYPE I.

Only one per class

Statement: CLASS-DATA

Also known as class attributes

ENDCLASS.

14

Global network
of innovation

Attributes : Instance and Static-2

count:
2

name: LH Berlin
weight: 30,000 kg

name: AA Boston
weight: 45,000 kg

Main
memory
Static attributes for class LCL_AIRPLANE

15

Global network
of innovation

Principles : Overview-4
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

16

Global network
of innovation

Methods

Contain coding
Have an interface

lcl _airplane
...

fly
land

17

Global network
of innovation

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.

18

Global network
of innovation

Methods : Visibility

Public methods

Can be called from


outside the class

Private methods

Can only be called


within the class

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.

19

Global network
of innovation

Methods : Instance and Static


Instance 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

20

Global network
of innovation

Methods Instance and Static : Example


CLASS lcl _airplane DEFINITION.
PUBLIC SECTION.
METHODS :
set_name IMPORTING im _name TYPE string.
CLASS-METHODS : get_count RETURNING VALUE(re_count) TYPE I.
PRIVATE SECTION.
DATA:
name TYPE string.
CLASS-DATA: count TYPE I.
ENDCLASS.
CLASS lcl _airplane IMPLEMENTATION.
...
METHOD get_count.
re_count = count .
ENDMETHOD
ENDCLASS.

21

Global network
of innovation

Attributes & Methods : UML Notations


Class name
Attributes

lcl _airplane
- name: string
- count: i

Methods

+ public components

+ set_name( im _name: string)

- private components

+ get_count(): i

_ static components marked with


an underscore

- set_count( im_count: i)
CLASS lcl _airplane DEFINITION.
PUBLIC SECTION.
METHODS:
set_name IMPORTING im _name TYPE string.
CLASS-METHODS: get_count RETURNING VALUE(re_count) TYPE I.
PRIVATE SECTION.
DATA:
name TYPE string.
CLASS-DATA: count TYPE I.
METHODS: set_count IMPORTING im _count TYPE i.
ENDCLASS.

22

Global network
of innovation

Principles : Overview-5
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

23

Global network
of innovation

Creating Objects
Objects can only be created and addressed using
reference variables

lcl _airplane
name
weight
...

CREATE OBJECT

name: LH Berlin
weight: 30,000 kg

24

Global network
of innovation

Referance 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

airplane2
Main memory

25

Global network
of innovation

Creating Objects : Syntax


CREATE OBJECT <reference>.

DATA: airplane1 TYPE REF TO lcl _airplane,


airplane2 TYPE REF TO lcl _airplane.
CREATE OBJECT airplane1.
CREATE OBJECT airplane2.

airplane1

airplane2

name:
weight: 0

name:
weight: 0
Main memory

26

Global network
of innovation

Assigning References
...
DATA: airplane1 TYPE REF TO
airplane2 TYPE REF TO

lcl _airplane,
lcl _airplane.

CREATE OBJECT airplane1.


CREATE OBJECT airplane2.
airplane1 = airplane2.

airplane1

airplane2

name:
weight: 0

name:
weight: 0
Main memory

27

Global network
of innovation

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

name: LH B
weight: 30,000 kg

name: AA Bost
weight: 45,000 kg
Main memory

28

Global network
of innovation

Garbage Collector : Procedure

All independent references in the global main memory are checked. The
references point to active objects, which are marked internally.

If class or instance attribute references point to other objects, these are


also marked.

Objects that are not marked are deleted from the main memory.

Main memory

29

Global network
of innovation

Principles : Overview-6
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

30

Global network
of innovation

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.

airplane1

airplane2

not equal to

n: LH Berlin
w: 30,000 kg
n: LH Berlin
w: 30,000 kg
Main memory

31

Global network
of innovation

Buffering References -1
DATA: airplane
airplane_table

TYPE REF TO cl _airplane,


TYPE TABLE OF REF TO cl_airplane.

CREATE OBJECT airplane.


APPEND airplane TO airplane_table.

airplane
airplane_table

Main memory

CREATE OBJECT airplane.


APPEND airplane TO airplane_table.

airplane
airplane_table

Main memory

32

Global network
of innovation

Buffering References -2
LOOP AT TO airplane_table INTO airplane.
*

work with the current instance

ENDLOOP.

airplane

airplane_table
2

Main memory

33

Global network
of innovation

Object Reference as Attributes

lcl_airplane

name: LH Berlin
weight: 30,000 kg
left_wing:
right_wing:

lcl_wings

orientat .: left
length: 15 m

orientat .: right
length: 15 m

34

Global network
of innovation

External Access to Public Attributes


Instance attribute:
<reference> -><instance_attribute>
Class attribute:
<classname> => <class_attribute>

name: LH Berlin

CLASS lcl _airplane DEFINITION.


PUBLIC SECTION .
DATA : name
TYPE string READ-ONLY.
CLASS-DATA : count TYPE I READ-ONLY.
...
ENDCLASS.
...
DATA: airplane1 TYPE REF TO lcl _airplane.
DATA: airplane_name TYPE STRING,
n_o_airplanes TYPE i.
...
airplane_name = airplane1->name .
n_o_airplanes = lcl _airplane=>count .

35

Global network
of innovation

Calling Methods

Run

Data

O1

call method O2->Do_it

Do_it

Data

O2

36

Global network
of innovation

Calling Methods : Syntax


Instance methods:

CALL METHOD <instance> -><instance_method>


EXPORTING < im_var > = <variable>
IMPORTING <ex_ var > = <variable>
CHANGING < ch_var > = <variable>
RECEIVING <re_ var > = <variable>
EXCEPTIONS <exception> = < nr>.

Static methods:

CALL METHOD < classname >=> <class_method>


EXPORTING ... .

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.

37

Global network
of innovation

Functional Methods
When defining:
RETURNING parameters
Only IMPORTING parameters and exceptions are also possible

When calling:
RECEIVING parameters, or ...
... Various forms of direct call possible:

MOVE, CASE, LOOP


Logical expressions (IF, ELSEIF, WHILE, CHECK, WAIT)
Arithmetic expressions and bit expressions (COMPUTE)

38

Global network
of innovation

Functional Methods : Example


CLASS lcl _airplane DEFINITION.
PUBLIC SECTION.
METHODS: estimated_fuel_consumption
IMPORTING im _distance
TYPE ty _distance
RETURNING VALUE(re_fuel) TYPE ty _fuel,
CLASS-METHODS: get_count RETURNING VALUE(re_count) TYPE i.
ENDCLASS.
DATA: plane1
plane2
fuel_consumption
count_planes

TYPE REF TO lcl _airplane,


TYPE REF TO lcl _airplane,
TYPE ty _fuel,
TYPE i.

* Instantiation omitted
* CALL METHOD plane1->get_count RECEIVING re_count = count_planes.
count_planes = lcl _airplane=>get_count( ).
fuel_consumption =
plane1->estimated_fuel_consumption( 1000 )
+ plane2->estimated_fuel_consumption( im _distance = 1500 ).

39

Global network
of innovation

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)

lcl _airplane
name
weight
count
constructor

CREATE OBJECT

Is executed exactly once per


instance
METHODS CONSTRUCTOR IMPORTING <im _parameter>
EXCEPTIONS <exception>.

name: LH Berlin
weight: 30,000 kg

40

Global network
of innovation

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.

41

Global network
of innovation

Static Constructor
CLASS <classname> DEFINITION.
PUBLIC SECTION.
CLASS-METHODS CLASS_CONSTRUCTOR.
ENDCLASS.

CLASS <classname> IMPLEMENTATION.


METHOD CLASS_CONSTRUCTOR.
...
ENDMETHOD.
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.

42

Global network
of innovation

Call To Static Constructor


Special static method

* Example 1:

Automatically called
before the class is first
accessed

DATA airplane TYPE REF TOcl_airplane.

Only executed once per


program

* Example 2:

CREATE OBJECT airplane


.

DATA class_id TYPE string.


class_id = lcl_airplane=>count.
* Example 3:
DATA count_airplane TYPE I.
CALL METHOD lcl_airplane=>get_count
RECEIVING re_count = count_airplane.

43

Global network
of innovation

Principles Overview -7
Objects
Classes
Attributes
Methods
Instantiation, Garbage Collector
Working with Objects
Further Principles

44

Global network
of innovation

Encapsulation
Class as capsule for functions
Defined responsibilities within a capsule (class)
Defined interfaces using

Public components of class (PUBLIC SECTION)

Interfaces

Implementation of component remains hidden through


limited visibility (PRIVATE SECTION)

45

Global network
of innovation

Client Server Behavior


Classes behave toward each other as client/server
systems.
Classes normally play both roles.
Responsibilities between the classes must be established.

Data

Client

Run

CALL METHOD server->Do_it


Do_it

Data

Server

46

Global network
of innovation

The Delegation Principle

lcl _tank

lcl _airplane
lcl _client

tank : lcl _tank

fuel : i
fuel_max : i

get_fuel_level () : re_level

re_level = tank->get_fuel_level ( ).

get_fuel_level () : re_level

re_level = fuel / fuel_max * 100.

47

Global network
of innovation

Delegation : Sequence Diagram


pilot : lcl _client

airbus : lcl _airplane

tank : lcl _tank

1: get_fuel_level ( )
2: get_fuel_level ( )
re_level
re_level

48

Global network
of innovation

Namespaces within a Class


The same namespace for

Attribute names

Method names

Event names

Type names

Constant names

ALIAS names

There is a local namespace within methods

49

Global network
of innovation

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.

50

Saturday, July 16, 2016


Date

51
Author/Filename

Global network
of innovation

Questions ?

Copyright Siemens Business Services

Você também pode gostar