Вы находитесь на странице: 1из 439

%& $%$3 2EMHFWV

%&
SAP AG 2002

$%$3 2EMHFWV

n n n n

SAP R/3 System requirements: SAP R/3, Basis Release 6.10 or later 2002/Q3 Material number 5005 4667

&RS\ULJKW

1R SDUW RI WKLV SXEOLFDWLRQ PD\ EH UHSURGXFHG RU WUDQVPLWWHG LQ DQ\ IRUP RU IRU DQ\ SXUSRVH ZLWKRXW WKH H[SUHVV SHUPLVVLRQ RI 6$3 $* 7KH LQIRUPDWLRQ FRQWDLQHG KHUHLQ PD\ EH FKDQJHG ZLWKRXW SULRU QRWLFH

&RS\ULJKW  6$3 $* $OO ULJKWV UHVHUYHG

SAP AG 2002

1RWHV RQ 7UDGHPDUNV
n n n n n n n n n n

Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, WINDOWS, NT, EXCEL, Word, PowerPoint, and SQL Server are registered trademarks of Microsoft Corporation. IBM, DB2, OS/2, DB2/6000, Parallel Sysplex, MVS/ESA, RS/6000, AIX, S/390, AS/400, OS/390, and OS/400 are registered trademarks of IBM Corporation. ORACLE is a registered trademark of ORACLE Corporation. INFORMIX-OnLine for SAP and INFORMIX Dynamic ServerTM are registered trademarks of Informix Software Incorporated. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. HTML, DHTML, XML, and XHTML are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. JAVA is a registered trademark of Sun Microsystems, Inc. JAVASCRIPT is a registered trademark of Sun Microsystems, Inc., used under license for technology developed and implemented by Netscape. SAP, SAP Logo, R/2, RIVA, R/3, ABAP, SAP ArchiveLink, SAP Business Workflow, WebFlow, SAP EarlyWatch, BAPI, SAPPHIRE, Management Cockpit, mySAP.com Logo, and mySAP.com are trademarks or registered trademarks of SAP AG in Germany and several other countries all over the world. All other products mentioned are trademarks or registered trademarks of their respective companies.

&RXUVH 3UHUHTXLVLWHV

l 3URJUDPPLQJ H[SHULHQFH

l 6$37(& P\6$3 7HFKQRORJ\ 6ROXWLRQ 2YHUYLHZ

l %& $%$3 :RUNEHQFK )RXQGDWLRQV DQG &RQFHSWV l 3URJUDPPLQJ H[SHULHQFH LQ $%$3

SAP AG 2002

7DUJHW *URXS

l 3DUWLFLSDQWV 'HYHORSHUV FRQVXOWDQWV l 'XUDWLRQ  GD\V

SAP AG 2002

1RWHV WR WKH XVHU


n

The training materials are QRW DSSURSULDWH IRU VHOIVWXG\. 7KH\ FRPSOHPHQW WKH FRXUVH LQVWUXFWRU
V H[SODQDWLRQV. There is space for you to write down additional information on the sheets.

There may not be enough time during the course to complete all the exercises. The exercises are intended as additional examples of the topics discussed during the course. Participants can also use them as an aid to enhancing their knowledge after the course has finished.

&RXUVH 2YHUYLHZ &RQWHQWV


l &RXUVH JRDO l &RXUVH REMHFWLYHV l &RXUVH FRQWHQW l &RXUVH RYHUYLHZ GLDJUDP l 0DLQ EXVLQHVV VFHQDULR

SAP AG 2002

SAP AG

BC401

1-1

&RXUVH *RDO

l /HDUQ WKH SULQFLSOHV RI REMHFWRULHQWHG l /HDUQ WKH VWUXFWXUH DQG DSSOLFDWLRQ RI

7KLV FRXUVH ZLOO HQDEOH \RX WR

SURJUDPPLQJ

$%$3 2EMHFWV

SAP AG 2002

SAP AG

BC401

1-2

&RXUVH 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV FRXUVH \RX ZLOO EH DEOH WR


l 'HVFULEH DQG XVH WKH PRVW LPSRUWDQW SULQFLSOHV n ,QWHUQDO WDEOHV n &ODVVHVLQKHULWDQFHLQWHUIDFHV n (YHQWV

n 3RO\PRUSKLVP LQKHULWDQFH DQG LQWHUIDFHV n ([FHSWLRQ KDQGOLQJ

l 'HYHORS SURJUDPV LQ $%$3 2EMHFWV

n '\QDPLF SURJUDPPLQJ

SAP AG 2002

SAP AG

BC401

1-3

&RXUVH &RQWHQW 
3UHIDFH Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Unit 8 Unit 9 Unit 10
SAP AG 2002

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD $QDO\VLV DQG 'HVLJQ

8VLQJ ,QWHUQDO 7DEOHV

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO

&RXUVH 2YHUYLHZ

,QWURGXFWLRQ WR 2EMHFW2ULHQWHG 3URJUDPPLQJ 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ

,QWHUIDFHV

&DVWLQJ

,QKHULWDQFH

SAP AG

BC401

1-4

&RXUVH &RQWHQW 

Unit 11 Unit 12 Unit 13 Unit 14 Unit 15

(YHQWV

*OREDO &ODVVHV DQG ,QWHUIDFHV 6SHFLDO 7HFKQLTXHV ([FHSWLRQ +DQGOLQJ

'\QDPLF 3URJUDPPLQJ $SSHQGL[

SAP AG 2002

SAP AG

BC401

1-5

&RXUVH 2YHUYLHZ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

SAP AG 2002

SAP AG

BC401

      
1-6

0DLQ %XVLQHVV 6FHQDULR

l $ WUDYHO DJHQW PDLQWDLQV LWV FRQQHFWLRQV WR

EXVLQHVV SDUWQHUV  VXFK DV DLUOLQHV FDU KLUH FRPSDQLHV DQG KRWHOV

SAP AG 2002

SAP AG

BC401

1-7

7HFKQLFDO 1DPHV

l 3DFNDJH %&

l 1DPLQJ FRQYHQWLRQV IRU SURJUDP REMHFWV n 'HPRQVWUDWLRQV n &RS\ WHPSODWHV n 0RGHO VROXWLRQV

6$3%&B[[['B 6$3%&B[[[7B 6$3%&B[[[6B

[[[ LV WKH LQGLYLGXDO XQLW FRGH

SAP AG 2002

Unit codes: Unit 2: Unit 3: Unit 4: Unit 7: Unit 8: Unit 9: Unit 10: Unit 11: Unit 12: Unit 13: Unit 14: Unit 15:

DTO TAB CAL AIR INH CAS INT EVE CLS SPC EXC DYN

SAP AG

BC401

1-8

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO &RQWHQWV


l 'DWD REMHFWV LQ SURJUDPV l (OHPHQWDU\ GDWD REMHFWV l 6WUXFWXUHV

l &KDUDFWHU VWULQJ SURFHVVLQJ LQ 8QLFRGH

SAP AG 2002

SAP AG

BC401

2-1

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 8VH HOHPHQWDU\ GDWD REMHFWV DQG VWUXFWXUHV DSSURSULDWHO\ l 8VH QXPHULF GDWD W\SHV DSSURSULDWHO\ l 8VH DXWRPDWLF W\SH FRQYHUVLRQV DSSURSULDWHO\ l 7DNH WKH VSHFLDO IHDWXUHV RI FKDUDFWHU VWULQJ SURFHVVLQJ LQ 8QLFRGH LQWR DFFRXQW

SAP AG 2002

SAP AG

BC401

2-2

B6 38 2 6 9353%)0( A @ 8 7 6 42 1

&' &'   & & $       $%#"!!      

SAP AG
SAP AG 2002

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

2-3

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 

,QWURGXFWLRQ ,QWURGXFWLRQ

(OHPHQWDU\ 'DWD 2EMHFWV (OHPHQWDU\ 'DWD 2EMHFWV


6WUXFWXUHV 6WUXFWXUHV

6SHFLDO )HDWXUHV LQ 8QLFRGH 6SHFLDO )HDWXUHV LQ 8QLFRGH

SAP AG 2002

SAP AG

BC401

2-4

5HYLVLRQ 'HILQLQJ 'DWD 2EMHFWV


/RFDO W\SHV
7<3(6 W\SHBQDPH 7<3( 

3UHGHILQHG $%$3 W\SHV


G W L

'$7$ GRBQDPH 7<3( W\SHBQDPH

S I Q VWULQJ F [VWULQJ [

*OREDO W\SHV

'$7$ GRBQDPHBQHZ /,.( GRBQDPH


SAP AG 2002

Data objects are usually defined with the '$7$ statement as follows. After the name of the data object, a a fully-specified type is assigned to it using the 7<3( addition. The type is linked to the data object statically and cannot be changed at runtime. There are other syntax variants available (for historical reasons). Note however, that some of these historical variants are no longer supported in ABAP Objects. For further information, refer to the keyword documentation for the '$7$ statement. All types are based on predefined ABAP types, which will be discussed in greater detail in the following slide. Some of these ABAP types are fully specified and can be used to type data objects directly. Other types need to be modified to include the length, and in some cases the number of decimal places so that they are fully specified. These are the simplest examples of user-defined types. You can create complex structured types of any level of complexity, based on elementary types. You can also define types centrally in the $%$3 'LFWLRQDU\ You can then use these JOREDO W\SHV in all appropriate Repository objects in the system.

n n

SAP AG

BC401

2-5

3UHGHILQHG $%$3 7\SHV


'DWD W\SH 1XPHULF
'HVFULSWLRQ ,QWHJHU /HQJWK LQ E\WHV    
$WWULEXWHV

)L[HG OHQJWK

L I S

'LIIHU LQ

3DFNHG QXPEHU

)ORDW SRLQW QXPEHU 

9DOXH UDQJH

5XOHV IRU VWRUDJH $ULWKPHWLF XVHG

&KDUDFWHU VWULQJ W\SH

Q F G W [

1XPEHU VHTXHQFH

&KDUDFWHU VHTXHQFH    'DWH  7LPH 

  

&KDUDFWHU VWULQJ RSHUDWLRQV DOORZHG IRU DOO W\SHV  GDWH FDOFXODWLRQV  WLPH FDOFXODWLRQV

+H[DGHFLPDO

+H[DGHFLPDO FRGH

  

%LW RSHUDWLRQV
5XQWLPH V\VWHP DGMXVWV OHQJWK G\QDPLFDOO\

9DULDEOH OHQJWK

[VWULQJ

VWULQJ

&KDUDFWHU VWULQJ W\SH  KH[DGHFLPDO &KDUDFWHU VHTXHQFH +H[DGHFLPDO FRGH

SAP AG 2002

The following ABAP types are fully specified and can be used directly to type data objects: G, W, L, I, VWULQJ, and [VWULQJ. A special feature of the VWULQJ and [VWULQJ types is that their length is adjusted dynamically at runtime. Thus, for these two types, length is not a component of the type. The following ABAP types are not fully specified, so you must include additional information before you use them to define data objects: F, Q, and [ Length needs to be added. Permitted values:  to  characters. If you do not declare a length when you define a data object, the system assigns the default length of .

S You must extend the type to include the length, and number of decimal places, Permitted length:  to  bytes. If you do not declare a length when you define a data object, the system assigns the default length of  bytes (that is, 15 digits) and  decimal places.

The type also contains information on how the data is stored, what range of values is allowed, what operations can be performed on the data, and what sort of arithmetic is used (if the data object is suitable for use in calculations). We will deal with this topic in greater detail later in this unit.

SAP AG

BC401

2-6

7\SH *URXSV LQ WKH $%$3 'LFWLRQDU\


1DPH RI W\SH JURXS 8S WR  FKDUDFWHUV HDFK W\SH QDPH DQG FRQVWDQW QDPH VWDUWV ZLWK WKH QDPH RI WKH W\SH JURXS
(DFK W\SH JURXS ZKRVH W\SHV DQG FRQVWDQWV \RX ZDQW WR XVH PXVW EH VSHFLILHG LQ WKH $%$3 SURJUDP

7\SH JURXS ]P\WS


7<3(322/ ]P\WS ]P\WS &2167$176 ]P\WS ]P\WSBFRQVWBQDPH  > 7<3(6 ]P\WS ]P\WSBW\SHBQDPH  @ > 7<3(6 ]P\WS ]P\WSBW\SHBQDPH  @ 

8VLQJ D FRQVWDQW
$%$3 SURJUDP

7<3(322/6 ]P\WS 7<3(322/6 ]P\WS > '$7$ YDU 7<3( ]P\WS ]P\WSBW\SH @ ,) YDU  (1',)
SAP AG 2002

]P\WS ]P\WSBFRQVWBQDPH

n n

You define constants in the type group using the &2167$176 statement, just as in an ABAP program. You only have to adhere to the following namespace convention: All constants (and type names) must have the name of the type group as a prefix. You can use either a global Dictionary type or a predefined ABAP type as a valid type.

You must use a type group to define global constants. The name of the type group can contain up to 5 characters.

To be able to use the types of a type group in a program, you must refer to the type group using the 7<3(322/6 statement. After this line in the program, you can then use all the constants in the type group. You can also define global data types in a type group. Before SAP R/3 Basis Release 4.5, this was the only way to define complex global data types.

SAP AG

BC401

2-7

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 

,QWURGXFWLRQ ,QWURGXFWLRQ

(OHPHQWDU\ 'DWD 2EMHFWV (OHPHQWDU\ 'DWD 2EMHFWV


6WUXFWXUHV 6WUXFWXUHV

6SHFLDO )HDWXUHV LQ 8QLFRGH 6SHFLDO )HDWXUHV LQ 8QLFRGH

SAP AG 2002

SAP AG

BC401

2-8

,QWHJHUV DQG ,QWHJHU $ULWKPHWLF


/HQJWK  E\WHV

'$7$ FRXQWHU 7<3( L 9$/8( 

l l l

9DOXH UDQJH > @ ,QWHJHU $ULWKPHWLF

LQW

LQW  LQW  LQW

$OO LQWHUQDO DX[LOLDU\ ILHOGV DUH LQWHJHUV ,QWHULP UHVXOWV DUH URXQGHG

LQW
LQW

LQW




 


LQW



$ULWKPHWLF RSHUDWLRQV IRU LQWHJHUV    ',9 02'

,QWHJHU DX[LOLDU\ YDULDEOH

$GGLWLRQ 6XEWUDFWLRQ 0XOWLSOLFDWLRQ 'LYLVLRQ ,QWHJHU GLYLVLRQ 5HPDLQGHU LQWHJHU GLY 3RZHU

SAP AG 2002

In integer arithmetic, the system always rounds to the appropriate decimal place. So, for example: '$7$ LQW 7<3( L LQW    " result: 0 LQW    " result: 1 Calculations performed using integer arithmetic are faster than calculations using fixed point or floating point arithmetic. For further information on calculations performed on integers, refer to the keyword documentation for the &20387( statement.

n n

SAP AG

BC401

2-9

3DFNHG 1XPEHUV DQG )L[HG 3RLQW $ULWKPHWLF


'$7$ SHUFHQWDJH  7<3( S '(&,0$/6  9$/8(

 
/HQJWK LQ E\WHV Q E\WHV FRUUHVSRQG WR Q   GLJLWV
1XPEHU RI GHFLPDO SODFHV
)L[HG SRLQW DULWKPHWLF OLNH XVLQJ SDSHU DQG SHQFLO

1XPEHU 

,QWHUQDO UHSUHVHQWDWLRQ



 

     

SAP AG 2002

The number of decimal places is defined using the '(&,0$/6 addition. The maximum number of decimal places is either 15 or the length of the number minus 1 (that is, there must be at least one digit before the comma). In user dialogs, decimal numbers are formatted according to the settings in the user master record. Decimal point-aligned fixed point arithmetic is used as default for calculations. Packed numbers are thus well-suited to business calculations, where the correct rounding for the specified number of decimal places is very important. The algorithm for this arithmetic is similar to using "pencil and paper". You can switch off fixed point arithmetic in the program attributes. If you do, the '(&,0$/6 addition when defining a packed number only affects the output of the number. Internally, all numbers are interpreted as integers, regardless of the position of the decimal point. The fixed point arithmetic option is always selected by default. You should DOZD\V accept this value and use packed numbers for business calculations. The system always uses packed numbers of maximum length for interim results.

The length of packed numbers is given in bytes. The connection between value range and length is derived from the internal representation: Each decimal digit is represented by a half-byte. The last byte is reserved for the plus or minus sign.

n n

n n

SAP AG

i pH F h

TQgf#Qb`W P e dcaY X VSUSQIH GEC T R PF F D

l 3HUPLWWHG OHQJWK  WR  E\WHV  WR  GLJLWV l ([DPSOH /HQJWK  E\WHV  GHFLPDO SODFHV 9DOXH UDQJH >@

l 1XPEHU RI GHFLPDO SODFHV  QXPEHU RI GLJLWV PD[ 

BC401

2-10

)ORDWLQJ 3RLQW $ULWKPHWLF


_
_
-

1,5 = 120 + 12 1 1 =1 + 2
_   _
-3 -6

2QO\  ELWV DYDLODEOH

_   _
-7 -10

_ 
-11

0,15 = 12 + 12 + 12 + 12 + 12 + 1 1 1 1 1 = + + + + + 8 128 1024 2048 64


= 0,125 + = = = =
SAP AG 2002

2QO\ IRU DSSUR[LPDWLRQV

0,140625 + 0,1484375 + 0,1494140625 + 01499023437

Unlike packed numbers, floating point numbers are represented using sums of binary fractions. Floating point numbers are also normalized, and both the exponent and the mantissa are stored in binary form. This representation complies with the IEEE norm for double precision floating point numbers. The floating point operations of the relevant processors are used for calculations. Since algorithms are converted to binary, inaccuracies can occur. Therefore, floating point numbers are QRW suitable for business calculations. ([DPSOH You want to calculate 7.72% of 73050 and display the result accurate to two decimal places. The answer should be 5310.74 (73050 * 0.0727 = 5310.7735). The program, however: '$7$ IORDW 7<3( I SDFN 7<3( S '(&,0$/6  IORDW 

 UHVXOW ( SDFN IORDW :5,7( SDFN UHVXOW 

n n

The DGYDQWDJH RI IORDWLQJ SRLQW QXPEHUV is the large value range: It comprises numbers from is, from 2,2250738585072014E-308 to 1,7976931348623157E+308 including both positive and negative numbers as well as zero. In addition, you must use floating point numbers for special aggregation functions of the 6(/(&7 statement.

<RX VKRXOG WKHUHIRUH RQO\ XVH IORDWLQJ SRLQW QXPEHUV IRU DSSUR[LPDWLRQV :KHQ \RX FRPSDUH QXPEHUV DOZD\V XVH LQWHUYDOV DQG DOZD\V URXQG DW WKH HQG RI \RXU FDOFXODWLRQV

SAP AG

BC401

2-11

6XPPDU\ 5HFRPPHQGDWLRQV IRU 8VLQJ 1XPHULF 'DWD 7\SHV

5HTXLUHG
,QWHJHUV RQO\

5HFRPPHQGHG SUHGHILQHG $%$3 GDWD W\SH


7\SH L VLQFH FDOFXODWLRQV XVLQJ LQWHJHU DULWKPHWLF DUH IDVWHVW

'HFLPDO QXPEHUV IRU EXVLQHVV FDOFXODWLRQV


'HFLPDO QXPEHUV IRU URXJK FDOFXODWLRQV SHUIRUPHG RQ YHU\ VPDOO RU YHU\ ODUJH QXPEHUV

7\SH S

7\SH )

SAP AG 2002

1RWH The results of the following functions have the data type I: Hyperbolic functions: WDQK, VLQK, FRVK Trigonometric functions: FRV, DFRV, VLQ, DVLQ, WDQ, DWDQ

Square root: VTUW

Logarithms (base 10): ORJ

Natural logarithms (base e): ORJ

Exponential functions (base e): H[S

SAP AG

BC401

2-12

$ULWKPHWLF DQG 0L[HG ([SUHVVLRQV


l )L[HG SRLQW DULWKPHWLF LV WKH GHIDXOW

l ,QWHJHU DULWKPHWLF LV RQO\ XVHG LI DOO WKH FRPSRQHQWV DUH LQWHJHUV


l $OO FRPSRQHQWV DUH FRQYHUWHG WR WKH UHOHYDQW GDWD W\SH

l )ORDWLQJ SRLQW DULWKPHWLF LV XVHG LI DW OHDVW RQH FRPSRQHQW LV D IORDWLQJ SRLQW QXPEHU
l $IWHU WKH FDOFXODWLRQ DOO DUH FRQYHUWHG WR WKH UHVXOW W\SH

([DPSOH
S '(&,0$/6  U L




E





)ORDW SRLQW DULWKPHWLF


&RQYHUVLRQ DIWHU I DQG FDOFXODWLRQ



(
(

( 

SAP AG 2002

,QWHULP UHVXOW

&RQYHUVLRQ DIWHU S '(&,0$/6 

(

n n

The system converts all the values into one of the three numeric data types (L, S, or I), depending on the data types of the operands. The ABAP runtime system contains an arithmetic for each of the three data types. The system then performs the calculation and converts it into the data type of the result field. This may mean that the same arithmetic expression leads to different results when performed on different combinations of data types. It is also possible for an arithmetic expression to have RQO\ FKDUDFWHU VWULQJ W\SH data objects, as long as their contents are appropriate. The values are converted to numeric type objects. Bear in mind that conversions affect performance. Wherever possible, choose a data type that does QRW require runtime conversion. If an arithmetic expression contains numeric literals, the choice of arithmetic depends on the size of the number: If the number is within the value range for the data type L, the numeric literal is interpreted as an integer. If the value of the literal is greater than 2147483647, it is interpreted as a packed number. ([DPSOH '$7$ LQW 7<3( L LQW     "result: 9 LQW     "result: 10

An arithmetic expression may contain any data types that are convertible into each other and into the type of the result field.

n n

SAP AG

BC401

2-13

3UHGHILQHG $%$3 7\SHV IRU &KDUDFWHU 6WULQJV

'HVFULSWLRQ
/HQJWK
9DOXH UDQJH
&DOFXODWLRQV
)RUPDWWLQJ RSWLRQV

7\SH W 7LPH

7\SH G 'DWH

7\SH Q

7\SH F

6HTXHQFH )L[HGOHQJWK RI GLJLWV FKDU VWULQJ

7\SH VWULQJ
&KDU VWULQJ RI YDULDEOH OHQJWK

 GLJLWV ++0066
%\ FORFN
7LPH DULWKPHWLF
++0066

 GLJLWV    <<<<00'' FKDUDFWHUV


%\ *UHJRULDQ FDOHQGDU
'DWH DULWKPHWLF
%DVHG RQ XVHU GHIDXOW YDOXHV 'LJLWV
&RQYHUVLRQ

   FKDUDFWHUV

9DULDEOH

'HSHQGV RQ FRGHSDJH
&RQYHUVLRQ
&RQYHUVLRQ

SAP AG 2002

The value range of each string depends on the code page, which contains all the supported characters in form of a table. Internally, each character is represented by a code number. When the system outputs the character, it uses the code page to convert this number. To find the code page valid in a given system, choose 7RROV &&06 6SRRO $GPLQLVWUDWLRQ )XOO $GPLQLVWUDWLRQ &KDUDFWHU 6HWV. The initial value of each character string with fixed length is a space character. Numeric strings are represented internally as character strings. Note, however, that only digits are permissible as characters. When character strings are assigned to numeric strings, the system ignores letters and only copies the digits (right-aligned). Missing characters are filled with zeros. Only sequences of digits are valid for values of type G. These digits form a meaningful date, which complies with the Gregorian calendar. The first four digits represent the year, the next two the month and the last two the date. For performance reasons however, the object is only checked if it is an input field on a screen or selection screen. For values of type W, a sequence of digits is only valid if it can be interpreted as a time on the 24-hour clock. The rules for interpreting the value are analogous to those used for dates. The initial value of a date is

. The formatting options are determined by the user settings. The initial value of each character in a numeric string is a zero.

n n

n n

n n n

The initial value of a time is



.

SAP AG

BC401

2-14

2YHUYLHZ &KDUDFWHU 6WULQJ 3URFHVVLQJ


6HDUFK LQ D FKDUDFWHU VWULQJ 3RVLWLRQ RI VHDUFK VWULQJ XVLQJ 0$7&+ 2))6(7 RII DGGLWLRQ
5HSODFH ILUVW RFFXUUHQFH
5HSODFH DOO RFFXUUHQFHV

'HVFULSWLRQ DQG QRWHV

),1'

5(3/$&(
75$16/$7(
6+,)7
&21'(16(
29(5/$<

$%$3

$%$3

"

$%$3 $3 $% 3 $$$$

&21&$7(1$7(

$ % $ 3
$%&$3

63/,7

SAP AG 2002

Note for ),1' statement (search in a character string): There are special comparison operators for strings, which you can use in logical expressions in a query (,)) to search more flexibly for character sequences in a character string. For more information, see the keyword documentation for ,).

n n n n n

For every statement, the operands are treated like type F fields, regardless of their actual field type. No internal type conversions take place. All of the statements apart from 75$16/$7( and &21'(16( set the system field V\VXEUF. (6($5&+ also sets the system field V\IGSRV with the offset of the character string found.) To find out the occupied length of a string, use the standard function 675/(1  All of the statements apart from 6($5&+ are case-sensitive.

For the 63/,7 statement there is the variant 63/,7  ,172 7$%/( <itab>, which you can use to split the character string dynamically. You do not need to specify the number of parts into which the string should be split.

SAP AG

g`y9x fvusq t y w t r

$%$3

D E DS %$3 $3

%%$3

0RYH

5HPRYH VSDFH FKDUDFWHUV 2YHUZULWH  6SDFHV DUH RYHUZULWWHQ E\ FKDUDFWHUV IURP WKH VHFRQG FKDUDFWHU VWULQJ &RQFDWHQDWH VHYHUDO FKDUDFWHU VWULQJV

$%$3 $%$3 $% $3

6SOLW D FKDUDFWHU VWULQJ

BC401

2-15

$FFHVVLQJ 3DUWV RI )LHOGV


VWDWHPHQW! ILHOG!RII! OHQ! 
3RVVLEOH ZLWK DQ\ FKDUDFWHUW\SH ILHOG

5(3257  3$5$0(7(56 SDBVWU  /2:(5 &$6( SDBSRV 7<3( L SDBOHQ 7<3( L

SAP AG 2002

In DQ\ statement that operates on a FKDUDFWHUW\SH field, you can address part of the field or structure by specifying a starting position and a number of characters. If the field lengths are different, the system either truncates the target or fills it with initial values. The source and target fields must have the type [, F, Q, G, W, or 675,1* You can also use structures. ([DPSOH 029( <field1>+<off1> <len1> 72 <field2>+<off2> <len2>  This statements assigns len1> characters of field <field1> starting at offset <off1> to <len2> characters of <field2> starting at offset <off2>.

SAP AG

BC401

G``ssQ#`S`s
G```G
2-16

:5,7( SDBVWUSDBSRV SDBOHQ  SDBSRV SDBOHQ SDB SRV SDB OHQ

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 

,QWURGXFWLRQ ,QWURGXFWLRQ

(OHPHQWDU\ 'DWD 2EMHFWV (OHPHQWDU\ 'DWD 2EMHFWV


6WUXFWXUHV 6WUXFWXUHV

6SHFLDO )HDWXUHV LQ 8QLFRGH 6SHFLDO )HDWXUHV LQ 8QLFRGH

SAP AG 2002

SAP AG

BC401

2-17

'HILQLQJ 6WUXFWXUHV ZLWK /RFDO 7\SHV


7<3(6 7<3(6 %(*,1 2) VBQDPHBW\SH VBQDPHBW\SH SUHQDPH  7<3( F VXUQDPH  7<3( F WLWOH  7<3( F (1' 2) VBQDPHBW\SH
'HILQLWLRQ RI D VWUXFWXUH W\SH

'HILQLWLRQ RI D VWUXFWXUH ZLWK LPSOLFLW W\SH FRQVWUXFWLRQ

'$7$ VBQDPH 7<3( VBQDPHBW\SH 


$OWHUQDWLYHV

'HILQLWLRQ RI D VWUXFWXUH ZLWK UHIHUHQFH WR D XVHUGHILQHG W\SH

'$7$ %(*,1 2) VBQDPH VBQDPH SUHQDPH  7<3( F VXUQDPH  7<3( F WLWOH  7<3( F VBQDPH (1' 2) VBQDPH

67$572)6(/(&7,21 VBQDPHSUHQDPH
6PLWK
 VBQDPH VBQDPHVXUQDPH
-RKQ
 VBQDPH
SAP AG 2002

As with elementary data objects, you can define structures in two ways:

Define the data object directly using the '$7$ statement. The syntax is similar to the definition of a structure type. If you use this option, the corresponding structure type is defined implicitly at the same time.
n n

First, define a structure type explicitly using the 7<3(6 statement. To do this, enter the name of the structure after %(*,1 2) and then list the definitions of all the components. End the definition of the structure type using (1' 2). You then define the structured data object with the '$7$ statement, using your own user-defined structure type.

In both cases, the type is defined locally. Bear in mind that you can also use globally defined types instead. You address components of structures using: structure_namecomp_name. For this reason, you should avoid using hyphens in variable names.

SAP AG

BC401

2-18

'HILQLQJ 1HVWHG 6WUXFWXUHV ZLWK /RFDO 7\SHV


7<3(6 %(*,1 2) VBQDPHBW\SH VBQDPHBW\SH SUHQDPH  7<3( F VXUQDPH  7<3( F WLWOH  7<3( F (1' 2) VBQDPHBW\SH '$7$ %(*,1 2) VBDGGUHVV QDPH 7<3( VBQDPHBW\SH VBQDPHBW\SH VWUHHW  7<3( F FLW\  7<3( F (1' 2) VBDGGUHVV
VBDGGUHVV 6WUXFWXUH W\SH
VBQDPHBW\SH SUHQDPH VXUQDPH WLWOH

1HVWHG VWUXFWXUH
QDPH SUHQDPH VXUQDPH WLWOH

VWUHHW FLW\

VBDGGUHVVQDPHVXUQDPH   VBDGGUHVVFLW\ 
SAP AG 2002

6PLWK


/RQGRQ


$SSOLFDWLRQ /RJLFDO VXEJURXSLQJ RI GDWD

n n n n n

You can define nested structures by assigning a structure type to a component ZLWKLQ a structure type. You can address this substructure as a whole using the component name: structure_namesubstructure_name. You can also address individual components in the VXEstructure: structure_name-substructure_name-comp_name. Structures can be nested to any level you wish. You can also integrate components of a dynamic type in a structure. This can either be an elementary data object of variable length (VWULQJ or [VWULQJ),an internal table, or a reference. These structures are known as GHHS structures. There are constraints on how such deep structures can be used. For instance, a deep structure cannot be used DV D ZKROH in the ,172 clause of the 6(/(&7 statement. (Instead, each component must be listed separately). Offset operations are also not appropriate. For more information, refer to SAP Note 176336.

SAP AG

BC401

2-19

8VLQJ 1DPHG ,QFOXGHV


'$7$ %(*,1 2) WBOLQHW\SH VBQDPHBW\SH SUHQDPH  7<3( F VXUQDPH  7<3( F WLWOH  7<3( F (1' 2) VBQDPHBW\SH '$7$ %(*,1 2) VBDGGUHVV ,1&/8'( 6758&785( VBQDPHBW\SH ,1&/8'( 6758&785( $6 QDPH '$7$ VWUHHW  7<3( F FLW\  7<3( F (1' 2) VBDGGUHVV VBDGGUHVVQDPHVXUQDPH
6PLWK
   RU VBDGGUHVVVXUQDPH
6PLWK
 
SAP AG 2002

6WUXFWXUH W\SH
VBQDPHBW\SH SUHQDPH VXUQDPH WLWOH

6WUXFWXUH ZLWK QDPHG LQFOXGH


VBDGGUHVV SUHQDPH VXUQDPH WLWOH VWUHHW FLW\

QDPH

8VH /RJLFDO VXEJURXS GHVLUHG EXW QHVWHG VWUXFWXUHV QRW WHFKQLFDOO\ SRVVLEOH

In some cases, you cannot use nested structures, for example: Because you can only define database tables with flat line types In situations where user dialogs with a table-type display can only contain simple structures (such as in the SAP List Viewer or Table Control)

In situations like these, you may still want to group parts of the structure and address it as a whole. You do this using named includes: You can include a substructure in another structure, and give this substructure a name: '$7$ %(*,1 2) structure_name   ,1&/8'( 6758&785( substructure_type $6 name '$7$   (1' 2) structure_name

You can address this substructure using structure_name-name. You can address the fields in the substructure using structure_name-name-comp_name or directly using structure_name-comp_name. Technically, however, this structure is QRW nested. If naming conflicts occur - for example, if you want to include the same substructure twice - you can append another name to the component names using 5(1$0,1*. For further information, refer to the keyword documentation for the ,1&/8'( 6758&785( statement.

SAP AG

BC401

2-20

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 

,QWURGXFWLRQ ,QWURGXFWLRQ

(OHPHQWDU\ 'DWD 2EMHFWV (OHPHQWDU\ 'DWD 2EMHFWV


6WUXFWXUHV 6WUXFWXUHV

6SHFLDO )HDWXUHV LQ 8QLFRGH 6SHFLDO )HDWXUHV LQ 8QLFRGH

SAP AG 2002

SAP AG

BC401

2-21

8QLFRGH 2YHUYLHZ
3UHYLRXV SUREOHPV
l l

0L[WXUH RI GLIIHUHQW LQFRPSDWLEOH FKDUDFWHU VHWV LQ RQH FHQWUDO V\VWHP

'DWD H[FKDQJH EHWZHHQ V\VWHPV ZLWK LQFRPSDWLEOH FKDUDFWHU VHWV

D "
SAP AG 2002

D
8QLFRGH

"

"

l 21( FRGHSDJH WKDW FRPSULVHV DOO NQRZQ FKDUDFWHUV

6ROXWLRQ 8QLFRGH

l (YHU\ FKDUDFWHU LV JHQHUDOO\ GLVSOD\HG E\ WZR E\WHV

To be able to work with Unicode, you must have a Unicode-compatible SAP System installed that itself has a corresponding operating system and database. The ABAP programs must also be Unicode-compatible. In Unicode programs, other syntax rules apply than in non-Unicode programs. This is due to the difference between the length in bytes and the number of characters in a character set in Unicode. Existing programs are affected by a conversion to Unicode if an explicit or implicit assumption is made about the internal length of a character. To execute the relevant syntax checks, you must check 8QLFRGH &KHFNV $FWLYH under program attributes. In a Unicode system, you can only execute programs that have the Unicode flag set. If the Unicode flag is set for a program, the syntax check and program are executed in accordance with the rules described in the Unicode online help (irrespective of whether it is a Unicode or a non-Unicode system). If the Unicode flag is not set, the program can only be executed in a non-Unicode system. For such programs, the Unicode-specific changes of syntax and semantics do not apply. However, you can use all language enhancements introduced in connection with the conversion to Unicode.

SAP AG

BC401

2-22

&KDUDFWHU7\SH DQG %\WH7\SH 7\SHV


&KDUDFWHUW\SH W\SHV LQ 8QLFRGH SURJUDPV
2QO\ FRPSRQHQWV ZLWK WKH W\SHV c n d DQG t

G F W Q

E\WHW\SH

VWULQJ

&KDUDFWHUW\SH VWUXFWXUH W\SHV

[VWULQJ

'LIIHUHQFH EHWZHHQ E\WH SURFHVVLQJ DQG FKDUDFWHU SURFHVVLQJ


'$7$ FBILHOG  7<3( F 9$/8(
+8*2
 [BILHOG  7<3( [ 9$/8(
(%$
 6+,)7 FBILHOG %<  3/$&(6 >,1 &+$5$&7(5 02'(@ 6+,)7 [BILHOG %<  3/$&(6 ,1 %<7( 02'(
SAP AG 2002

In Unicode programs, the following data types are interpreted as character-type: F, Q, G, W, VWULQJ as well as structure types, that directly or in substructures only contain components with types F, Q , G and W. In non-Unicode systems, a character of this type is one byte and in Unicode systems, it is as long as corresponds with the length of a character on the relevant platform. Variables of the types [ and [VWULQJ are described as byte-type. The character string processing (for which, previously, all arguments were implicitly interpreted as type F fields) is separated into string processing for character-type and byte-type arguments.

n n

In the case of the string processing for character-type arguments, the single fields must be type F, Q, G, W, or VWULQJ; character-type structures are also permitted. If arguments of another type are transferred, this triggers a syntax or runtime error.

The ; variants of the string statement are distinguished from the character string commands by the ,1 %<7( 02'( addition. For the character string variants, ,1 &+$5$&7(5 02'( is an optional addition. If the ,1 %<7( 02'( addition is specified, only ; fields and ; strings are permitted as arguments. If arguments of another type are transferred, this triggers a syntax or runtime error.

SAP AG

BC401

z yv w vu IGx00`t
||||z y 0%%%{evx
2-23

j Sh glk i f d

~d d

rsqnom p j h #e i gf d
r}p

)XQFWLRQV DQG &RPSDULVRQ 2SHUDWRUV


/HQJWK LQ FKDUDFWHUV /HQJWK LQ E\WHV

&KDUDFWHUW\SH W\SH 675/(1 675/(1


&2 &$ &6 &3

%\WHW\SH W\SH

)XQFWLRQV

;675/(1
%<7(&2 %<7(&$ %<7(1$ %<7(16

&RPSDULVRQ RSHUDWRUV

&1 1$ 16 13

%<7(&6 %<7(&1

([DPSOHV

SAP AG 2002

The ; variants of the string comparison operators are distinguished from the character string variants by the %<7(- prefix. For this operation, only ; fields and fields of the type [VWULQJ are allowed as arguments. The 675/(1 function only works with character-type fields and returns the length in characters. With F fields, only the so-called occupied length is relevant, that is, unlike with strings, trailing blanks are not counted.

The operators &2, &1, &$, 1$, &6, 16, &3, 13 are available for comparing the contents of character string type data objects (for syntax and semantics, see keyword documentation). As with the statements for string processing, these operators require single fields of type F Q, G, W, or VWULQJ as arguments. Again, character-type structures are also permitted.

The ;675/(1 function is available for the length of byte sequences. For ; strings, ;675/(1 returns the current length and for ; fields, the defined length in bytes, where null bytes at the end of fields are counted.

SAP AG

BC401

`uG fU9fp ps{f  GpB`I`p`IG susgU9ffgfsfs


2-24

GBgsf`ugep`p{S fggfgsf`uge `p{S` G  s  gsgusgG

&RPSDWLELOLW\ DQG &RQYHUVLRQ


fI
l l
SAP AG 2002

:KHQ DUH WZR W\SHV FRPSDWLEOH" 7ZR HOHPHQWDU\ W\SHV DUH FRPSDWLEOH ZKHQ WKH\ KDYH H[DFWO\ WKH VDPH W\SH DQG OHQJWK DQG LQ WKH FDVH RI SDFNHG QXPEHUV WKH VDPH QXPEHU RI GHFLPDO SODFHV  GHFLPDO SODFHV 7ZR VWUXFWXUH W\SHV DUH FRPSDWLEOH ZKHQ WKH\ KDYH WKH VDPH VWUXFWXUH DQG WKHLU FRPSRQHQWV DUH FRPSDWLEOH  VDPH VWUXFWXUH FRPSRQHQWV DUH 7ZR WDEOH W\SHV DUH FRPSDWLEOH LI WKHLU URZ W\SHV DUH FRPSDWLEOH DQG WKHLU NH\ GHILQLWLRQV DQG WDEOH W\SHV FRPSDWLEOH NH\ GHILQLWLRQV DUH WKH VDPH

,I QRQW\SHFRPSDWLEOH GDWD REMHFWV DUH DVVLJQHG FRQYHUVLRQ GRHV WDNH SODFH LI D FRQYHUVLRQ UXOH LV GHILQHG

1R FRQYHUVLRQ WDNHV SODFH LI W\SHFRPSDWLEOH GDWD REMHFWV DUH DVVLJQHG

n n n

For a full list of all conversion rules, refer to the ABAP syntax documentation for the 029( statement.

If two data types are not compatible but there is a conversion rule, the system converts the source object into the type of the target object when you assign values, perform calculations, or compare values.

If there is no conversion rule defined for a particular assignment, the way in which the system reacts depends on the program context. If the types of the objects involved are defined VWDWLFDOO\, a V\QWD[ HUURU occurs. ([DPSOH: '$7$ GDWH 7<3( G 9$/8(

 WLPH 7<3( W ),(/'6<0%2/6 IVBGDWH! 7<3( G IVBWLPH! 7<3( W $66,*1 GDWH 72 IVBGDWH! WLPH 72 IVBWLPH! IVBWLPH! IVBGDWH!

In the case of G\QDPLF typing a UXQWLPH HUURU occurs, because the field symbols are not assigned types until the assignment of the data objects at runtime.

([DPSOH:  ),(/'6<0%2/6 IVBGDWH! 7<3( $1< IVBWLPH! 7<3( $1<  (Rest as above)

SAP AG

BC401

2-25

&RQYHUVLRQ 8QGHU 8QLFRGH 


&RQYHUVLRQ EHWZHHQ IODW VWUXFWXUHV

&RQYHUVLRQ EHWZHHQ IODW VWUXFWXUHV LV SRVVLEOH LI WKH IUDJPHQWV RI WKH VRXUFH DQG WDUJHW VWUXFWXUHV PDWFK ERWK LQ W\SH DQG OHQJWK LQ WKH OHQJWK RI WKH VKRUWHU VWUXFWXUH
SAP AG 2002

For some data types there are, for technical reasons, specific alignment requirements that depend on the individual platforms. (In the memory, fields of this type must begin and end on specific addresses - for example, a memory address divisible by four.) Within a structure, the runtime system, if necessary, inserts bytes before or after these components with alignment requirements to achieve the alignment needed. These bytes are known as $OLJQPHQW. To check whether the conversion is even permitted, the system first creates the Unicode fragment view of the structures by grouping adjacent components and alignment gaps (one group each for character-type types [F, Q, G, W], byte-type types, and types L, I, and S). Adjacent character-type components of a structure are therefore only grouped together if there are no alignment gaps between these components. Adjacent byte-type components are grouped in the same way. If the fragments of the source and initial structures match the type and length in the length of the shorter structure, the conversion is allowed. If the target structure is longer than the source structure, the character-type components of the remainder are filled with space characters. All other components in the remainder are filled with the type-specific initial value, alignment gaps are filled with null bytes.

n n

SAP AG

BC401

%`

%ug

|w 0Q

$VVLJQPHQW LV SRVVLEOH

Gt" y

'HFLVLYH FULWHULRQ )UDJPHQW YLHZ

u u %g b  0 u Gt" y
Gt" y

8QGHU ZKDW FRQGLWLRQ LV WKLV DVVLJQPHQW SRVVLEOH"

%`
2-26

05

u %b0

G" y t

usffgusff 9{

&RQYHUVLRQ 8QGHU 8QLFRGH 


&RQYHUVLRQ EHWZHHQ VWUXFWXUHV DQG VLQJOH ILHOGV

$VVLJQPHQW LV SRVVLEOH

 &RQYHUVLRQ EHWZHHQ VWUXFWXUHV DQG VLQJOH ILHOGV LV SRVVLEOH LI WKH VWUXFWXUH EHJLQV ZLWK D FKDUDFWHUW\SH IUDJPHQW DQG WKLV IUDJPHQW LV DW OHDVW DV ORQJ DV WKH VLQJOH ILHOG
SAP AG 2002

The following rules apply for the conversion of a structure to a single field and vice versa: If the single field is type F, but the structure isnt completely character-type, the conversion is only allowed if the structure begins with a character-type group and this group is at least as long as the single field. The conversion then takes place between the first character-type group of the structure and the single field. If the structure is the target field, the character-type parts of the remainder are filled with space characters and all other components with the type-specific initial value. The conversion is not allowed if the structure is not purely character-type and the single field is not type F.

Internal tables can be converted if their row type can be converted.

SAP AG

BC401

GB y t

'HFLVLYH FULWHULRQ )UDJPHQW YLHZ

u %g

%f

w Bf

Gt" y

8QGHU ZKDW FRQGLWLRQ LV WKLV DVVLJQPHQW SRVVLEOH"

b v
2-27

w g Bf

pGGffpsg 9{

([DPSOH RI 8VLQJ WKH &RQYHUVLRQ 5XOHV

3$5$0(7(56 SBGDWH /,.( V\GDWXP '$7$ %(*,1 2) VBGDWH \HDU  7<3( Q &KDUDFWHUW\SH PRQWK  7<3( Q VLQJOH ILHOG GD\  7<3( Q (1' 2) VBGDWH
g w

&KDUDFWHUW\SH VWUXFWXUH

l l

6LQFH WKH VWUXFWXUH VBGDWH LV SXUHO\ FKDUDFWHUW\SH LW LV WUHDWHG OLNH D W\SH F GDWD REMHFW GXULQJ FRQYHUVLRQ 7KLV PHDQV LW LV WUDQVIHUUHG OHIWDOLJQHG FKDUDFWHU E\ FKDUDFWHU

SAP AG 2002

VBGDWH is structured in such a way that the first four characters of a variable specify the year, the next two specify the month, and the last two the day. If a date is assigned to this structure using the 029( statement, the characters are then copied left-aligned. You can then determine the year, month, and day directly using the components of the structure, without having to use offsets.

SAP AG

BC401

gSy w
2-28

029( SBGDWH 72 VBGDWH VBGDWHGD\



 029( VBGDWH 72 SBGDWH

2IIVHW DQG /HQJWK $FFHVVHV DQG (OHPHQWDU\ 'DWD 2EMHFWV


$FFHVV WR VLQJOH ILHOGV VSHFLI\LQJ RIIVHW DQG OHQJWK
VWDWHPHQW! ILHOG!RII! OHQ! 

l l

,Q WKH FDVH  E\WHW\SH ILHOGV WKH YDOXHV IRU RIIVHW DQG OHQJWK DUH RI WDNHQ LQ E\WHV

)RU FKDUDFWHUW\SH ILHOGV RIIVHW DQG OHQJWK DUH LQWHUSUHWHG FKDUDFWHU E\ FKDUDFWHU

SAP AG 2002

n n

Offset/length accesses are permitted on character-type single fields, single fields with the type VWULQJ, and single fields of the types [ and [VWULQJ.

For character-type fields and type VWULQJ fields, offset and length are interpreted character by character. Only with types [ and [WULQJ are the values for offset and length taken in bytes.

SAP AG

BC401

sp9g{ w Bf

w  BfS

Guffu{%GIIG IGe `p sU` 0G IGe` `pS f uEuGpGg9 U3IsuGgGs {

2-29

2IIVHW DQG /HQJWK $FFHVVHV DQG 6WUXFWXUHV


$FFHVVLQJ VWUXFWXUHV E\ VSHFLI\LQJ RIIVHW DQG OHQJWK
VWDWHPHQW! VWUXFWXUH!RII! OHQ! 

l l l

6WUXFWXUH PXVW VWDUW ZLWK D FKDUDFWHUW\SH IUDJPHQW

$FFHVV LV RQO\ DOORZHG ZLWKLQ WKH FKDUDFWHUW\SH LQLWLDO SDUW

2IIVHW DQG OHQJWK VSHFLILFDWLRQV DUH LQWHUSUHWHG DV FKDUDFWHUV

SAP AG 2002

Offset and length accesses to structures are only permitted in Unicode programs if the structure is flat and the offset and length specifications only contain character-type fields from the beginning of the structure. This means, the access will cause an error if the offset and length area contains both character-type and non-character-type components. If an offset/length access is allowed in a Unicode program, both offset and length specifications are interpreted as characters. 5HFRPPHQGDWLRQ Only use offset and length accesses if it is necessary or useful. Bear in mind that processing component by component and using character string operations is generally safer and more readable.

n n

SAP AG

BC401

|z %%
2-30

w }0Q

f 9 ih f S

%e b w s

gpBggpgsuISgIIpI`{Gus

    

pgsuu  G gu9gg G gf GuG3IgGI9 GuG3Ig G p"sg gpsuuSIg f

'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 6XPPDU\

l 8VH HOHPHQWDU\ GDWD REMHFWV DQG VWUXFWXUHV DSSURSULDWHO\ l 8VH QXPHULF GDWD W\SHV DSSURSULDWHO\ l 8VH DXWRPDWLF W\SH FRQYHUVLRQV DSSURSULDWHO\ l 7DNH WKH VSHFLDO IHDWXUHV RI FKDUDFWHU VWULQJ SURFHVVLQJ LQ 8QLFRGH LQWR DFFRXQW

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

2-31

([HUFLVH 
8QLW 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 7RSLF 'HILQLQJ 'DWD 7\SHV DQG 'DWD 2EMHFWV %DVLF 6WDWHPHQWV 3URFHVVLQJ &KDUDFWHU 6WULQJV
Define structure types locally in the program Define elementary and complex data objects Split strings Use conversion rules Display the contents of data objects in lists
In this exercise, you will use a template to create a program that receives a single data record from the database table SFLIGHT in form of a character string. The program will split the this character string into its components and display it formatted in a list. Because the focus of this exercise is not on the transfer of data as a character string, we will use a function module that will provide us with the required database. This will simulate actual cases, such as data transfer from an external system. 7HPSODWH 3URJUDP 6$3%&B'727B63/,7B675,1* =%&B B63/,7B675,1*

At the conclusion of these exercises, you will be able to:

0RGHO VROXWLRQ

LV \RXU WZRGLJLW JURXS QXPEHU

6$3%&B'726B63/,7B675,1*

1-1

Copy the program, 6$3%&B'727B63/,7B675,1* and give it the new name =%&B B63/,7B675,1*. Familiarize yourself with the main body of the program. Pay special attention to the content of the data object datastring after the function module call. Use the Debugger to do this, and/or display the character string in a list. (The function module itself is here seen as a black box. For this exercise, it is not necessary to understand its construction.) To be able to split the character string into its components you must first remove the ## characters. Remove the two leading separators from the character string first. Then copy the initial part up to the closing separators to the auxiliary variable set_string. For this, set_string has to be defined appropriately.

1-2

1-3

SAP AG

BC401

2-32

1-4

Now use the separators to split the contents of the auxiliary variable set_string into the structure wa_flight_c. The latter is typed with the local program structure type st_flight_c. You still have to comment out the components of this structure type and assign them an appropriate type. As a test, display the fields of the structure, wa_flight_c in a list. In the list displayed in exercise 1-5, you should have observed that some of the fields were displayed without formatting for example, the PRICE field. Your next step is to change this. To do this, convert the data you have extracted by copying it to data objects with suitable types. Also, not all components of wa_flight_c are to be displayed. For this purpose, a structure wa_flight has already been defined. It is typed with the structure type st_flight. You must comment out the components of st_flight and find appropriate types for these components for the formatting. Then copy the identically-named components of the character-type structure wa_flight_c to the fields of the structure wa_flight. Display the contents of the structure wa_flight in a list. Use the appropriate formatting options for the WRITE statement for the fldate and price components.

1-5 1-6

SAP AG

BC401

2-33

6ROXWLRQ 
8QLW 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO 7RSLF 'HILQLQJ 'DWD 7\SHV DQG 'DWD 2EMHFWV %DVLF 6WDWHPHQWV 3URFHVVLQJ &KDUDFWHU 6WULQJV

REPORT
TYPES:

sapbc401_dtos_split_string.

BEGIN OF st_flight_c, mandt(3) carrid(3) connid(4) fldate(8) price(20) currency(5) planetype(10) seatsmax(10) seatsocc(10) TYPE c, TYPE F, TYPE Q, TYPE Q, TYPE F, TYPE F, TYPE F, TYPE Q, TYPE Q,

paymentsum(22) TYPE F, seatsmax_b(10) TYPE Q, seatsocc_b(10) TYPE Q, seatsmax_f(10) TYPE Q, seatsocc_f(10) TYPE Q, END OF st_flight_c, BEGIN OF st_flight, carrid(3) connid(4) fldate price(9) currency(5) seatsmax seatsocc TYPE c, TYPE n, TYPE G, TYPE F, TYPE L, TYPE L, TYPE S '(&,0$/6 ,

planetype(10) TYPE F,

END OF st_flight.

SAP AG

BC401

2-34

DATA: datastring VHWBVWULQJ TYPE string, 7<3( VWULQJ

wa_flight_c TYPE st_flight_c, wa_flight TYPE st_flight.

START-OF-SELECTION. CALL FUNCTION BC401_GET_SEP_STRING * EXPORTING * * * * IM_NUMBER IM_TABLE_NAME IM_SEPARATOR IM_UNIQUE IMPORTING ex_string EXCEPTIONS no_data OTHERS IF sy-subrc <> 0. MESSAGE a038(bc401). ENDIF. = 1 = 2. = datastring = 1 = SFLIGHT = # = X

6+,)7 GDWDVWULQJ %<  3/$&(6 ),1'


,) V\VXEUF !  (1',)
,1 GDWDVWULQJ

0(66$*( D EF 


,172 VHWBVWULQJ GDWDVWULQJ

63/,7 GDWDVWULQJ $7

63/,7 VHWBVWULQJ $7

,172 ZDBIOLJKWBFPDQGW ZDBIOLJKWBFFDUULG ZDBIOLJKWBFFRQQLG ZDBIOLJKWBFIOGDWH ZDBIOLJKWBFSULFH ZDBIOLJKWBFFXUUHQF\

ZDBIOLJKWBFSODQHW\SH

SAP AG

BC401

2-35

ZDBIOLJKWBFVHDWVPD[ ZDBIOLJKWBFVHDWVRFF ZDBIOLJKWBFSD\PHQWVXP ZDBIOLJKWBFVHDWVPD[BE ZDBIOLJKWBFVHDWVRFFBE

ZDBIOLJKWBFVHDWVPD[BI

ZDBIOLJKWBFVHDWVRFFBI

029(&255(6321',1* ZDBIOLJKWBF 72 ZDBIOLJKW WRITE: / wa_flight-carrid, wa_flight-connid, ZDBIOLJKWIOGDWH ''00<<<< ZDBIOLJKWFXUUHQF\ ZDBIOLJKWVHDWVPD[ ZDBIOLJKWSULFH &855(1&< ZDBIOLJKWFXUUHQF\ ZDBIOLJKWSODQHW\SH ZDBIOLJKWVHDWVRFF

SAP AG

BC401

2-36

8VLQJ ,QWHUQDO 7DEOHV &RQWHQWV


l ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV l 'HILQLQJ LQWHUQDO WDEOHV l 1RWHV RQ SHUIRUPDQFH l 6SHFLDO LQWHUQDO WDEOHV l ,QWHUQDO WDEOH RSHUDWLRQV

SAP AG 2002

SAP AG

BC401

3-1

8VLQJ ,QWHUQDO 7DEOHV 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HILQH LQWHUQDO WDEOHV l 3HUIRUP RSHUDWLRQV RQ LQWHUQDO WDEOHV

l ,GHQWLI\ WDEOH NLQGV DQG XVH WKHP DSSURSULDWHO\ LQ GLIIHUHQW VLWXDWLRQV

SAP AG 2002

SAP AG

BC401

3-2

R QI H (R PU I R UVSTPPGF X W          

DE ! " ) 8 D C )@9 ! 3 %1 " #BA(#67! ! " # 45#! 20' & % " 3 1) $ !

SAP AG
SAP AG 2002

8VLQJ ,QWHUQDO 7DEOHV ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

3-3

8VLQJ ,QWHUQDO 7DEOHV 

,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV
'HILQLWLRQ 'HILQLWLRQ
2SHUDWLRQV 2SHUDWLRQV
6SHFLDO VLWXDWLRQV 6SHFLDO VLWXDWLRQV
1RWHV RQ SHUIRUPDQFH 1RWHV RQ SHUIRUPDQFH

SAP AG 2002

SAP AG

BC401

3-4

7KH ,QWHUQDO 7DEOH  D 'DWD 2EMHFW RI 9DULDEOH /HQJWK


$%$3 SURJUDP

(OHPHQWDU\ GDWD REMHFWV

6WUXFWXUHV

6WDWLF GDWD REMHFWV H[FHSW IRU W\SHV 675,1* ;675,1*

,QWHUQDO WDEOH $W UXQWLPH WKH UXQWLPH V\VWHP DOORFDWHV PHPRU\ IRU WKH OLQHV LQ WKH WDEOH DV QHHGHG G\QDPLF WDEOH H[WHQVLRQ

SAP AG 2002

n n n

Internal tables are data objects that allow you to store datasets with a fixed structure in memory. The data is stored line by line in memory. Each line has the same structure. You can also refer to each component in a line as a column in the internal table. You refer to each line in the internal table as a table line or table entry. Internal tables are G\QDPLF GDWD REMHFWV - that is, they can hold any number of lines of a fixed type. The number of lines in an internal table is limited only by the capacity of the specific system with which you are working. You can define the line type of an internal table to include elementary, structured, or even table types, and can be as complex as you want to make it.

SAP AG

BC401

3-5

$WWULEXWHV RI ,QWHUQDO 7DEOHV

&$55,' &211,' ',67$1&( ',67,'


/LQH LQGH[

/LQH W\SH
,QGH[ DFFHVV

$$ /+ /+

 



   

0,

64

4)

8$











0,

0,

.0

.0

0,

64 

.H\ DFFHVV

.H\ GHILQLWLRQ
( .H\ ILHOGV ( 6HTXHQFH ( 8QLTXHQHVV
SAP AG 2002

7DEOH NLQG Standard table Sorted table Hashed table

Each internal table has the following attributes: /LQH W\SH You use the line type to specify which columns are to be included in your internal table. You must assign a name and type to each column. You generally define a line type using a structure type. .H\ Internal tables have a key, just like database tables. The key is defined by: Specifying the FROXPQV that should belong to the NH\; the VHTXHQFH; and whether or not the key should be XQLTXH or QRQ XQLTXH. Depending on how you define your internal table, the runtime system may create an DGGLWLRQDO XQLTXH NH\ (the LQGH[) for some internal tables (this will be discussed in more detail later). For simplicitys sake you can think of the index as a OLQH QXPEHU.

7DEOH NLQG There are three table kinds in ABAP. Each kind has different attributes, which specify whether the key is unique, how the index is managed, and how the table data is addressed internally. The table kind specifies the possible accesses. The purpose of this is to provide particular support for special access types from the runtime system (will be discussed later).

SAP AG

BC401

3-6

/LQN %HWZHHQ 7DEOH .LQG DQG $FFHVV 7\SH


,QGH[ WDEOHV 7DEOH NLQG +DVKHG WDEOH

STANDARD TABLE

SORTED TABLE

HASHED TABLE

,QGH[ DFFHVV
Q

SAP AG 2002

The table kind is linked to the access type as follows:


n n

You can only perform an index access (the fastest type of access) on STANDARD and SORTED tables. The runtime system implements key accesses of SORTED and HASHED tables in a way that optimizes runtime performance. Key access of STANDARD tables is by WDEOH VFDQ - that is, comparing the field contents with the search key line by line, in a loop. The runtime system implements key accesses of SORTED tables, for fully or partly qualified keys (left-aligned, no gaps, all fields filled with an "=") in a special way to optimize runtime. The same applies to partial sequential loops, where the loop condition is specified using the key fields. With the runtime-optimizing access, the runtime system keeps SORTED tables sorted by key fields, In all other cases, the system performs a WDEOH VFDQ. The runtime optimizes key accesses of HASHED tables IRU IXOO\ TXDOLILHG NH\V RQO\. The table entries are addressed by means of a special hash algorithm. This minimizes access time for reading single records with fully qualified keys. The access time is thus independent of the number of lines in the table. In all other cases, the system performs a WDEOH VFDQ. This means that you should only use HASHED tables if you want to access table entries using a fully-qualified key.

SAP AG

BC401

7edVa

V 29T

VT 2wb

$FFHVV E\

ip dte r e h Y

r e h Y c Y ` u r e h Y gsp gieysxYwvtsp iie

r e h Y e c Y ` dqp igfdbaY

8QLTXHQHVV

f 2V i f 29i l n p p( 7 #oo l gam x


3-7

l7ii w l k T 7g  k% Tg g j j k

i b gg hT%ff h g 7g 7

.H\ DFFHVV

8VLQJ ,QWHUQDO 7DEOHV 

,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV
'HILQLWLRQ 'HILQLWLRQ
2SHUDWLRQV 2SHUDWLRQV
6SHFLDO VLWXDWLRQV 6SHFLDO VLWXDWLRQV
1RWHV RQ SHUIRUPDQFH 1RWHV RQ SHUIRUPDQFH

SAP AG 2002

SAP AG

BC401

3-8

2YHUYLHZ 7\SHV RI 'HILQLWLRQV IRU ,QWHUQDO 7DEOHV


:LWK ORFDO WDEOH W\SH
/RFDO WDEOH W\SH

,PSOLFLW WDEOH W\SH

:LWK JOREDO WDEOH W\SH


*OREDO WDEOH W\SH

7<3(6 ORFDOBW\SH 


7DEOH DWWULEXWHV

JOREDOBW\SH
7DEOH DWWULEXWHV

,QWHUQDO WDEOH

'$7$ WBOLVW 7<3(

,QWHUQDO WDEOH

ORFDOBW\SH ORFDOBW\SH

'$7$ WBOLVW 7<3( 


7DEOH DWWULEXWHV

,QWHUQDO WDEOH

'$7$ WBOLVW 7<3( JOREDOBW\SH JOREDOBW\SH

SAP AG 2002

There are several ways to define an internal table in ABAP: Use the TYPES statement to define a table type. Then use this to assign a type to one or more data objects or interface variables. Alternatively, declare the table attributes directly when you define the data object (that is, use a ERXQG type).

The third option is to define an internal table using a global table type (defined in the ABAP Dictionary).
n

The tables you define using the first or second option are visible only in the program in which you define them. Whenever you need table type visible from all programs, create a global table type (that is, use the third option).

SAP AG

BC401

3-9

7KH 'DWD 7\SH RI DQ ,QWHUQDO 7DEOH


/RFDO WDEOH W\SHLQWHUQDO WDEOH
7<3(6
RU

QDPH 7<3( WDEOHBNLQG WDEOHBW\SH 2) 2) :,7+ NH\BGHI NH\BGHI OLQHBW\SH

'$7$

$WWULEXWHV
( 7DEOH NLQG ( /LQH W\SH FROXPQV ( .H\ ( ,QLWLDO VL]H RSWLRQDO

> ,1,7,$/ 6,=( Q @


*OREDO WDEOH W\SH JOREDOBW\SH
,QSXW RQ VFUHHQ XVHG WR VSHFLI\ DWWULEXWHV

$WWULEXWHV
( 7DEOH NLQG ( /LQH W\SH FROXPQV ( .H\

SAP AG 2002

n n n n

To define a table type (explicitly or implicitly), you must give the type (or data object) a name, as well as specifying a WDEOH NLQG OLQH W\SH and NH\ If you are defining a local table type, enter the kind after TYPE and the line type after OF. You must list the key fields after WITH. You create and edit global types in the ABAP Dictionary. If you are defining a global table type, specify the same information on the maintenance screens. For table types defined in a program, you can enter the number of lines that the runtime system should reserve when it initializes the data object, after the INITIAL SIZE addition. This makes sense if you know exactly how many lines you will want in your table when you create it. However, if your table needs more lines at runtime, it will not be limited in size by this addition, since the runtime system frees the necessary memory dynamically. (Internal tables are G\QDPLF data objects).

SAP AG

BC401

3-10

7DEOH $WWULEXWH /LQH 7\SH


/LQH W\SH
/RFDO VWUXFWXUH W\SH

*OREDO VWUXFWXUH W\SH

/RFDO VWUXFWXUH

7<3(6 7<3(6 %(*,1 2) OLQHBW\SH OLQHBW\SH  (1' 2) OLQHBW\SH

OLQHBW\SH

'$7$ '$7$

VWUXFWXUH 7<3( 

7DEOH W\SH

 QDPH 7<3( WDEOHBNLQG 7<3( 2) 2) :,7+ NH\BGHI OLQHBW\SH OLQHBW\SH

 QDPH /,.( WDEOHBNLQG VWUXFWXUH 2) :,7+ NH\BGHI

> ,1,7,$/ 6,=( Q @


SAP AG 2002

> ,1,7,$/ 6,=( Q @

n n n

To specify the line type of an internal table, you can use all the local and global data types or data objects. Internal tables are most frequently used to display contents of the database tables. Normally, nonnested structured data types are used for this.

If you use a line type, you must use a statement in the form: 7<3( WDEOHBNLQG 2) line_type, where WDEOHBNLQG is the kind of table (to be discussed later) and line_type is the name of the structure type you are using.

If you use structure (data object), you must use a statement in the form: /,.( WDEOHBNLQG 2) line_type, where structure is the name of the structure object you are using.

SAP AG

BC401

3-11

7DEOH $WWULEXWH 7DEOH .LQG DQG .H\

7<3(6 LWDEW\SH 7<3( WDEOHBNLQG


:,7+ NH\BGHI NH\BGHI NH\B

2)

OLQHBW\SH

> ,1,7,$/ 6,=( Q @ '$7$ WBQDPH 7<3( LWDEW\SH

WDEOHBNLQG
67$1'$5' 7$%/( 6257(' +$6+('
SAP AG 2002

NH\BGHI NH\B
>12181,48(@ ^ .(< FRO  FROQ _ '()$8/7 .(< ` ^81,48( _ 12181,48(` .(< FRO FROQ 81,48( .(< FRO  FROQ

7$%/( 7$%/(

n n n

There are three parameters that you use to specify a table kind for your internal table: STANDARD TABLE, SORTED TABLE and HASHED TABLE You specify the table key with the :,7+ NH\BGHI addition. NH\BGHI includes the names of all the key fields in order and specifies whether the key is to be UNIQUE or NON-UNIQUE. The combination of the table kind and the key definition is very significant, because of the special support that certain table kinds receive with specific types of read access. You can use any of the following combinations: For STANDARD tables: Either create a user-defined key by naming the key fields after NON-UNIQUE KEY, or specify the standard key using the WITH DEFAULT KEY addition. The standard key consists of all the fields with character-type data types (c, n, d, t, x, string, xstring). For SORTED tables: List the key fields after WITH UNIQUE KEY or NON-UNIQUE KEY as appropriate. For HASHED tables: List the key fields after WITH UNIQUE KEY.

Alternatively, use the pseudo-component table_line, if you are specifying a table without a structured line type, or if the entire table line is being used as the key. This will be discussed in more detail later in the unit.

SAP AG

BC401

3-12

([DPSOH 6WDQGDUG 7DEOH ZLWK /RFDO 7\SH


/LQH W\SH

7<3(6 VBGLVWDQFHBW\ %(*,1 2) VBGLVWDQFHBW\ FDUULG 7<3( VBFDUUBLG FRQQLG 7<3( VBFRQQBLG GLVWDQFH 7<3( VBGLVWDQFH GLVWLG 7<3( VBGLVWLG (1' 2) VBGLVWDQFHBW\

7DEOH W\SH

,QWHUQDO WDEOH

7<3(6 WWBGLVWDQFHBW\ 7<3( 67$1'$5' 7$%/( 2) VBGLVWDQFHBW\ :,7+ 12181,48( .(< GLVWDQFH GLVWLG
,QWHUQDO WDEOH RI WKH WDEOH NLQG 67$1'$5' 7$%/(

'$7$ WWBGLVWDQFHBW\ WWBGLVWDQFHBW\ WWBGLVWDQFH 7<3( WWBGLVWDQFHBW\ WWBGLVWDQFH

SAP AG 2002

n n n

The above example shows the definition of an internal table (tt_distance) using a local table type (tt_distance_ty), which itself uses a local line type (s_distance_ty). The internal table defined here is a STANDARD table with the line type s_distance_ty. It contains the columns carrid, connid, distance and distid. The distance and distid fields are key fields. The key is not unique.

SAP AG

BC401

3-13

([DPSOH 6RUWHG DQG +DVKHG 7DEOH


6HTXHQFH

6RUWHG WDEOH

'$7$ GLVWDQFHBUDQF 7<3( 6257(' 7$%/( VBGLVWDQFHBW\ 2) VBGLVWDQFHBW\ :,7+ 12181,48( .(< GLVWLG GLVWDQFH
+DVKHG WDEOH
6HTXHQFH

FDUULG
   

FRQQLG

GLVWDQFH GLVWLG
 

/+ /+ $$ 40

   

   

.0 .0 0, 0,

6HTXHQFH

'$7$ GLVWDQFHBEXIIHU GLVWDQFHBEXIIHU 7<3( +$6+(' 7$%/( VBGLVWDQFHBW\ 2) VBGLVWDQFHBW\ :,7+ 81,48( .(< FDUULG FRQQLG

FDUULG


FRQQLG


GLVWDQFH GLVWLG

/+ $$ 40 /+

   

   

.0 0, 0, .0

SAP AG 2002

n n

The above example shows the definition of a SORTED table and a HASHED table. Both tables have the same line type as the STANDARD table on the previous slide. Note that the contents of the table are in a different order. For SORTED tables, the sequence of the entries in the internal table is determined by the sequence of fields in the key definition.

SAP AG

BC401

3-14

7KH 6WDQGDUG 7DEOH 7\SH


'$7$ WBQDPH 7<3( 7$%/(

2) OLQHBW\SH
,QWHUSUHWHG E\ WKH V\VWHP DV

'HILQLWLRQ E\ VWDQGDUG WDEOH W\SH

'$7$ 67$1'$5' WBQDPH 7<3( 67$1'$5' 7$%/( 2) OLQHBW\SH :,7+ '()$8/7 .(< 

([DPSOH '$7$ WBVIOLJKW 7<3( 7$%/( 2) VIOLJKW

SAP AG 2002

A standard type exists for defining standard tables with bound types. (Table type STANDARD TABLE, key WITH DEFAULT KEY). When you create such a table, you can omit the STANDARD and WITH DEFAULT KEY additions, since the runtime system supplies them automatically. Note however, that the standard table type exists for data objects only. If you are defining a table type that you want to use to provide a type for data objects, you must specify all its attributes completely. Incomplete table types are known as JHQHULF. You can only use them to assign types to table-type interface parameters.

SAP AG

BC401

3-15

,QWHUQDO 7DEOHV ZLWK DQ 8QVWUXFWXUHG /LQH 7\SH


'HILQLWLRQ '$7$

LWBILHOGV 7<3( 67$1'$5' 7$%/( 2) GGOILHOGQDPH


WDEOHBOLQH :,7+ .(< WDEOHBOLQH WDEOHB

ZDBILHOG

7<3( GGOILHOGQDPH

)LOO

6(/(&7 ILHOGQDPH )520 GGO ,172 7$%/( LWBILHOGV :+(5( WDEQDPH

WDEOH RI WUDQVS WDEOHV

63)/,


.H\ DFFHVV

5($' 7$%/( LWBILHOGV ,172 ZDBILHOG :,7+ .(< WDEOHBOLQH WDEOHBOLQH WDEOHB

&,7<)520


SAP AG 2002

n n n

Use an unstructured line type if you need a single-column internal table. The slide shows an example of a single-column table with the line type field_name. To declare an explicit key, you must use the pseudo-component table_line. You can use internal tables with an unstructured line type in the following ABAP statement (among others): SET PF-STATUS .. EXCLUDING itab. (deactivate function codes dynamically) SPLIT .. INTO TABLE itab. (split a string dynamically)

You can use a key access, for example, to ascertain whether a specific entry H[LVWV.

SAP AG

BC401

3-16

8VLQJ ,QWHUQDO 7DEOHV 

,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV
'HILQLWLRQ 'HILQLWLRQ
2SHUDWLRQV 2SHUDWLRQV
6SHFLDO VLWXDWLRQV 6SHFLDO VLWXDWLRQV
1RWHV RQ SHUIRUPDQFH 1RWHV RQ SHUIRUPDQFH

SAP AG 2002

SAP AG

BC401

3-17

2YHUYLHZ 7\SHV RI 2SHUDWLRQ


,QWHUQDO WDEOH DV D GDWD REMHFW
2SHUDWLRQV RQ RQH RU PRUH OLQHV
RU

(GLWLQJ OLQHV LQ D ORRS

( &RS\ ( ,QLWLDOL]H ( ,QLWLDOL]H

&/($5 .HHS PHPRU\ DYDLODEOH )5(( 5HOHDVH PHPRU\

029(

( ,QVHUW ( 5HDG ( 'HOHWH

,16(57 5($'

( (GLW OLQHV

VHTXHQWLDOO\

( &KDQJH 02',)< ( ,QVHUW VXPPDWHG


'(/(7( &2//(&7

( &RPSDUH XVLQJ RSHUDWRUV ( 6RUW !!  !


6257

/223  FRPPDQG EORFN (1'/223

( $UUD\ )HWFK 6(/(&7  ,172 7$%/(


SAP AG 2002

You can perform three different types of operations on internal tables:


n

2SHUDWLRQV RQ WKH ZKROH GDWD REMHFW: Elementary operations available include MOVE (copy the content line by line), CLEAR (delete the content, but keep the memory allocated), and FREE (delete the content and release the memory). You can also use all the numerical comparison operators with internal tables in logical expressions. With these operators, the number of lines is compared first and then, if necessary, the line contents. (This is recursive for deep types). For more information, refer to the documentation on internal tables. You can use the SORT statement to sort the content of STANDARD and HASHED tables. This statement allows you to sort the table by one or columns, specifying DVFHQGLQJ or GHVFHQGLQJ for each column. For more details, refer to the keyword documentation for the SORT statement. You can use the SELECT ... INTO TABLE statement to copy the contents of database tables directly into internal tables. 2SHUDWLRQV RQ OLQHV: You can use the INSERT, READ, MODIFY, DELETE, and COLLECT statements to perform operations on one or more lines in the internal table. The syntax of the first four of these statements is dealt with in the slides that follow. For the syntax of the COLLECT statement, refer to the online documentation. /RRS SURFHVVLQJ You can use the LOOP AT ... ENDLOOP statement to process the lines of the internal table sequentially. The exact syntax for this is dealt with in the slides that follow.

SAP AG

BC401

3-18

/LPLWLQJ WKH 6FRSH RI /LQH 2SHUDWLRQV 7DUJHW 5HFRUGV DQG $FFHVV 7\SH
6SHFLI\ VHW RI WDUJHW UHFRUGV
Q
FRQGLWLRQ!
> Q  Q @
7DEOH NH\ /LQH LQGH[ IRU LQGH[ WDEOHV &RQGLWLRQ DSSOLHG WR DQ\ QXPEHU RI FROXPQV ,QGH[ LQWHUYDO IRU LQGH[ WDEOHV

6SHFLI\ DFFHVV W\SH  GDWD WUDQVIHU W\SH


:RUN DUHD
7DEOH

)LHOG V\PERO

&RS\LQJ SURFHVV
SAP AG 2002

'LUHFW DFFHVV E\ GHUHIHUHQFHG SRLQWHU

,QVHUW XVLQJ WDEOH

Almost all line operations and loop processing statements offer you several options for specifying the target set of records to be processed. You can specify these target records by declaring a key or index, by formulating a condition that applies to some of the columns, or by specifying an index interval. Your use of these options is limited only by the table kind you have chosen. Example: You cannot specify a line in a hashed table using an index. When accessing table rows, you have two possibilities for a number of line operations and for loop processing: You can either copy the data into a structure with the same type as the line type of the internal table, or you can set a pointer to a table line and access the data in that line directly. In particular, when you insert table lines, you can pass the data to be inserted using another internal table. Note that all statements either set a return value or trigger a runtime error. If a return code has been set, it is stored in the sy-subrc field after the statement has been executed. Runtime errors are only triggered if the data passed at runtime are crucial in determining whether or not the operation can be performed. Example: You try to insert a new line into a SORTED table using an index. If you insert the new line in exactly the right place in a sorted table, the system performs the operation. If not, the system returns a runtime error.

n n

SAP AG

BC401

3-19

3UHYLHZ )LHOG 6\PER,V 


'DWD REMHFWV LQ WKH $%$3 SURJUDP

'$7$ YDUBD 7<3( L 9$/8(  ),(/'6<0%2/6 IV! 7<3( L ),(/'6<0%2/6


$VVLJQLQJ D GDWD REMHFW WR D ILHOG V\PERO

'HILQLQJ D ILHOG V\PERO

IV! YDUBD

$66,*1 YDUBD 72 IV!

IV! YDUBD

IV!
7LPH

$VVLJQLQJ D YDOXH WR D GDWD REMHFW XVLQJ D ILHOG V\PERO



IV! YDUBD



SAP AG 2002

n n

First, declare a data object using the ),(/'6<0%2/6 statement. This data object can contain a pointer to another data object at runtime. Where possible, you should give the field symbol the same type as the data object (7<3( L, in this example). Note that the angle brackets (<>) are part of the name of the field symbol: In this example, the name is <fs>. To point a field symbol at a data object, you must assign it to the object GDWDBREMHFW using the $66,*1 GDWDBREMHFW 72 IV! statement. You can use the field symbol to access the content of the data object to which it points - either to read or to change this content.

You can create a pointer to a data object in ABAP using a field symbol.

n n n n

You can "redirect" a field symbol to a different data object at runtime using the $66,*1 statement.

SAP AG

BC401

3-20

3UHYLHZ )LHOG 6\PER,V 


{ g zk
3-21

'$7$ LWBVIOLJKW 7<3( 7$%/( 2) VIOLJKW ),(/'6<0%2/6 IV! 7<3( VIOLJKW


$VVLJQLQJ D OLQH WR WKH ILHOG V\PERO

 WR EH FRQWLQXHG 

$FFHVVLQJ WKH OLQH FRQWHQW XVLQJ WKH ILHOG V\PERO

:5,7( IV!FRQQLG IV!FDUULG


/+


SAP AG 2002

In the above example, a field symbol is assigned the line type of an internal table. This makes it possible to assign a table line to this field symbol. The syntax required for this is discussed later in this unit. After a field symbol has been assigned a line, it is also possible to access the individual column values of the assigned line. As well as being able to read the data contents, you can also change the contents of the individual components.

n n

SAP AG

BC401

y w v 7 xsu uwlPsBtsV i k j r h g xq g

'HILQLQJ WKH ILHOG V\PERO

IV!

LWBVIOLJKW

IV! LWBVIOLJKW

IV! LWBVIOLJKW

,QVHUW
.H\ IURP ZD VSHFLILHV WKH OLQH

ZD

LWDE

ILOO ZRUNDUHD

,16(57 ZD ,172 7$%/( LWDE ,16(57 7$%/(


LWDE
Q
Q

ZD

ZD

ILOO ZRUNDUHD

,16(57 ZD ,172 LWDE ,1'(; Q ,16(57 ,172


,16(57 /,1(6 2) LWDE ,16(57 /,1(6 2) > )520 Q 72 Q @ WDUJHW
WDUJHW

ZD

LWDE
>Q@ >Q@

LWDE
Q

2QO\ LI LWDE LV DQ LQGH[ WDEOH

Q
SAP AG 2002

,172 LWDE > ,1'(; Q @ ,172

,172 7$%/( ,172 7$%/( LWDE

To insert lines into a table, use the INSERT statement:


n

Using INSERT wa INTO itab INDEX n, you insert the data from ZD into the internal table at the line with the index Q. If Q is an index value within the internal table, the lines with the index value greater than or equal to Q are pushed downwards. If n-1 is greater than the number of lines in the internal table, the line is not inserted. Always add new lines to a SORTED table using the key. If you try to insert a line into a SORTED table using the index, you will not succeed unless the line has exactly the same index number as it would have when correctly sorted. Otherwise, a runtime error occurs. Use INSERT LINES OF itab1 INTO TABLE itab2 to insert the lines of the internal table itab1 into the internal table itab2. Also, if itab1 is an index table, you can limit the number of lines to be inserted using an index interval (FROM n1 TO n2). If your target table is an index table, you can use INSERT LINES OF itab1 INTO itab2 to insert the lines of the internal table itab1 into the internal table itab2. You can then specify the line index from which the lines are to be inserted using INDEX n. Also, if itab1 is an index table, you can limit the number of lines to be inserted using an index interval (FROM n1 TO n2).

Using INSERT wa INTO TABLE itab, you insert the line whose data are stored in wa into the internal table. The record is inserted with reference to the table key. In STANDARD tables, the new line is appended to the end of the table.

SAP AG

BC401

3-22

5HDGLQJ D 6LQJOH 5HFRUG LQWR D :RUN $UHD 8VLQJ WKH /LQH 1XPEHU
ZD LWDE
Q
Q

,QGH[

,172 5($' 7$%/( LWDE ,1'(; Q ,172 ZD > RSWLRQV @ ,1'(;


> RSWLRQV @
%HOLHELJH )HOGOLVWH $Q\ ILHOG OLVW

75$163257,1* WUDQVBOLVW

&203$5,1*

FRPSBOLVW

( I  IP ( $// ),(/'6


WUDQVBOLVW

FRPSBOLVW

( I  IN ( 12 ),(/'6

SAP AG 2002

You can read single table lines from index tables using the READ TABLE itab INDEX n INTO wa statement. After INDEX, enter the index of the line you want to read. If the system was able to read the line, it sets the return code to sy-subrc = 0 and stores the data from the line in wa. The total number of lines is then in sy-tfill, the length of the lines in sy-tleng. If the system could not read the line, the return code sy-subrc is unequal to 0. In this case, the content of wa is not changed. Use the option TRANSPORTING addition to specify the columns for which you want to transport data: If you do not want to read any data, but simply want to ascertain whether or not line n exists (evaluating the return value sy-subrc), use the TRANSPORTING NO FIELDS addition. If you want to read some of the columns in the line only, specify them after TRANSPORTING. Separate each column name with a space.

You can use the optional addition COMPARING I ... IP to ascertain whether or not the line to be read Q has specific column contents: To do this, copy the value of all columns that point to the records you want to read, into wa and list the columns after the COMPARING addition to the READ statement. If the system was able to read the line and if all the columns listed contain the values stored in ZD, it sets the return code sy-subrc to zero. If the system was able to read the line, but if one or more of the columns does not contain the value stored for it in ZD, it sets the return code sysubrc to two. If it could not read the line, it sets the return code to greater than two. The COMPARING ALL FIELDS addition provides a shorter syntax for comparing all the columns.

SAP AG

BC401

3-23

5HDGLQJ D 6LQJOH 5HFRUG LQWR D :RUN $UHD 8VLQJ WKH )LHOG &RQWHQWV
.H\
ZD LWDE
ZD 7DEOH NH\ IURP ZD ([SOLFLW WDEOH NH\
$Q\ FRQWHQW IURP ILHOG OLVW

5($' 7$%/( LWDE VHDUFKBFODXVH ,172 ZD > RSWLRQV @


VHDUFKBFODXVH )520 ZD 7DEOH NH\
$Q\ ILHOG FRQWHQWV
SAP AG 2002

$V LQ LQGH[ DFFHVV

:,7+ .(< FRO I  FROP >%,1$5< 6($5&+@

:,7+ 7$%/( .(< N

I  NQ

IP

IQ

n n

You can read individual lines from any kind of table using READ TABLE itab NH\ INTO wa. Use either a table key, or a comparison for some of the columns, as the search criterion NH\. If you want to use a table key, you have two options: Copy the key field values of the entry you want to read into the work area wa1 and use READ TABLE itab FROM wa1 INTO wa. Provided sy-subrc = 0, the system stores the result of the READ statement in the work area wa. Note that the values in wa1 that you have not explicitly filled contain appropriately-typed initial values. Thus, the READ TABLE itab FROM wa1 statement searches for a line that has initial values in the key fields that were not declared explicitly. Note: You can also use a single work area wa for both declaring the key fields and receiving the result: READ TABLE itab FROM wa INTO wa. Evaluate the key fields explicitly using the call READ TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn statement. In this case, you must fill all the key fields.

n n

You can specify a formulated search condition to be applied to any columns using READ TABLE itab WITH KEY .... However, you can only use dynamically formulated read accesses using the key and the READ TABLE itab FROM wa statement.

SAP AG

BC401

3-24

5HDGLQJ D 6LQJOH 5HFRUG 8VLQJ D )LHOG 6\PERO


LWDE

IV!

.H\
RU

,QGH[

),(/'6<0%2/6 IV! /,.( /,1( 2) LWDE 5($' 7$%/( LWDE


,1'(; Q

NH\

$66,*1,1*  ! IV!

,QGH[ .H\

:5,7(  IV!ILHOGB  )6!ILHOGB


6DPH DV ZKHQ UHDGLQJ LQWR D ZRUN DUHD
SAP AG 2002

n n

You can use either a work area or a field symbol to access the individual lines in a table you want to read. To use a field symbol, first define it using FIELD-SYMBOLS <fs>. Give it the same type as the line type of the internal table that you want to read. Example: FIELD-SYMBOLS <fs> LIKE LINE OF itab. Set the pointer to the correct line using the ASSIGNING <fs> addition instead of INTO. You can use any search criterion you want. You can access the components of the structure directly using the component names in the line type: You can directly address elementary components in the line type using <fs>-field_1. If the line type contains structured components (a structure or internal table), <fs>-component_name points to the entire structured component. To access the subcomponents of this structured component, you need another field symbol.

n n

Note: If you are searching for a specific string in an internal table, you can also use the SEARCH statement. For more details, refer to the keyword documentation for the SEARCH statement.

SAP AG

BC401

3-25

&KDQJLQJ WKH 7DEOH 8VLQJ D :RUN $UHD


.H\ IURP ZD VSHFLILHV WKH OLQH

LWDE ZD

02',)< 7$%/( LWDE )520 ZD )520

ILOO ZRUNDUHD

ZD

> 75$163257,1* I  IQ @ 75$163257,1*

LWDE ZD
Q
Q

02',)< LWDE )520 ZD ,1'(; Q ,1'(;

ILOO ZRUNDUHD

ZD

> 75$163257,1* I  IQ @


02',)< LWDE )520 ZD
:+(5( ORJBH[SU

ZD

LWDE

:+(5( ORJBH[SU

75$163257,1* I I 

SAP AG 2002

The MODIFY TABLE itab FROM wa statement allows you to change the content of RQH line of the internal table itab. The runtime system specifies the line to be changed using the key values from the work area ZD and changes the non-key fields using the other fields. If your table has a nonunique key, the system changes the first entry only (using a linear search algorithm in STANDARD tables, and a binary search algorithm in SORTED tables). If you want to change only some of the fields (that is, columns) in a line, you must specify these after TRANSPORTING. You can change the Qth line in an index table using MODIFY itab FROM wa INDEX n. Note that all the fields of the structure wa will be copied. Since changes to key fields in SORTED and HASHED tables can cause non-catchable runtime errors, you PXVW use the TRANSPORTING addition in this case. If you want to make the same changes to several lines in a table, use the MODIFY itab FROM wa TRANSPORTING f1 f2 ... WHERE log_expr statement. Specify all the columns to be changed after TRANSPORTING. You must place the new values for these columns in the work area wa. Specify a condition for the line using the WHERE clause.

SAP AG

BC401

3-26

&KDQJLQJ D 6LQJOH 5HFRUG 8VLQJ D )LHOG 6\PERO


LWDE

IV!


.H\
RU

IV!ILHOGB



,QGH[

),(/'6<0%2/6 IV! /,.( /,1( 2) LWDE





5($' 7$%/( LWDE )6!ILHOGB IV!ILHOGB IV!ILHOGB

,1'(; Q NH\

$66,*1,1* IV!

,QGH[ .H\


6DPH DV ZKHQ UHDGLQJ LQWR D ZRUN DUHD

&DQQRW EH NH\ ILHOG LQ VRUWHG RU KDVKHG WDEOHV


SAP AG 2002

Instead of changing an individual line in a table using MODIFY, you can also change it using field symbols. First use READ TABLE ... ASSIGNING <fs> to assign a field symbol to the line to be changed. Then change the components directly using the field symbol components <fs>field_1 = .... When you assign the field symbol using READ TABLE ... ASSIGNING you can specify the line to be read using a key (WITH TABLE KEY), index (for index tables), or a condition (WITH KEY). Note that you cannot change key fields in SORTED or HASHED tables. - trying to do so causes a runtime error.

n n

SAP AG

BC401

3-27

'HOHWH
ILOO ZRUNDUHD ZD

LWDE

7DEOH NH\

ZD ([SOLFLW

'(/(7( 7$%/( LWDE NH\


NH\

LWDE
Q
Q

( )520 ZD ( :,7+ 7$%/( .(< N I  NQ

IQ

'(/(7( LWDE ,1'(; Q

:+(5( ORJBH[SU
SAP AG 2002

'HOHWH XVLQJ D FRQGLWLRQ

'(/(7( LWDE :+(5( ORJBH[SU

Use the READ TABLE itab INDEX n INTO wa statement to delete single table lines from index tables. Specify the line you want to delete using the table key. There are two ways of doing this: Copy the key field values of the entry you want to delete into the work area wa and use DELETE TABLE itab FROM wa. Fill the key fields directly in the DELETE statement using the WITH TABLE KEY k = f ... kQ = fQ addition.

n n

You can delete the Qth line from the index table itab using DELETE itab INDEX n If you want to delete several lines from an internal table, use the DELETE itab WHERE log_expr statement. The condition that specifies the lines you want to delete is declared in ORJBH[SU, in the WHERE clause (where ORJBH[SU is any logical expression applied to the columns).

SAP AG

BC401

3-28

/RRS 3URFHVVLQJ
UHVXOW
ZD
LWDE

UHVXOW

IV!

( ,172 ZD ( $66,*1,1* IV!

/223 $7 LWDE UHVXOW >75$163257,1* 12 ),(/'6@

^ > )520 Q @ > 72 Q @ _ > :+(5( ORJBH[SU @ ` 

,QGH[ WDEOHV RQO\

(1'/223 (1'/223
SAP AG 2002



,QGH[ WDEOH V\WDEL[ FRQWDLQV OLQH LQGH[ RI FXUUHQW OLQH

Use the LOOP AT itab UHVXOW ... ENDLOOP statement to perform loop processing on the lines of the internal table itab. The system then executes the statement block between LOOP AT itab ... and ENDLOOP for each loop pass. UHVXOW stands for: INTO wa: The system copies the table line it has processed in each loop pass into the work area wa

ASSIGNING <fs>: The relevant line is assigned to the field symbol <fs>.
n

With both variants you can use the TRANSPORTING NO FIELDS addition. No data is copied. Use this addition if, for example, you simply want to determine the number or indexes of the lines processed by the loop. Specify the number of lines that the loop is to process using a WHERE clause (condition for any columns). In an index table, limit the number of lines processed in the loop by declaring an index interval. While the loop is being processed, the system field sy-tabix contains the index of the table row of the current loop pass. The total number of lines is in sy-tfill, the length of the lines in sytleng. Note that in index tables, you can perform all the line operations (which you previously performed on the line specified in the INDEX n addition) ZLWKRXW this addition in the loop. The operation is then performed on the current line.

n n n

SAP AG

BC401

3-29

2YHUYLHZ RI 2SHUDWLRQV
6257
799fg ~} ~
w V V 99e

5($' 7$%/( 02',)< '(/(7(


g

/223 $7

,16(57 02',)< '(/(7(

5($' 7$%/(

/223 $7
SAP AG 2002

Index accesses (such as APPEND, INSERT ... INDEX, LOOP AT ... FROM ... TO) are possible for standard and sorted tables. However, caution is advised when using INSERT or APPEND on sorted tables; this would violate the sort sequence and cause a runtime error. You can use the SORT statement to sort standard and hashed tables. Sorted tables are sorted by the runtime system. You can use key accesses with any table type, but their effect differs. A key access with INSERT has the same effect on standard tables as an APPEND, that is the relevant line is appended to the end of the table. In a sorted table however, the record is inserted in accordance with the sort sequence. In the case of a hashed table, the line is appended, but the system also changes the hash index internally.

n n

SAP AG

BC401

w f g 9i 9ih T p ww p

,16(57

~} } 799A5y|
9 VqBsTV%9a

~} ~ ~ A79Pwf9}d|

9999i i

i 9w9i

Th Va

3-30

8VLQJ ,QWHUQDO 7DEOHV 

,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV
'HILQLWLRQ 'HILQLWLRQ
2SHUDWLRQV 2SHUDWLRQV
6SHFLDO VLWXDWLRQV 6SHFLDO VLWXDWLRQV

1RWHV RQ SHUIRUPDQFH 1RWHV RQ SHUIRUPDQFH

SAP AG 2002

SAP AG

BC401

3-31

,QWHUQDO 7DEOHV ZLWK D +HDGHU /LQH


'$7$ 6\QWD[ YDULDQW 

LWDE 7<3( 7$%/( 2) VFDUU


:,7+ +($'(5 /,1( /,1(

:,7+ '()$8/7 .(<

:RUN DUHD KHDGHU

LWDE

'DWD REMHFW LWDE

'$7$ 6\QWD[ YDULDQW  REVROHWH


:,7+ +($'(5 /,1( /,1(

LWDE 7<3( VFDUU 2&&856  2&&856

LWDE
%RG\

'$7$ 6\QWD[ YDULDQW  REVROHWH FDUULG 7<3( VBFDUUBLG  XUO 7<3( VBFDUUXUO (1' 2) LWDE
SAP AG 2002

%(*,1 2) LWDE 2&&856 

As well as the internal tables we have discussed, you can define LQWHUQDO WDEOHV ZLWK D KHDGHU. These consist of a pair of components - the internal table itself (the ERG\) and the work area (KHDGHU), with the same line type. The header and the body have the same name, which sometimes simplifies the syntax for table accesses. There are several possible syntax variants used to define internal tables with header lines, some of which are shown in the slide. Internal tables with header lines are the oldest form of internal table. However, note that Many statements have a different effect on internal tables with a header than they would on normal internal tables. You can then address the body separately using ">@" . ([DPSOH The CLEAR itab statement initializes only the header line, whereas in an internal with no header, it initializes the content of the entire table. You can delete the body of an internal table with a header using CLEAR itab[] or REFRESH itab. You FDQQRW use internal tables with a header line in an object-oriented environment - that is, within classes or interfaces.

n n n

SAP AG

BC401

3-32

6HOHFWLRQ 2SWLRQV DQG 6HOHFWLRQ 7DEOHV


'$7$ FDUULG 7<3( VBFDUUBLG VRBFDUU )25 FDUULG

6(/(&7237,216 6(/(&7237,216

( /LQN WR VHOHFWLRQ VFUHHQ


VRBFDUU ( 'DWD REMHFW VRBFDUU
,GHQWLFDOO\W\SHG GDWD REMHFW ZLWKRXW KHDGHU RU OLQN WR VHOHFWLRQ VFUHHQ

'$7$ VRBFDUU /,.( 5$1*( 2) FDUULG 5$1*( 2)


w 77T Ve
f
BC401 3-33

SAP AG 2002

As of SAP R/3 Basis Release 4.6A onwards, you can use the 5$1*( 2) addition to the 7<3(6 and '$7$ statements to define a corresponding (internal) table without a header line.

Use the 6(/(&7237,21 statement to create an internal table with a header, for which the runtime system automatically creates an input dialog for value sets for a selection screen. The system automatically inserts the appropriate entries in the internal table, from the user input. For more details, refer to the keyword documentation for the 6(/(&7237,216 statement.

SAP AG

w %T7

nT e2qT9ne

9f7 %Pq797

8VLQJ ,QWHUQDO 7DEOHV 

,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV ,QWURGXFWLRQ DQG DGYDQWDJHV RI LQWHUQDO WDEOHV
'HILQLWLRQ 'HILQLWLRQ
2SHUDWLRQV 2SHUDWLRQV
6SHFLDO VLWXDWLRQV 6SHFLDO VLWXDWLRQV

1RWHV RQ SHUIRUPDQFH 1RWHV RQ SHUIRUPDQFH

SAP AG 2002

SAP AG

BC401

3-34

6LQJOH 5HFRUG $FFHVV &RPSOHWHO\ DQG 3DUWLDOO\ 4XDOLILHG .H\


5($' 7$%/( LWDE ,172 ZD :,7+ 7$%/( .(< NH\ :,7+ 7$%/( .(<
0XVW EH FRPSOHWHO\ TXDOLILHG

7DEOH NLQG

NH\

67$1'$5'
7DEOH VFDQ

6257('
%LQDU\ VHDUFK

+$6+('
+DVK DOJRULWKP

5($' 7$%/( LWDE ,172 ZD :,7+ .(< NH\ :,7+ .(<


NH\

7DEOH NLQG

67$1'$5'

6257('
%LQDU\ VHDUFK

+$6+('

&RPSOHWHSDUW NH\ OHIWDOLJQHG ZLWKRXW JDSV


%\ TXDOLILHG NH\
$Q\ FRPSRQHQW FRQGLWLRQ
SAP AG 2002

7DEOH VFDQ

7DEOH VFDQ

7DEOH VFDQ

Whenever you want to read individual table lines by declaring a FRPSOHWH key, use the READ TABLE ... WITH 7$%/( KEY statement (fastest single record access by key). The runtime system supports this syntax variant especially for SORTED and HASHED tables. If the table is a STANDARD table, the runtime system performs a WDEOH VFDQ. The same applies if you have copied the values from DOO key fields of the entry to be read into the work area wa and are then use READ TABLE itab FROM wa. The runtime system carries out the syntax variant READ TABLE ... WITH KEY (read an entry after applying DQ\ condition) using a WDEOH VFDQ The only exception to this rule applies to SORTED tables, if you fill the first Q key fields with "=" (no gaps), where Q <= number of key fields. With standard tables however, you can also sort correspondingly using SORT and then use the BINARY SEARCH addition. Whenever possible, use READ TABLE ... WITH 7$%/( KEY or the variant with a correspondingly-filled work area. If you need to use READ TABLE ... WITH KEY, make your internal table a SORTED table.

6XPPDU\
n n

SAP AG

BC401

3-35

/RRS 3URFHVVLQJ DQG 7DEOH .LQGV


/223 $7 LWDE ,172 ZD ORJBH[SU :+(5( ORJBH[SU ORJBH[SU  (1'/223
ORJBH[SU
)LUVW Q NH\ ILHOGV ILOOHG ZLWK ZLWKRXW JDSV
$Q\ ORJLFDO H[SUHVVLRQ IRU FROXPQV

7DEOH NLQG

67$1'$5'

6257('
%LQDU\ VHDUFK IRU VWDUWLQJ SRLQW WKHQ ORRS RQO\ WKURXJK JURXS OHYHO 7DEOH VFDQ

+$6+('

7DEOH VFDQ

7DEOH VFDQ

SAP AG 2002

n n

The runtime system generally processes loops with a WHERE clause by performing a WDEOH VFDQ that is, determining whether the condition in the WHERE clause is true for each line in the table. SORTED tables are the only exception to this rule. For these, the runtime system optimizes the runtime under the following condition: In the WHERE clause, the first Q key fields are filled with a "=" (no gaps). (Q is less than or equal to the number of all key fields). As a result, the loop is only performed on the lines that match the condition in the WHERE clause. Since the table is sorted, the first line can be specified to optimize performance at runtime (using a binary search).

SAP AG

BC401

3-36

3DUWLDO 6HTXHQWLDO /RRS 7KURXJK 6WDQGDUG 7DEOHV


'HVLUHG /RRS WKURXJK GDWD UHFRUGV ZKHUH &,7<)520
)5$1.)857

&$55,'

$$ /+ 8$ 8$ /+

&211,' &,7<)520

 6$1 )5$1&,6&2   )5$1.)857   %(5/,1   )5$1.)857  1(: <25. 


6257  %< FLW\IURP 67$%/(



&$55,' &211,' &,7<)520 /+ /+  %(5/,1  )5$1.)857  )5$1.)857

$VFHUWDLQ VWDUWLQJ SRLQW 5($' 7$%/(  :,7+ .(< FLW\IURP


)5$1.)857
75$163257,1* 12 ),(/'6 %,1$5< 6($5&+ VWDUWOLQH V\WDEL[

8$

8$

$$

 1(: <25.   6$1 )5$1&,6&2 









$VFHUWDLQ HQG SRLQW 5($' 7$%/(  ,172  :,7+ .(< FLW\IURP
1(: <25.
75$163257,1* 12 ),(/'6 %,1$5< 6($5&+ HQGOLQH V\WDEL[
SAP AG 2002

,QGH[FRQWUROOHG ORRS /223 $7  ,172  )520 VWDUWOLQH WR HQGOLQH  (1'/223

7R UHFDS If you program a loop through a standard table to match a specific field criterion, the runtime system always executes a WDEOH VFDQ. You can use the algorithm described here to program the runtime behavior of sorted tables: First you must sort the standard table by the desired criterion, so that you can subsequently ascertain the starting and end points in a ELQDU\ VHDUFK. (The line indexes are available in the system field sy-tabix.) Finally, you can use these values to program an index-controlled loop. Since the SORT and READ TABLE statements require additional runtime, this procedure is only useful if the loop can be repeated several times according to the field criterion. 6XPPDU\Use SORTED tables if you want to implement partial sequential loops on internal tables (where the first Q key fields are filled with "=") or use the above algorithm.

n n

SAP AG

BC401

3-37

$FFHVV 8VLQJ )LHOG 6\PEROV


5($' 7$%/( LWDE $66,*1,1* IV! :,7+ 7$%/( .(<   5($' 7$%/( IV!FRO $66,*1,1* IV! :,7+ 7$%/( .(<  
IV! IV!
ZD

5($' 7$%/( ZDFRO ,172 ZD :,7+ 7$%/( .(<  


ZD &RS\

5($' 7$%/( LWDE ,172 ZD :,7+ 7$%/( .(<  

&RS\

FRO

FRO

FRO

FRO

LWDE

FRO

FRO

FRO

FRO

SAP AG 2002

Instead of READ TABLE ... INTO, you can use the READ TABLE ... ASSIGNING variant. This offers better performance at runtime for pure read accesses with a line width greater than or equal to 1000 bytes. If you then change the read line using MODIFY, READ ... ASSIGNING already improves runtime with a line width of 100 bytes. The same applies to LOOP ... INTO in comparison with LOOP ... ASSIGNING. The LOOP ... ASSIGNING variant offers better performance at runtime for any loop of five loop passes or more. Both field symbol variants are much faster than work area variants, in particular when you use nested internal tables. This is because, if you use work areas instead, the whole inner internal table is copied (unless you prevent this by using a TRANSPORTING addition). Always assign a type to field symbols, if you know their static type (again, for performance reasons). 1RWH If you use READ TABLE ... ASSIGNING the field symbol points to the originally assigned table line, even DIWHU the internal table has been sorted. Note that when using field symbols, you cannot change key fields in SORTED or HASHED tables. Trying to do so causes a runtime error. The following restrictions apply to LOOP ... ASSIGNING <fs>: You cannot use the SUM statement in control level processing. You cannot reassign field symbols within the loop. The statements ASSIGN do TO <fs> and UNASSIGN <fs> will cause runtime errors.

n n

n n

SAP AG

BC401

3-38

8VLQJ ,QWHUQDO 7DEOHV 8QLW 6XPPDU\

l 'HILQH DQ LQWHUQDO WDEOH

<RX DUH QRZ DEOH WR

l 3HUIRUP RSHUDWLRQV RQ LQWHUQDO WDEOHV

l ,GHQWLI\ WDEOH NLQGV DQG XVH WKHP DSSURSULDWHO\ LQ GLIIHUHQW VLWXDWLRQV

SAP AG 2002

SAP AG

BC401

3-39

([HUFLVH 
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF 6LQJOH&ROXPQ 7DEOHV RI WKH 7\SH VWULQJ

At the conclusion of these exercises, you will be able to: Define single-column tables of the data type VWULQJ Split character strings with internal tables Process internal tables in loops
Now, the character string will contain PRUH WKDQ RQH data record. Extend your program in such a way that the various data records in the rows of an internal table are split. Then separate the individual data records into single components and output them as before. 3URJUDP =%&B B63/,7B,7$%

0RGHO VROXWLRQ

7HPSODWH

6$3%&B'726B63/,7B675,1*

LV \RXU WZRGLJLW JURXS QXPEHU

6$3%&B7$%6B63/,7B,7$%

1-1

Copy your solution for the exercise in the chapter 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO, =%&B B63/,7B675,1*, or the corresponding model solution 6$3%&B'726B63/,7B675,1* and give it the new name =%&B B63/,7B,7$%. Ensure that the function module %&B*(7B6(3B675,1* creates a character string consisting of 30 data records. For this you must assign a suitably typed constant (suggested name FBQXPEHU) to the parameter LPBQXPEHU. Define a single-column internal table of the type 6WDQGDUG. The column component is to have the data type VWULQJ (suggested name: LWBVHWV). Change the 63/,7 statement so that always one partial character string that contains a data record is placed into a row of its internal table. Ensure that your internal table containing the partial character strings is used for the subsequent splitting of the individual data record and output of the components in a loop.

1-2

1-3

1-4

1-5

SAP AG

BC401

3-40

([HUFLVH   2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF 7DEOH 7\SHV

At the conclusion of these exercises, you will be able to: Select the appropriate types of internal table Process data using internal tables
Now the character string will contain GXSOLFDWH data records. Extend your program in such a way that the various data records are kept sorted in internal tables. Those data records that appear twice are to be sorted into a separate internal table. 3URJUDP =%&B B7$%.,1'

0RGHO VROXWLRQ

7HPSODWH

6$3%&B7$%6B63/,7B,7$%

LV \RXU WZRGLJLW JURXS QXPEHU

6$3%&B7$%6B7$%.,1'

2-1

Copy your solution to the last exercise =%&B B63/,7B,7$% or the corresponding model solution 6$3%&B7$%6B63/,7B,7$% and give it the new name =%&B B7$%.,1'. Ensure that the function module %&B*(7B6(3B675,1* creates a character string containing duplicate data records. For this, assign the parameter LPBXQLTXH the value VSDFH. Test your program. Define an internal table in such a way that it can contain flight data sorted by airline, flight number, and flight date (suggested name LWBIOLJKWV). Insert the separated data records (contents of the structure ZDBIOLJKW) into this internal table, instead of displaying them. (You can still use the output statement later.) Make use of the return value V\VXEUF for the ,16(57 statement to ascertain whether or not a data record appears twice. For this, you must have defined the key of your internal table accordingly. Define an additional internal table for the data records that appear twice (suggested name LWBGRXEOHV). Insert the duplicate data records into this internal table. Display the contents of both internal tables with the flight data.
BC401 3-41

2-2

2-3

2-4
SAP AG

([HUFLVH   2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF 3URFHVVLQJ 'DWD 8VLQJ ,QWHUQDO 7DEOHV

At the conclusion of these exercises, you will be able to: Select the appropriate types of internal table Process data using internal tables
Extend your program so that it displays the flight data sorted by the flight date. The data is to be displayed in different colors, according to a key date selected by the user. If desired, the data is to be displayed using the SAP Grid Control. 3URJUDP =%&B B352&(66B'$7$

0RGHO VROXWLRQ

7HPSODWH

6$3%&B7$%6B7$%.,1'

LV \RXU WZRGLJLW JURXS QXPEHU

6$3%&B7$%6B352&(66B'$7$

3-1

Copy your solution to the last exercise =%&B B7$%.,1' or the corresponding model solution 6$3%&B7$%6B7$%.,1' and give it the new name =%&B B352&(66B'$7$. Define a selection screen parameter for entering the key date (suggested name SDBGDWH). Specify the default value of the key date as 30 days in the future. The user must still be able to choose the key date. Error message  of the message class EF is to be displayed if the selected key date is in the past. Make sure that the data is first displayed sorted by flight date. Ensure that the individual flight dates of the internal table LWBIOLJKWV are displayed in color according to the following criteria: Use the )250$7 &2/25 FROB statement and load the type group FRO. (You can reverse any color set using the )250$7 5(6(7 statement.) Display flights that occurred in the past with the background color col_negative. Display flights that occurred between todays data and the key date with the background color col_total.

3-2

3-3 3-4

SAP AG

BC401

3-42

Display flights that occurred in the past with the background color col_positive.

3-5

Define a selection screen check box for displaying the data with the standard tool SAP Grid Control (suggested name SDBDOY, addition $6 &+(&.%2;). Ensure that the data is only displayed in the usual ABAP list if the user does QRW check the SAP Grid Control box. Otherwise, proceed as follows: 3-5-1 Call the function module %&B$/9B/,67B287387. Terminate the program if the function module raises an exception. (In this case, there is a problem with the system configuration.)
Detailed information on using the SAP Grid Control is not part of this course. For this reason, you use a function module that encapsulates all the necessary technical details, which you can treat as a black box. For more information on the SAP Grid Control, see: The online documentation The example programs under (QYLURQPHQW ([DPSOHV &RQWURO ([DPSOHV

The training course %& $%$3 'LDORJ 3URJUDPPLQJ ZLWK (QMR\6$3 &RQWUROV

3-5-2 Find out about the types of the two internal tables that can/must be transferred to the function module as a parameter. Define two corresponding internal tables and an appropriate work area in your program (suggested names LWBFROBIOLJKWV, LWBFROBGRXEOHV, and ZDBFROBIOLJKW). 3-5-3 The line type of the internal table that you must/can transfer contains a column &2/25, in which you can set the color values FROBQHJDWLYH, FROBWRWDO, FROBSRVLWLYH, and FROBEDFNJURXQG for every single line. Copy the contents of the tables LWBIOLJKWV and LWBGRXEOHV to the internal tables LWBFROBIOLJKWV and LWBFROBGRXEOHV respectively. Use two loops to do this, and to fill the &2/25 column. LWBIOLJKWV:
/RJLFDO FRQGLWLRQ
&RORU YDOXH
fldate < sy-datum
fldate BETWEEN sydatum AND p_date
fldate > p_date

col_negative
col_total
col_positive

Always use FROBEDFNJURXQG to display LWBGRXEOHV.


SAP AG BC401 3-43

6ROXWLRQ 
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF
5(3257 TYPES: BEGIN OF st_flight_c, mandt(3) carrid(3) connid(4) fldate(8) price(20) currency(5) planetype(10) seatsmax(10) seatsocc(10) TYPE c, TYPE c, TYPE n, TYPE n, TYPE c, TYPE c, TYPE c, TYPE n, TYPE n,

6LQJOH&ROXPQ 7DEOHV RI WKH 7\SH VWULQJ

VDSEFBWDEVBVSOLWBLWDE

paymentsum(22) TYPE c, seatsmax_b(10) TYPE n, seatsocc_b(10) TYPE n, seatsmax_f(10) TYPE n, seatsocc_f(10) TYPE n, END OF st_flight_c, BEGIN OF st_flight, carrid(3) connid(4) fldate price(9) currency(5) seatsmax seatsocc TYPE c, TYPE n, TYPE d, TYPE p DECIMALS 2, TYPE c, TYPE i, TYPE i,

planetype(10) TYPE c,

END OF st_flight. &2167$176 FBQXPEHU 7<3( L 9$/8( 

SAP AG

BC401

3-44

DATA: datastring set_string TYPE string, TYPE string,

wa_flight_c TYPE st_flight_c, wa_flight '$7$ TYPE st_flight.

LWBVHWV 7<3( 67$1'$5' 7$%/( 2) VWULQJ :,7+ 12181,48( '()$8/7 .(< ,1,7,$/ 6,=( FBQXPEHU

START-OF-SELECTION. CALL FUNCTION BC401_GET_SEP_STRING EXPORTING LPBQXPEHU * * * IM_TABLE_NAME IM_SEPARATOR IM_UNIQUE IMPORTING ex_string EXCEPTIONS no_data OTHERS IF sy-subrc <> 0. MESSAGE a038(bc401). ENDIF. = 1 = 2. = datastring FBQXPEHU = SFLIGHT = # = X

SHIFT datastring BY 2 PLACES IN CHARACTER MODE. FIND ## IN datastring. IF sy-subrc <> 0. MESSAGE a702(bc401). ENDIF. 63/,7 GDWDVWULQJ $7

,172 7$%/( LWBVHWV

/223 $7 LWBVHWV ,172 VHWBVWULQJ

SAP AG

BC401

3-45

SPLIT set_string AT # INTO wa_flight_c-mandt wa_flight_c-carrid wa_flight_c-connid wa_flight_c-fldate wa_flight_c-price wa_flight_c-currency wa_flight_c-planetype wa_flight_c-seatsmax wa_flight_c-seatsocc wa_flight_c-paymentsum. MOVE-CORRESPONDING wa_flight_c TO wa_flight. WRITE: / wa_flight-carrid, wa_flight-connid, wa_flight-fldate DD/MM/YYYY, wa_flight-price CURRENCY wa_flight-currency, wa_flight-currency, wa_flight-planetype, wa_flight-seatsmax, wa_flight-seatsocc. (1'/223

SAP AG

BC401

3-46

6ROXWLRQ   2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF 7DEOH 7\SHV

5(3257 TYPES:

VDSEFBWDEVBWDENLQG

BEGIN OF st_flight_c, mandt(3) carrid(3) connid(4) fldate(8) price(20) currency(5) planetype(10) seatsmax(10) seatsocc(10) TYPE c, TYPE c, TYPE n, TYPE n, TYPE c, TYPE c, TYPE c, TYPE n, TYPE n,

paymentsum(22) TYPE c, seatsmax_b(10) TYPE n, seatsocc_b(10) TYPE n, seatsmax_f(10) TYPE n, seatsocc_f(10) TYPE n, END OF st_flight_c, BEGIN OF st_flight, carrid(3) connid(4) fldate price(9) currency(5) seatsmax seatsocc TYPE c, TYPE n, TYPE d, TYPE p DECIMALS 2, TYPE c, TYPE i, TYPE i,

planetype(10) TYPE c,

END OF st_flight. CONSTANTS c_number TYPE i VALUE 30.

SAP AG

BC401

3-47

DATA: datastring set_string TYPE string, TYPE string,

wa_flight_c TYPE st_flight_c, wa_flight DATA: it_sets TYPE STANDARD TABLE OF string WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE c_number, LWBIOLJKWV 7<3( 6257(' 7$%/( 2) VWBIOLJKW ,1,7,$/ 6,=( FBQXPEHU TYPE st_flight.

:,7+ 81,48( .(< FDUULG FRQQLG IOGDWH

LWBGRXEOHV 7<3( 6257(' 7$%/( 2) VWBIOLJKW ,1,7,$/ 6,=( FBQXPEHU

:,7+ 12181,48( .(< FDUULG FRQQLG IOGDWH

START-OF-SELECTION. CALL FUNCTION BC401_GET_SEP_STRING EXPORTING im_number * * IM_TABLE_NAME IM_SEPARATOR ,0B81,48( IMPORTING ex_string EXCEPTIONS no_data OTHERS IF sy-subrc <> 0. MESSAGE a038(bc401). ENDIF. = 1 = 2. = datastring = c_number = SFLIGHT = # VSDFH

SAP AG

BC401

3-48

SHIFT datastring BY 2 PLACES IN CHARACTER MODE. FIND ## IN datastring. IF sy-subrc <> 0. MESSAGE a702(bc401). ENDIF. SPLIT datastring AT ## INTO TABLE it_sets.

LOOP AT it_sets INTO set_string. SPLIT set_string AT # INTO wa_flight_c-mandt wa_flight_c-carrid wa_flight_c-connid wa_flight_c-fldate wa_flight_c-price wa_flight_c-currency wa_flight_c-planetype wa_flight_c-seatsmax wa_flight_c-seatsocc wa_flight_c-paymentsum. MOVE-CORRESPONDING wa_flight_c TO wa_flight. ,16(57 ZDBIOLJKW ,172 7$%/( LWBIOLJKWV ,) V\VXEUF !  (1',) ENDLOOP. ,16(57 ZDBIOLJKW ,172 7$%/( LWBGRXEOHV

* output: ********* /223 $7 LWBIOLJKWV ,172 ZDBIOLJKW :5,7(  ZDBIOLJKWFDUULG ZDBIOLJKWFRQQLG ZDBIOLJKWIOGDWH ''00<<<<

ZDBIOLJKWSULFH &855(1&< ZDBIOLJKWFXUUHQF\

SAP AG

BC401

3-49

ZDBIOLJKWFXUUHQF\ ZDBIOLJKWVHDWVPD[ (1'/223 6.,3 ZDBIOLJKWVHDWVRFF

ZDBIOLJKWSODQHW\SH

:5,7( 
GXSOLFDWH GDWD UHFRUGV
GRE &2/25 &2/B+($',1* /223 $7 LWBGRXEOHV ,172 ZDBIOLJKW :5,7(  ZDBIOLJKWFDUULG ZDBIOLJKWFRQQLG ZDBIOLJKWIOGDWH ''00<<<< ZDBIOLJKWFXUUHQF\ ZDBIOLJKWVHDWVPD[ (1'/223 ZDBIOLJKWVHDWVRFF

ZDBIOLJKWSULFH &855(1&< ZDBIOLJKWFXUUHQF\ ZDBIOLJKWSODQHW\SH

SAP AG

BC401

3-50

6ROXWLRQ   2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV 7RSLF 3URFHVVLQJ 'DWD 8VLQJ ,QWHUQDO 7DEOHV

5(3257 VDSEFBWDEVBSURFHVVBGDWD TYPE-POOLS col. TYPES: BEGIN OF st_flight_c, mandt(3) carrid(3) connid(4) fldate(8) price(20) currency(5) planetype(10) seatsmax(10) seatsocc(10) TYPE c, TYPE c, TYPE n, TYPE n, TYPE c, TYPE c, TYPE c, TYPE n, TYPE n,

paymentsum(22) TYPE c, seatsmax_b(10) TYPE n, seatsocc_b(10) TYPE n, seatsmax_f(10) TYPE n, seatsocc_f(10) TYPE n, END OF st_flight_c, BEGIN OF st_flight, carrid(3) connid(4) fldate price(9) currency(5) seatsmax seatsocc TYPE c, TYPE n, TYPE d, TYPE p DECIMALS 2, TYPE c, TYPE i, TYPE i,

planetype(10) TYPE c,

END OF st_flight.

SAP AG

BC401

3-51

CONSTANTS c_number TYPE i VALUE 30. DATA: datastring set_string TYPE string, TYPE string,

wa_flight_c TYPE st_flight_c, wa_flight DATA: it_sets TYPE STANDARD TABLE OF string WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE c_number, it_flights TYPE SORTED TABLE OF st_flight WITH UNIQUE KEY fldate carrid connid INITIAL SIZE c_number, it_doubles TYPE SORTED TABLE OF st_flight WITH NON-UNIQUE KEY fldate carrid connid INITIAL SIZE c_number, TYPE st_flight.

it_col_flights TYPE bc401_t_flights_color, it_col_doubles LIKE it_col_flights, wa_col_flight LIKE LINE OF it_col_flights. 3$5$0(7(56 SDBDOY

SDBGDWH /,.( V\GDWXP

$6 &+(&.%2; '()$8/7
;


/2$'2)352*5$0 SDBGDWH

V\GDWXP  

$7 6(/(&7,216&5((1

,) SDBGDWH  V\GDWXP (1',) 0(66$*( H EF 

GDWH LQ WKH SDVW

SAP AG

BC401

3-52

START-OF-SELECTION. CALL FUNCTION BC401_GET_SEP_STRING EXPORTING im_number * * IM_TABLE_NAME IM_SEPARATOR im_unique IMPORTING ex_string EXCEPTIONS no_data OTHERS IF sy-subrc <> 0. MESSAGE a038(bc401). ENDIF. = 1 = 2. = datastring = c_number = SFLIGHT = # = space

SHIFT datastring BY 2 PLACES IN CHARACTER MODE. FIND ## IN datastring. IF sy-subrc <> 0. MESSAGE a702(bc401). ENDIF. SPLIT datastring AT ## INTO TABLE it_sets.

LOOP AT it_sets INTO set_string. SPLIT set_string AT # INTO wa_flight_c-mandt wa_flight_c-carrid wa_flight_c-connid wa_flight_c-fldate wa_flight_c-price wa_flight_c-currency wa_flight_c-planetype wa_flight_c-seatsmax wa_flight_c-seatsocc wa_flight_c-paymentsum.

SAP AG

BC401

3-53

MOVE-CORRESPONDING wa_flight_c TO wa_flight. INSERT wa_flight INTO TABLE it_flights. IF sy-subrc <> 0. INSERT wa_flight INTO TABLE it_doubles. ENDIF. ENDLOOP.

* output: ********* ,) SDBDOY


;


/223 $7 LWBIOLJKWV ,172 ZDBIOLJKW

029(&255(6321',1* ZDBIOLJKW 72 ZDBFROBIOLJKW ,) ZDBFROBIOLJKWIOGDWH  V\GDWXP ZDBFROBIOLJKWFRORU ZDBFROBIOLJKWFRORU ZDBFROBIOLJKWFRORU FROBQHJDWLYH FROBWRWDO

(/6(,) ZDBFROBIOLJKWIOGDWH  SDBGDWH (/6(

(1',) (1'/223

FROBSRVLWLYH

,16(57 ZDBFROBIOLJKW ,172 7$%/( LWBFROBIOLJKWV

/223 $7 LWBGRXEOHV ,172 ZDBIOLJKW ZDBFROBIOLJKWFRORU (1'/223

029(&255(6321',1* ZDBIOLJKW 72 ZDBFROBIOLJKW ,16(57 ZDBFROBIOLJKW ,172 7$%/( LWBFROBGRXEOHV FROBEDFNJURXQG

&$// )81&7,21
%&B$/9B/,67B287387
(;3257,1* LWBOLVW LWBOLVW FRQWUROBHUURU 27+(56 LWBFROBIOLJKWV LWBFROBGRXEOHV 

(;&(37,216

,) V\VXEUF !  (1',)



0(66$*( D EF 

SAP AG

BC401

3-54

(/6( LOOP AT it_flights INTO wa_flight. ,) ZDBIOLJKWIOGDWH  V\GDWXP (/6(,) ZDBIOLJKWIOGDWH  SDBGDWH (/6( )250$7 &2/25 )250$7 &2/25 FROBWRWDO )250$7 &2/25 FROBQHJDWLYH

(1',)

FROBSRVLWLYH

WRITE: / wa_flight-carrid, wa_flight-connid, wa_flight-fldate DD/MM/YYYY, wa_flight-price CURRENCY wa_flight-currency, wa_flight-currency, wa_flight-planetype, wa_flight-seatsmax, wa_flight-seatsocc. ENDLOOP. )250$7 5(6(7 SKIP. WRITE: / duplicate data records:(dob) COLOR COL_HEADING. LOOP AT it_doubles INTO wa_flight. WRITE: / wa_flight-carrid, wa_flight-connid, wa_flight-fldate DD/MM/YYYY, wa_flight-price CURRENCY wa_flight-currency, wa_flight-currency, wa_flight-planetype, wa_flight-seatsmax, wa_flight-seatsocc. ENDLOOP.

SAP AG

BC401

3-55

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD &RQWHQWV


l 7HFKQLTXHV IRU FDOOLQJ SURJUDPV l 0HPRU\ PRGHO l 8VH l 7HFKQLTXHV IRU SDVVLQJ GDWD

SAP AG 2002

SAP AG

BC401

4-1

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HVFULEH WKH 6$3 5 PHPRU\ PRGHO l &DOO H[HFXWDEOH SURJUDPV l &DOO WUDQVDFWLRQV

l 8VH WKH YDULRXV PHPRU\ DUHDV WR SDVV GDWD

SAP AG 2002

SAP AG

BC401

4-2

Y X VU T c b 7X (a U X a `(W(SR ## !"   $        & 95 B(8 A 9@() 75 1) 31) (% 8 6 4 2 0 '&

9' 84 5 C Q H ') & 4 @5 IPB 48 8 C D FG) 8 & 5 E7@A 48 8 C

SAP AG
SAP AG 2002

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

4-3

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD 

&DOOLQJ SURJUDPV &DOOLQJ SURJUDPV


0HPRU\ PDQDJHPHQW 0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD 3DVVLQJ GDWD

SAP AG 2002

SAP AG

BC401

4-4

&DOOLQJ 3URJUDPV

7LPH

0DLQ PHPRU\

0DLQ PHPRU\

1
,QVHUW SURJUDP

1
,QVHUWLRQ
5HVWDUW

1HZ SURJUDP

2
(QG LQVHUW




3URJUDP  3URJUDP 

SAP AG 2002

There are two ways of starting an ABAP program from another ABAP program that is already running: The called program is inserted, that is the current program is interrupted to run the new one. The called program is executed, and afterwards, processing returns to the program that called it. The current program is terminated and the called program is started.

Complete ABAP programs within a single user session can only run sequentially. We refer to this technique as VHTXHQWLDO FDOOLQJ. If you want to run functions in parallel, you must use function modules. For further information about this technique, refer to the course %& &RPPXQLFDWLRQ ,QWHUIDFHV LQ $%$3 or the documentation for the &$// )81&7,21  67$57,1* 1(: 7$6.  statement.

SAP AG

BC401

4-5

&DOOLQJ DQ ([HFXWDEOH 3URJUDP


3URJUDP 
 68%0,7 SURJBQDPHB 
5HVWDUW

3URJUDP 

SURJBQDPHB
352*5$0  

/LVW

 68%0,7 SURJBQDPHB $1' 5(7851 

,QVHUWLRQ

)
/LVW

SURJBQDPHB
352*5$0  

,QVHUWLRQ  68%0,7 SURJBQDPHB 9,$ 6(/(&7,216&5((1 $1' 5(7851 


SAP AG 2002

6HOHFWLRQ 6FUHHQ

)

SURJBQDPHB
352*5$0  

/LVWH

)

)

To start an executable program, use the 68%0,7 statement. If you use the 9,$ 6(/(&7,216&5((1 addition, the system displays the standard selection screen of the program (if one has been defined). If you use the $1' 5(7851 addition, the system resumes processing with the first statement after the 68%0,7 statement once the called program has finished. For further information, refer to the documentation for the 68%0,7 statement.

SAP AG

BC401

4-6

&DOOLQJ D 7UDQVDFWLRQ
3URJUDP 
 /($9( 72 75$16$&7,21 T_CODE >$1' 6.,3 ),567 6&5((1@ 
5HVWDUW

3URJUDP  7UDQVDFWLRQ

TCODE

 6FUHHQ

6$30 WBQDPH
 /($9( 352*5$0 

 6FUHHQ
)

,QVHUWLRQ  &$// 75$16$&7,21 T_CODE >$1' 6.,3 ),567 6&5((1@ 

 6FUHHQ

6$30 WBQDPH
 /($9( 352*5$0 

 6FUHHQ
)

SAP AG 2002

With the /($9( 72 75$16$&7,21


T_CODE
statement you terminate the current program and start the transaction with transaction code 7B&2'( The statement is the equivalent to entering Q<T_CODE> in the command field. &$// 75$16$&7,21 T_CODE allows you to insert ABAP programs that have a transaction code. To terminate an ABAP program, use the /($9( 352*5$0 statement. If the statement is used in a program that you called using &$// 75$16$&7,21 T_CODE or 68%0,7 prog_name $1' 5(7851, the system resumes processing at the next statement after the call in the calling program. Otherwise, the user returns to the application menu from which he or she started the program. If you use the  $1' 6.,3 ),567 6&5((1 addition, the system does QRW display the VFUHHQ FRQWHQWV of the first screen. However, it does process the flow logic. If the transaction T_CODE you called with &$// 75$16$&7,21 uses update techniques, you can use the 83'$7( addition to specify the update technique (asynchronous (default), synchronous, or local) that the program should use. For further information, refer to course %& 3URJUDPPLQJ 'DWDEDVH 8SGDWHV and the online documentation.

SAP AG

BC401

4-7

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD 

&DOOLQJ SURJUDPV &DOOLQJ SURJUDPV


0HPRU\ PDQDJHPHQW 0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD 3DVVLQJ GDWD

SAP AG 2002

SAP AG

BC401

4-8

/RJLFDO 0HPRU\ 0RGHO


8VHU WHUPLQDO VHVVLRQ

([WHUQDO VHVVLRQ ZLQGRZ 


,QWHUQDO VHVVLRQ 

([WHUQDO VHVVLRQ ZLQGRZ 

$%$3 PHPRU\ 

3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

$%$3 PHPRU\ 

,QWHUQDO VHVVLRQ 


3URJUDP 

6$3 PHPRU\
SAP AG 2002

The way in which the main memory is organized from the programs point of view can be represented in the above logical model. There is a distinction between internal and external sessions: Generally, an H[WHUQDO VHVVLRQ is connected to an R/3 window. You can create a new session by choosing 6\VWHP 1HZ 6HVVLRQ or by entering R<T_CODE> in the command field. You can have up to six external sessions open simultaneously in one terminal session. External sessions are subdivided into LQWHUQDO VHVVLRQV (placed on a VWDFN). Each program that you run occupies its own internal session. Each external session can contain up to nine internal sessions.

Data for a program is RQO\ YLVLEOH ZLWKLQ an internal session. The visibility of the data is generally restricted to the relevant program. The following slides illustrate how the stack inside an external session changes with various program calls.

SAP AG

BC401

4-9

,QVHUWLQJ D 3URJUDP 
User terminal session

External session (window) 1

External session (window) 2

ABAP memory 1

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

SAP AG

BC401

4-10

,QVHUWLQJ D 3URJUDP 
User terminal session
,QVHUWLRQ

External session (window) 1


,QWHUQDO VHVVLRQ 

External session (window) 2

ABAP memory 1

3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

When you insert a program, the system creates a new internal session, which in turn creates a new program context. The new session is placed on the stack The program context of the calling program also remains intact.

SAP AG

BC401

4-11

7HUPLQDWLQJ WKH ,QVHUWHG 3URJUDP


User terminal session

External session (window) 1

External session (window) 2

ABAP memory 1

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

When the called (inserted) program finishes, its internal session (the top one in the stack) is deleted. Processing is resumed in the next-highest internal session in the stack.

SAP AG

BC401

4-12

5HVWDUWLQJ DQ ([HFXWDEOH 3URJUDP 


User terminal session

External session (window) 1


,QWHUQDO VHVVLRQ 
3URJUDP 

External session (window) 2

ABAP memory 1

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

,QWHUQDO VHVVLRQ 


3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

When you end a program and start a new one, there is a distinction between calling an executable program and calling a transaction, with regard to memory areas.

SAP AG

BC401

4-13

5HVWDUWLQJ DQ ([HFXWDEOH 3URJUDP 


User terminal session
5HVWDUW session (window) 1 External

External session (window) 2

,QWHUQDO VHVVLRQ 

ABAP memory 1

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

3URJUDPP 

,QWHUQDO VHVVLRQ 


3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

If you call an executable program using its program name (terminating the calling program), the system deletes the internal session of the program that you are terminating (the top one from the stack). The system creates a new internal session, which in turn creates the program context of the called program. The new session is placed on the stack Existing program contexts remain intact. The topmost internal session on the stack is replaced.

SAP AG

BC401

4-14

5HVWDUWLQJ D 7UDQVDFWLRQ 
User terminal session

([WHUQDO VHVVLRQ ZLQGRZ 


,QWHUQDO VHVVLRQ 

External session (window) 2

$%$3 PHPRU\ 

,QWHUQDO VHVVLRQ 


3URJUDP 

ABAP memory 2

3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

SAP memory
SAP AG 2002

SAP AG

BC401

4-15

5HVWDUWLQJ D 7UDQVDFWLRQ 
User terminal session

([WHUQDO VHVVLRQ ZLQGRZ 

External session (window) 2

$%$3 PHPRU\ 

ABAP memory 2

,QWHUQHU 0RGXV 

3URJUDP 


,QWHUQDO VHVVLRQ 


3URJUDP 

5HVWDUW FRPSOHWH LQLWLDOL]DWLRQ


SAP AG 2002

SAP memory

If you start a program using its WUDQVDFWLRQ FRGH (that is, if one was defined), DOO of the internal sessions on the stack are deleted. The system creates a new internal session, which in turn creates the program context of the called program. After the call, the ABAP memory is LQLWLDOL]HG.

SAP AG

BC401

4-16

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD 

&DOOLQJ SURJUDPV &DOOLQJ SURJUDPV


0HPRU\ PDQDJHPHQW 0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD 3DVVLQJ GDWD

SAP AG 2002

SAP AG

BC401

4-17

2YHUYLHZ 3DVVLQJ 'DWD %HWZHHQ 3URJUDPV

6$3 PHPRU\ 6(7*(7 SDUDPHWHUV

3
$%$3 PHPRU\

3URJUDP $

,QWHUIDFH 1 4

3URJUDP %

DB
5
SAP AG 2002

There are various ways of passing data to programs running in separate internal sessions:

The interface of the called program (usually a standard selection screen) ABAP memory SAP memory

You can use:

Database tables

Local files on your presentation server

The following slides deal with the first three of these methods. For further information regarding the passing of data using database tables or the VKDUHG EXIIHU, refer to the documentation for the (;3257 and ,03257 statements. For further information on transferring data between an ABAP program and a presentation server, refer to the documentation for the function modules *8,B83/2$' and *8,B'2:1/2$'.

SAP AG

BC401

4-18

3DVVLQJ 'DWD 8VLQJ WKH 3URJUDP ,QWHUIDFH

6WDQGDUG VHOHFWLRQ VFUHHQ

SAP AG 2002

When you call ABAP programs that have a standard selection screen, you can pass data for the input fields in the call. There are two ways to do this: By specifying a variant for the selection screen when you call the program By specifying values for the input fields when you call the program

SAP AG

Data

3URJUDP $

3URJUDP %

BC401

4-19

3UHDVVLJQLQJ 9DOXHV IRU ,QSXW )LHOGV

'$7$ VHW 7<3(_/,.( 5$1*( 2) W\SH_GDWDREMHFW` 68%0,7 SURJBQDPH $1' 5(7851 >9,$ 6(/(&7,216&5((1@ :,7+ SDUDPHWHU (4_1(_ YDO :,7+ VHOBRSW (4_1(_ YDO 6,*1
,
_
(
` :,7+ VHOBRSW %(7:((1 YDO $1' YDO 6,*1
,
_
(
` :,7+ VHOBRSW 127 %(7:((1 YDO $1' YDO 6,*1
,
_
(
` :,7+ VHOBRSW ,1 VHW  
SAP AG 2002

The :,7+ addition to the 68%0,7 statement allows you to preassign values for parameters and selection options on a standard selection screen of the called executable program. The abbreviations "(4, 1(, ...; ,, (" have the same meanings as with selection options. If you want to pass several selections for a selection option, you can use the 5$1*( statement instead of individual :,7+ additions. The RANGES statement creates a selection table, which you can fill as though it were a selection option. You then pass the whole table to the executable program. If you want to display the standard selection screen when you call the program, use the 9,$ 6(/(&7,216&5((1 addition. When you use the 68%0,7 statement, use the Pattern function in the ABAP Editor to insert an appropriate statement pattern for the program you want to call. It automatically supplies the names of the parameters and selection options that are available on the standard selection screen. For further information about working with variants and about other syntax variants of the :,7+ addition, refer to the documentation for the 68%0,7 statement.

SAP AG

BC401

(G

 y @ 1x y y

wPPw w wPPw w ww PPw


4-20

3DWWHUQ IRU 68%0,7 VWDWHPHQW LQVHUWV WKH DSSURSULDWH IUDPH IRU WKH SURJUDP FDOO LQWR WKH VRXUFH FRGH

e@ivuht$sq`$hfd p qq r p i g e

$%$3 0HPRU\ DQG 6$3 0HPRU\


8VHU WHUPLQDO VHVVLRQ

([WHUQDO VHVVLRQ ZLQGRZ 

([WHUQDO VHVVLRQ ZLQGRZ 

$%$3 PHPRU\ 

3URJUDP 

$%$3 PHPRU\ 

,QWHUQDO VHVVLRQ 

,QWHUQDO VHVVLRQ 


3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

,QWHUQDO VHVVLRQ 


3URJUDP 

6$3 PHPRU\ 6(7 *(7 SDUDPHWHUV


SAP AG 2002

You can use SAP memory and ABAP memory to pass data between programs. 6$3 PHPRU\ is a user-specific memory area for storing ILHOG YDOXHV. It is only of limited value for passing data between internal sessions. Values in SAP memory are retained for the duration of the users terminal session. The memory can be used between sessions in the same terminal session. You can use the contents of SAP memory as default values for screen fields. All H[WHUQDO VHVVLRQV can use the SAP memory. $%$3 PHPRU\ is also user-specific. There is a local ABAP memory for each external session. You can use it to exchange DQ\ $%$3 YDULDEOHV (fields, structures, internal tables, complex objects) between the LQWHUQDO sessions in any one external session. When the user exits an external session (L in the command field), the corresponding ABAP memory is automatically initialized or released.

SAP AG

BC401

4-21

3DVVLQJ 'DWD 8VLQJ WKH $%$3 0HPRU\


,QWHUQDO VHVVLRQ 

$%$3 PHPRU\
ZDBIOL

0<B,'

352*5$0 S   '$7$ SBVSIOL 7<3( VSIOL LWBVSIOL 7<3( 67$1'$5' 7$%/( 2) VSIOL 

LWBVSIOL

(;3257 ZDBIOL )520 SBVSIOL ZDB LWBVSIOL 72 0(025< ,'


0<B,'

,QWHUQDO VHVVLRQ 

0<B,'



352*5$0 S   '$7$ SBVSIOL 7<3( VSIOL LWBVSIOL 7<3( 67$1'$5' 7$%/( 2) VSIOL  ,03257

,03257 ZDBIOL 72 SBVSIOL ZDB LWBVSIOL )520 0(025< ,'


0<B,'


SAP AG 2002

The (;3257  72 0(025< statement allows you to copy any number of ABAP data objects with their current values to the ABAP memory (data cluster). The ,'  addition enables you to identify different clusters (maximum of 60 characters). If you use a new (;3257 72 0(025< statement for an existing data cluster, the new one will overwrite the old. The ,03257  )520 0(025< ,'  statement allows you to copy the data from the ABAP memory into corresponding data objects of your ABAP program. It is also possible to only import parts of data clusters using ,03257. The data objects that are to receive the data from the ABAP memory cluster must have the same types in both the calling and the called programs. To release a data cluster, use the )5(( 0(025< ,'  statement. Bear in mind that when you call programs using transaction codes, you can RQO\ use the ABAP memory to pass data ZKHQ LQVHUWLQJ (&$// 75$16$&7,21).

SAP AG

BC401

4-22

3DVVLQJ 3DUDPHWHUV 8VLQJ WKH 6$3 0HPRU\

Program A
$LUOLQH &RQQHFWLRQ 

/+

RU
6(7

6(7 3$5$0(7(5 ,'


&21
),(/' VG\QBFRQQFRQQLG &$// 75$16$&7,21  

&$5 &21
Program B
&RQQHFWLRQ 

6$3 PHPRU\


/+

SAP AG 2002

You can define memory areas (parameters) in the SAP memory in various ways: By creating input/output fields with reference to the ABAP Dictionary. These take the parameter name of the data element to which they refer. Alternatively, you can enter a name in the attributes of the input/output fields. Here, you can also choose whether the entries from the field should be transferred to the parameter (SET), or whether the input field should be filled with the value from the parameter (GET). To find out about the names of the parameters assigned to input fields, display the field help for the field (F1), then choose 7HFKQLFDO LQIR. You can also fill the memory areas directly using the 6(7 3$5$0(7(5 ,' PAR_ID ),(/' var statement and read them using *(7 3$5$0(7(5 ,'
PAR_ID
),(/' var Finally, you can define parameters in the 2EMHFW 1DYLJDWRU and let the user fill them with values.

SAP AG

$LUOLQH

/+

*(7

RU

*(7 3$5$0(7(5 ,'


&21
),(/' VG\QBFRQQFRQQLG

BC401

4-23

3UHYLHZ 3DVVLQJ 'DWD 8VLQJ DQ ,QWHUQDO 7DEOH


3URJUDP 
 '$7$ ELBLWDE 7<3( 7$%/( 2) EGFGDWD ELBZD 7<3( EGFGDWD ILOO ELBLWDE  FDOO RWKHU SURJUDP &$// 75$16$&7,21
7B&2'(
86,1* ELBLWDE ,) V\VXEUF   (/6( 

3URJUDP  7UDQVDFWLRQ

T_CODE

ELBLWDE

VW VFUHHQ

QG VFUHHQ
)

6$30 WBQDPH
 /($9( 352*5$0 

SAP AG 2002

The &$// 75$16$&7,21


T_CODE
86,1* bi_itab statement allows you to insert the transaction T_CODE, and the screens are processed according to the internal table bi_itab. 7KLV LQWHUQDO WDEOH PXVW EH W\SHG DFFRUGLQJ WR WKH VWUXFWXUH EGFGDWD DQG ILOOHG DSSURSULDWHO\ The 02'( addition allows you to specify whether the screen contents should all be displayed (A - the default setting), only when an error occurs (E), or not at all (N). The 0(66$*(6 ,172 mess_itab addition is used to specify where the system messages sent during the execution of the called transaction are written. 7KH LQWHUQDO WDEOH PXVW EH W\SHG DFFRUGLQJ WR WKH VWUXFWXUH EGFPVJFROO You can find out if the transaction was executed successfully from the system field V\VXEUF. This technique is useful if, for example: You are processing in the foreground, but the input fields have not been filled using GET parameters You want to process the transaction in the background. In this case, you normally have to pass function codes as well.

This technique is also RQH of the possible ways of transferring data from non-SAP systems. To do so the internal table in the EGFGDWD format must be filled completely.

SAP AG

BC401

4-24

)LHOGV RI WKH *OREDO 7\SH EGFGDWD

)LHOG QDPH
/HQJWK

SURJUDP


G\QSUR


G\QEHJLQ


IQDP


IYDO


SAP AG 2002

Filling the internal table in batch input format: Each screen that is to be processed and filled automatically in the transaction must be identified by a line, in which only the fields SURJUDP, G\QSUR and G\QEHJLQ are filled. After the record that identifies the screen, use a separate EGFGDWD record for each field you want to fill. These records use the table fields IQDP and IYDO. The following fields can be filled:
-

Input/output fields, with data The command field (EGFBRNFRGH), with function codes The cursor position field (EGFBFXUVRU), with field names.

You also use the &$// 75$16$&7,21 technique to transfer GDWD IURP H[WHUQDO V\VWHPV. Further information on this topic is available in the course %& 'DWD 7UDQVIHU and in the online documentation.

SAP AG

e i ip g e pp o i `hneo$ssqhnp q gm k i xe xj e h
BC401

e ip o g pp o i `ihn`$serqh$np qle lj e h g m k i

1RWH ZKHQ ILOOLQJ

Wt$gp `i `y i vi z i k i k k g e i ez q hg i g $$t


4-25

i t e i k gd$exp `i `y

{ {$g P7e i k p i q e i ip e`neo$g e pp o i rqnrhnp q g m p ~ { $} {|

pp ip gpk no$sq$f`y

p i w d v e e i ip xeu`o t

i d t e d tp p g`fevs

'HVFULSWLRQ

([DPSOH 3DVVLQJ 'DWD 8VLQJ DQ ,QWHUQDO 7DEOH


SURJUDP

G\QSUR

G\QEHJLQ

IQDP

IYDO

'$7$ EGFGDWD ZDBEGFGDWD 7<3( EGFGDWD LWBEGFGDWD /,.( 7$%/( 2) ZDBEGFGDWD ILOO WKH EGFGDWDWDEOH  &$// 75$16$&7,21
%&B&$/'B&5(B&867
86,1* LWBEGFGDWD 02'(
1
 ,) V\VXEUF !  0(66$*(  :,7+ V\VXEUF (1',)
SAP AG 2002

,' IRU FRPPDQG ILHOG

This example refers to the transaction %&B&$/'B&7$B8. If you request the creation of a new customer entry here, the transaction %&B&$/'B&5(B&867 is inserted. This transaction has not implemented an import from the ABAP memory, and its input fields are not set as *(7 parameters. The customer data is therefore passed using an internal table and the transaction processed in the background. If the operation is successful, the new customer data record can be entered in the waiting list.

The relevant internal table in EGFGDWD format is shown above. current_name is the customer name adopted from the input field at runtime, current_city is the city.
You address the command field using %'&B2.&2'(. Here you enter the function code that is triggered by the user choosing a function key, pushbutton, or menu entry during the dialog flow (or by entering a code directly in the command field).

SAP AG

BC401

3h`r`e`rr`$ `$
ee`hr`h`
6DYH

r`

3`r 3`

4-26

`s`hes$`eh `rh

&DOOLQJ 3URJUDPV DQG 3DVVLQJ 'DWD 8QLW 6XPPDU\

l 'HVFULEH WKH 6$3 5 PHPRU\ PRGHO l &DOO H[HFXWDEOH SURJUDPV l &DOO WUDQVDFWLRQV

<RX DUH QRZ DEOH WR

l 8VH WKH YDULRXV PHPRU\ DUHDV WR SDVV GDWD

SAP AG 2002

SAP AG

BC401

4-27

,QWURGXFWLRQ WR 2EMHFW2ULHQWHG 3URJUDPPLQJ &RQWHQWV


l 3URFHGXUDO SURJUDPPLQJ l 2EMHFWRULHQWHG SURJUDPPLQJ

l $LPV RI WKH $%$3 2EMHFWV SURJUDPPLQJ ODQJXDJH

SAP AG 2002

SAP AG

BC401

5-1

,QWURGXFWLRQ WR 2EMHFW2ULHQWHG 3URJUDPPLQJ 8QLW 2EMHFWLYHV


$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 1DPH WKH PRVW VLJQLILFDQW GLIIHUHQFHV EHWZHHQ SURFHGXUDO DQG REMHFWRULHQWHG SURJUDPPLQJ l 6WDWH WKH DLPV EHKLQG GHYHORSLQJ WKH $%$3 2EMHFWV SURJUDPPLQJ ODQJXDJH

SAP AG 2002

SAP AG

BC401

5-2

y w % 6 6 qxv f S a d #c e W S d c b ` 6#V %a @V XY@V U#TR 0 11 0 2 11 () '  "" & & "   % $$  "  #!

d U c` a g r u stf c ` c g V U S ` 6a c` c g h pqV c S a i%6e     

G G 4 A I8 4 P 4 Q64 B9 8 7 D C A 9 EB@8 7 5 564 G F H64 C 9 3 ( 

SAP AG
SAP AG 2002

,QWURGXFWLRQ WR 2EMHFW2ULHQWHG 3URJUDPPLQJ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

5-3

+LVWRU\ RI 3URJUDPPLQJ /DQJXDJHV

Machine language
Assembler

$%$3 2EMHFWV

SAP AG 2002

n n

Before ABAP, SAP used a macro assembler. $%$3 was created with the intention of improving reporting. ABAP developed almost independently as an in-house programming language, although it was influenced by other programming languages, for example, COBOL and PASCAL. $%$3 2EMHFWV is a true extension of ABAP. ABAP Objects unites the most promising aspects of other object-oriented programming languages, such as Java, C++, and Smalltalk.

SAP AG

BC401

d B

5-4

,QWURGXFWLRQ 3URJUDPPLQJ 0RGHOV 

3URFHGXUDO SURJUDPPLQJ 3URFHGXUDO SURJUDPPLQJ


2EMHFWRULHQWHG SURJUDPPLQJ 2EMHFWRULHQWHG SURJUDPPLQJ

SAP AG 2002

SAP AG

BC401

5-5

&KDUDFWHULVWLFV RI 3URFHGXUDO 3URJUDPPLQJ


&KDUDFWHULVWLFV
l l l

SAP AG 2002

Information systems were previously defined primarily by their functions: Data and functions were stored separately and linked using input-output relationships.

SAP AG

l T n %ql lqk Tp p o o mqqj j l n m k

l o Enqm @l @k @ p p rnqqqj j l o m l k

f ig f hHe
l o n nqql qk Tp p o r%mqqj j l m l k

l  n qm ql qk Tp p o or%qqj j l n m l k

he f g f

l o oEqqqk p p rnqmqlqj j l n m l k

f ig f H he
lp p Eqlqj j T o o n m mqlqk l n k

f hg ifHe

Tp p o o @qj j l T n %m ql lqk l n m k

f ig f H he
Tp p o E%qlqj j l T o n m qqk l n m l k

6HSDUDWLRQ RI IXQFWLRQV DQG GDWD 3RVVLELOLW\ RI HQFDSVXODWLQJ IXQFWLRQV XVLQJ PRGXODUL]DWLRQ

'LUHFW DFFHVV WR GDWD SRVVLEOH  YLVLELOLW\"

BC401

5-6

7\SLFDO $%$3 3URJUDP


5(3257 =$%$3B'(02 5(3257 =$%$3B'(02   7<3(6  7<3(6  '$7$  '$7$    3(5)250 IRUP  3(5)250 IRUP  &$// )81&7,21
)%
&$// )81&7,21
)%
  &$// )81&7,21
)%
&$// )81&7,21
)%
    )250 I  )250 I    (1')250 (1')250

l l l

7\SH GHILQLWLRQV 'DWD GHFODUDWLRQV 0DLQ SURJUDP

&DOOLQJ VXEURXWLQHV &DOOLQJ IXQFWLRQ PRGXOHV

'HILQLWLRQ RI VXEURXWLQHV

SAP AG 2002

n n

A typical ABAP program consists of type definitions and data declarations, which describe the blueprint of the data the program uses when it is executed. To make your program more readable and for better program structure, it is recommended that you work with modularization units (encapsulated units with functions), such as form routines or function modules. These components can be reused in many different programs.

SAP AG

BC401

5-7

$%$3 0DLQ 0HPRU\ DQG )XQFWLRQ *URXSV


,QWHUQDO VHVVLRQ
 vytE| v y ti{x!}i2{Bxytus |  ~ w v x | x z t w v t
Interface Interface

)XQFWLRQ JURXS 
Exx!! &

)XQFWLRQ JURXS 
!x!! Y

SAP AG 2002

n n n

This slide provides an overview of the main program components during program execution in an internal session (main memory area on application server). The loaded main program and the two function groups with their encapsulated modules (the function modules) are both in separate memory areas. From the main program, you can use function modules to access function group components, for example their global data. Therefore, a function group is a unit consisting of data and functions that manage this data. A user "client" (here, the main program) can only access the function groups and their services using the interface, that is the function modules. The function group acts as a "server", because it provides services.

SAP AG

BC401

2 x

2 x

 g

 g

 g

g

T g

g txY

@yt{x x v

5-8

)XQFWLRQ *URXS ([DPSOH


@yHu2y

6B9(+,&/(
VSHHG
q

 
SAP AG 2002

n n

The function group s_vehicle provides a user or client with the services inc_speed, dec_speed, and get_speed. These services make up the function group interface and access the internally encapsulated component speed.

SAP AG

BC401

 g

)81&7,21 *(7B63((' )81&7,21 *(7B63((' H[SBVSHHG VSHHG H[SBVSHHG VSHHG (1')81&7,21 (1')81&7,21

x@

g !

)81&7,21 '(&B63((' )81&7,21 '(&B63(('   68%75$&7 ,03B63((' IURP VSHHG 68%75$&7 ,03B63((' IURP VSHHG (1')81&7,21 (1')81&7,21

22

VSHHG LV D JOREDO YDULDEOH VSHHG LV D JOREDO YDULDEOH XVHG LQ WKH IXQWLRQSRRO XVHG LQ WKH IXQWLRQSRRO '$7$ VSHHG 7<3( , '$7$ VSHHG 7<3( ,   )81&7,21 ,1&B63((' )81&7,21 ,1&B63(('   $'' LPSBVSHHG 72 VSHHG $'' LPSBVSHHG 72 VSHHG (1')81&7,21 (1')81&7,21

qy

uy

)81&7,21322/ VBYHKLFOH )81&7,21322/ VBYHKLFOH

5-9

8VH RI )XQFWLRQ *URXS


5(3257 ]YHKLFOHGHPR 5(3257 ]YHKLFOHGHPR 7<3(6  7<3(6  '$7$ ZDBFDU 7<3(  '$7$ ZDBFDU 7<3(  QR GLUHFW DFFHVV WR VSHHG QR GLUHFW DFFHVV WR VSHHG XVH IXQFWLRQV RI SRRO XVH IXQFWLRQV RI SRRO &$// )81&7,21
,1&B63(('
&$// )81&7,21
,1&B63(('
  &$// )81&7,21
*(7B63(('
&$// )81&7,21
*(7B63(('
  &$// )81&7,21
6723
&$// )81&7,21
6723
 
VSHHG
q

6B9(+,&/(

SAP AG 2002

The main program cannot access speed directly.

SAP AG

BC401

B

yy@2y

Y@y

x@

g !

g

uH

y

5-10

g

g T!!

Txq

q

@@

yy

y B@B@uY 

qyyY }Y u

6HYHUDO ,QVWDQFHV RI 2QH )XQFWLRQ *URXS

6B9(+,&/(

y

SAP AG

If the main program is to work with several vehicles, this is not possible without extra programming and administration effort.

VSHHG

SAP AG 2002

BC401

5-11

,QVWDQWLDWLRQ DQG 2EMHFW2ULHQWHG /DQJXDJHV

VSHHG
VSHHG
VSHHG

SAP AG 2002

The ability to create multiple instances of a "class", such as a vehicle, is one of the central attributes of object-oriented languages.

SAP AG

BC401

q&y!



!@

VSHHG

Y B

@uqu&u@ y

qY2

@uq@y H Yy  Y

5-12

,QWURGXFWLRQ 3URJUDPPLQJ 0RGHOV 

3URFHGXUDO SURJUDPPLQJ 3URFHGXUDO SURJUDPPLQJ


2EMHFWRULHQWHG SURJUDPPLQJ 2EMHFWRULHQWHG SURJUDPPLQJ

SAP AG 2002

SAP AG

BC401

5-13

l o n m l k 6Tp r#@@j %p rqqqj l o n m l k %Tp o qqj l n m l k Ex n

l T n %m qlqk Tp p o o #l@j j l n m k

Tp p o rm@qj j l l o n qql qk n m l k

Tp p o o @qj j l T n %m ql qk l n m l k

p p Enq@@j j l o E%m #l @k @ l o n m l k

l o En m ql @k @ p p r#@j j l o n m l k
l n m l k %Tp o %qqj 6p En%#@@j l o m l k 6lp Eq@@j o n m l k n E

l T o o n m@l qk p p Emqqj j l n l k

l T o orm m#@k @ p p En%#l@@j j l n l k

l o oEqm@l @k @ p p rnqmqqj j l n l k

%Tp o n%qqj l m l k l n m l k %Tp o %qqj %Tp o qqj l n m l k Ex n

E n
n Ex

Enq

qx n

z t v t x t2&{

nx

SAP AG

Objects form capsules containing the data itself and the behavior of that data. Objects enable you to draft a software solution that is a one-to-one reflection of the real-life problem area.

The left part of the slide shows that, with procedural software systems, data and functions are often:

Linked with input-output relations

Stored separately

Created separately

3URJUDPPLQJ 0RGHOV

'DWD DQG IXQFWLRQV

SAP AG 2002

BC401

2EMHFW PRGHO DV DEVWUDFWLRQ RI UHDO ZRUOG

5-14

&KDUDFWHULVWLFV RI WKH 2EMHFW2ULHQWHG $SSURDFK

l l l l l l
z t v t x t@yuY{

2EMHFWV DUH DQ DEVWUDFWLRQ RI WKH UHDO ZRUOG 3URFHVVHV FDQ EH LPSOHPHQWHG UHDOLVWLFDOO\

2EMHFWV DUH XQLWV PDGH XS RI GDWD DQG WKH IXQFWLRQV EHORQJLQJ WR WKDW GDWD ,PSURYHV VRIWZDUH VWUXFWXUH DQG FRQVLVWHQF\ LQ WKH GHYHORSPHQW SURFHVV 0DLQWHQDQFH UHTXLUHPHQWV DUH UHGXFHG 0RGHOHU HQG XVHU DQG GHYHORSHU DUH DOO LQFOXGHG LQ DQDO\VLV DQG GHVLJQ SURFHVV

SAP AG 2002

In object-oriented programming, data and functions are developed together. Object orientation focuses on objects that represent either abstract or concrete things in the real world. They are first viewed in terms of their characteristics, which are displayed using the objects internal structure and attributes (data). The EHKDYLRU RI REMHFWV is described through methods (functions) and events. Consistency throughout the software development process: The "language" used in the various phases of software development (analysis, specification, design, and implementation) is uniform. Ideally, changes made to the design during the implementation phase will flow back into the design automatically. The aim is to use this concept to: Implement processes realistically and, at the same time, better involve the modeler and developer in the software design Achieve optimized structuring and maintenance of the software and hence reduce the work required

n n

SAP AG

BC401

5-15

7HFKQLFDO ,PSOHPHQWDWLRQ RI WKH 2EMHFW2ULHQWHG $SSURDFK


6l l o En qm@l @k @ %pp Eqqj j o n m l k 6l n #l@k @ %Tp or%mqqj x n % n

l (QFDSVXODWLRQ RI GDWD DQG IXQFWLRQV l 3RO\PRUSKLVP IRU VXSSRUW RI JHQHULF SURJUDPPLQJ l ,QKHULWDQFH

l ,PSURYHG VWUXFWXULQJ DQG FRQVLVWHQF\ LQ WKH VRIWZDUH GHYHORSPHQW SURFHVV

SAP AG 2002

(QFDSVXODWLRQ Encapsulation means that the implementation of an object is hidden from other components in the system, so that they cannot make assumptions about the internal status of the object and therefore dependencies on specific implementations do not arise. 3RO\PRUSKLVP Polymorphism (ability to have multiple forms) in the context of object technology signifies that objects in different classes react differently to the same messages. ,QKHULWDQFH Inheritance defines the implementation relationship between classes, in which one class (the subclass) shares the structure and the behavior defined in one or more other classes (superclasses). Note: ABAP Objects only allows single inheritance.

SAP AG

BC401

Hqp

Hqp
q@ qq n o

@p

&qp

2Tf &

TTf  &

Tf &

5-16

&OLHQW6HUYHU %HKDYLRU

l l l

2EMHFWV EHKDYH WRZDUGV HDFK RWKHU MXVW OLNH FOLHQWVHUYHU V\VWHPV 2EMHFWV QRUPDOO\ DGRSW ERWK UROHV 5HVSRQVLELOLWLHV PXVW EH VSHFLILHG EHWZHHQ WKH REMHFWV

0HWKRGV 'DWD

&$// 0(7+2' VHUYHU!GRBLW

FDOOBLW

GRBLW

0HWKRGV 'DWD

SAP AG 2002

Objects behave like client/server systems: When an object calls a method of another object, it automatically becomes the client of the other (server) object. This gives rise to two conditions: The client object must adhere to the SURWRFRO of the server object The protocol must be clearly described so that a potential client can follow it without problem

n n

Objects normally adopt both roles Every object is a potential server object, and when it is called by a method of another object, it becomes a client object too. Establishing logical business and software/technical responsibilities between classes results in a true client/server software system in which redundancy is avoided.

SAP AG

BC401

uu

%%

5-17

&RPSDWLELOLW\ DQG 'HVLJQ $LPV


$%$3 2EMHFWV 3URJUDPP $%$3 2EMHFWV 3URJUDPP '$7$ FRXQWHU 7<3( L '$7$ FRXQWHU 7<3( L ZD W\SH .1$ ZD W\SH .1$   &/$66 OFOBFDU '(),1,7,21 &/$66 OFOBFDU '(),1,7,21   (1'&/$66 (1'&/$66  PDLQ SURJUDP   PDLQ SURJUDP  &5($7( 2%-(&7  &5($7( 2%-(&7  FRXQWHU FRXQWHU   FRXQWHU FRXQWHU   029( ZD 72  029( ZD 72 

&RPSDWLELOLW\
l l

$%$3 2EMHFWV LV D WUXH FRPSDWLEOH H[WHQVLRQ RI $%$3 $%$3 2EMHFWV VWDWHPHQWV FDQ EH XVHG LQ SURFHGXUDO $%$3 SURJUDPV 2EMHFWV FODVVHV WKHPVHOYHV FRQWDLQ FODVVLF $%$3 VWDWHPHQWV

'HVLJQ DLPV ZHUH


l l l

$V VLPSOH DV SRVVLEOH

2QO\ REMHFWRULHQWHG FRQFHSWV WKDW KDYH SURYHG XVHIXO ,QFUHDVHG XVH RI W\SH FKHFNV

SAP AG 2002

ABAP Objects is not a new language, but has been developed as an extension of ABAP. It integrates seamlessly into ABAP syntax and the ABAP programming model. All enhancements are strictly upward compatible. In ABAP objects, types have to be assigned more strictly than in ABAP. - for example, when defining interface parameters for methods, you must type the parameters. The correct pass by value is then checked by the system when the method is called. In ABAP Objects, the ABAP language has been cleaned up. As part of this language clean up, the system sometimes executes stricter syntax checks for previously permitted constructions and obsolete statements are not allowed. The stricter syntax checks usually result in a syntax that should also be used outside ABAP Objects, but where the old forms cannot be prohibited for compatibility reasons. For further information, refer to the ABAP Objects documentation under 5HSODFHPHQW RI 2EVROHWH 6WDWHPHQWV

SAP AG

BC401

5-18

$%$3 0DLQ 0HPRU\ DQG (QFDSVXODWLRQ


T @qTqi@  q @ @ @ r T

l 0RGXODUL]DWLRQ XQLWV IRU HQFDSVXODWLQJ


y@2y

SAP AG 2002

n n

One feature and design aim of object-oriented languages is that business functions are held separately. The client (in this case, the main program) uses the addresses of the objects (pointers or reference variables) to access their encapsulated functions. Amongst other things, this concept is to improve the structuring, reusability, and maintainability of the software.

SAP AG

BC401

y@y

T q @ @@  h  @#Tqr 

%Tp oE@qkj 6l n %m #l@@

En% x n

7 3 6"5" 0 2 1)' &$!"  0 4 # 4 3 ' 0 ( % #


%

 h iq

 Exx! B  

 x xxxxE&  

l :RUNLQJ ZLWK JOREDO GDWD RI WKH PDLQ SURJUDP

DQG EXVLQHVV DUH HQFDSVXODWHG LQ REMHFWV

5-19

7KH 6RIWZDUH 'HYHORSPHQW 3URFHVV

5HTXLUHPHQW LGHD
D|  C  Y yBYt%AYBu v A z   v {}t1@v C | A BBy  v

7HVW

SAP AG 2002

In object-oriented programming, the analysis and design phase is even more important than it is for procedural programming. The reason for this is that in object-oriented programming, decisions taken during the analysis and design phase have even more pronounced effects on implementation than they do in procedural programming.

SAP AG

9 z x  @&{@tt

$QDO\VLV DQG GHVLJQ

,PSOHPHQWDWLRQ $%$3 2EMHFWV

BC401

5-20

,QWURGXFWLRQ WR 2EMHFW2ULHQWHG 3URJUDPPLQJ 6XPPDU\


<RX DUH QRZ DEOH WR

l 1DPH WKH PRVW VLJQLILFDQW GLIIHUHQFHV EHWZHHQ SURFHGXUDO DQG REMHFWRULHQWHG SURJUDPPLQJ l 6WDWH WKH DLPV EHKLQG GHYHORSLQJ WKH $%$3 2EMHFWV SURJUDPPLQJ ODQJXDJH

SAP AG 2002

SAP AG

BC401

5-21

$QDO\VLV DQG 'HVLJQ &RQWHQWV


l &ODVVLILFDWLRQ RI REMHFWV l 80/ l &ODVV GLDJUDPV

l 6HTXHQFH GLDJUDPV

SAP AG 2002

SAP AG

BC401

6-1

$QDO\VLV DQG 'HVLJQ 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l /LVW VRPH LPSRUWDQW GLDJUDP W\SHV LQ 80/ l &UHDWH D FODVV GLDJUDP l &UHDWH D VHTXHQFH GLDJUDP

SAP AG 2002

SAP AG

BC401

6-2

v g qu r y gh e q Er x qu u y h u e r tEw qu u y

` b X aQ G YG EGR R CVPQ UXT G "S IPQ EHGF X cE W T S R I G

t E ECEf x e vr 'u w vu s q p i ge E'h tr "h "h 'fd ( & 53 2 7 2 !8 0 %764 !$ !1$ 0')'%$# !"  D A A 1 D ECB# " #$ 9@& 7 ! #1           

SAP AG
SAP AG 2002

$QDO\VLV DQG 'HVLJQ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

6-3

$QDO\VLV DQG 'HVLJQ 

&ODVVLILFDWLRQ &ODVVLILFDWLRQ
80/  WKH PRGHOLQJ VWDQGDUG 80/ WKH PRGHOLQJ VWDQGDUG

SAP AG 2002

SAP AG

BC401

6-4

&ODVVLILFDWLRQ RI 2EMHFWV

OFOBYHKLFOH

SAP AG 2002

In the real world, there are objects, such as various airplanes , cars, and people. Some of these objects are very similar, that is, they can be described using the same attributes or characteristics and provide the same functions. Similar objects are grouped together in classes. Each class is described once, and each object is then created in accordance with this blueprint. A class is therefore a description of a quantity of objects characterized by the same structure and the same behavior. For example, the vehicle "make x, ... serial number xxx" is an object of the class "vehicle". This object is therefore a concrete instance of the class.

SAP AG

f}U~Huu{ywutqrpn o z ~ }| q z x v s s o

mlaid gk j h g f e
OFOBSHRSOH
BC401 6-5

'LIIHUHQW $EVWUDFWLRQV RI D &ODVV

36

SAP AG 2002

In this context, abstractions are a simplified representations of complex relationships in the real world. An actually existing object is abstracted to the significant dimensions that are to be mapped. Insignificant details are left out in order to aid understanding of the overall system. This example concerns cars. Software for a car enthusiast and software for a scrap merchant contain different abstractions (classes) for these objects. A class can contain very different objects depending on the DEVWUDFWLRQ. While in one software system the class lcl_vehicle describes all transport vehicles (including bikes), in another system it describes all motorized machines with wheels. Both classes have the same name but describe different objects. In our examples, the class lcl_vehicle describes all motorized vehicles with 4 wheels.

n n n

SAP AG

BC401

)"")H ) 4 Ei

%U'6a 

:KHHOV

C

OFOBYHKLFOH

7\SH

'RRUV

r a

0DNH

y 4
" a
OFOBYHKLFOH

r U4U  t

UU 

EyUuu
) fr) U 4 4 'i

6-6

5HSUHVHQWDWLRQ RI D &ODVV

PDNH

OFOBYHKLFOH

SAP AG 2002

UML notation: A class is represented with its name, attributes, and methods. With UML, you also have the option of omitting either the attribute or the method part. Attributes describe the data that can be stored in the objects in a class. They define the state of an object. Methods describe the functions that can be executed on the data of the object. They contain the executable source code and therefore also represent an objects "behavior". ABAP Objects events are not included in class diagrams.

n n n

SAP AG

4jaE U

fytr)U

tu



PRGHOO SULFH
Optional, does not have to be Assoziationsme specified

FRORU
VHWBPDNH

LQFUHDVHBVSHHG

GLVSOD\BDWWULEXWHV

BC401

6-7

&ODVVHV DQG 2EMHFWV

&ODVV
l l

6SHFLILF LQVWDQFH RI D FODVV

SAP AG 2002

n n n

A FODVV is a set of objects that have the same structure and the same behavior. A class is therefore like a blueprint, in accordance with which all objects in that class are created. Every REMHFW has an identity, a status (set of attribute values), and behavior (set of methods and events). The structure and behavior of similar objects are defined in their shared class. Identity is an attribute that distinguishes each object from all other objects. Identity is often confused with having the same attribute values or with a unique name. 7ZR GLIIHUHQW REMHFWV FDQ KDYH LGHQWLFDO DWWULEXWH YDOXHV DQG VWLOO QRW EH LGHQWLFDO Example: Two coffee cups are the same height and diameter, have the same handle and are both white. Although they look exactly the same, they are still two separate cups. In German literature on the subject of object orientation, we often speak of instances. In object orientation an instance is simply an object. Another synonym of REMHFW is LQVWDQFH.

SAP AG

BC401

k j

U H C

2EMHFW LQVWDQFH 

5HSUHVHQWDWLRQ RI UHDO ZRUOG

k j

6SHFLILHV VWDWXV GDWD DWWULEXWHV DQG EHKDYLRU PHWKRGV

H
6-8

*HQHUDO GHVFULSWLRQ RI REMHFWV EOXHSULQW

OFOBYHKLFOH

a@E@a@d e k j l e e ek e

$QDO\VLV DQG 'HVLJQ 

&ODVVLILFDWLRQ &ODVVLILFDWLRQ
80/  WKH PRGHOLQJ VWDQGDUG 80/ WKH PRGHOLQJ VWDQGDUG

SAP AG 2002

SAP AG

BC401

6-9

0RGHOLQJ ZLWK 80/


l 80/ VWDQGV IRU 8QLILHG 0RGHOLQJ /DQJXDJH

l ,W LV D JOREDO VWDQGDUG

l ,W LV D ODQJXDJH DQG IRUP RI QRWDWLRQ IRU WKH VSHFLILFDWLRQ FRQVWUXFWLRQ YLVXDOL]DWLRQ DQG GRFXPHQWDWLRQ RI PRGHOV IRU VRIWZDUH V\VWHPV
'LDJUDP W\SHV

SAP AG 2002

UML (Unified Modeling Language) is a standardized modeling language. It is used for the specification, construction, visualization, and documentation of models for software systems and enables uniform communication between various users. UML does not describe the steps in the object-oriented development process. UML is an industry standard and has been standardized by the OMG (Object Management Group) since September 1997 as UML Version 1.1. The members of the OMG are continuously developing it further. SAP uses UML as the company-wide standard for object-oriented modeling. You can find the UML specifications on the OMG homepage at: http://www.omg.org UML describes a number of different diagram types in order to represent different views of a system. &ODVV GLDJUDPV show the static view of a model.

n n n n n

%HKDYLRU GLDJUDPV demonstrate the relationships and method calls between objects. They emphasize the timing sequence of method calls. Component diagrams show the organization and dependencies of components. Distribution diagrams represent the dependencies of software and hardware.

SAP AG

w w w w w

&ODVV GLDJUDP

%HKDYLRU GLDJUDP VXFK DV D VHTXHQFH GLDJUDP &RPSRQHQW GLDJUDP  'LVWULEXWLRQ GLDJUDP

BC401

6-10

([DPSOH RI D &ODVV 'LDJUDP

OFOBUHQWDO
1

0..*

OFOBERRNLQJ

0..* 1

OFOBFXVWRPHU

OFOBYHKLFOH

1..*

1..*

OFOBZKHHO

OFOBFDU

OFOBWUXFN

OFOBEXV

SAP AG 2002

A class diagram describes the elements contained in the model and their various static relationships. There are two basic forms of static relationships: Associations (for example, a car rental customer books a car) Generalization / specialization (for example, a car and a bus are both vehicles)

In class diagrams, classes can also be shown with their attributes and methods.

SAP AG

BC401

6-11

$VVRFLDWLRQ

A customer can rent several cars

A booking refers to a customer

OFOBERRNLQJ 0..*

ERRNV

OFOBFXVWRPHU

* or 0..* 1 1..* 0..1

SAP AG 2002

An association describes a semantic relationship between classes. The specific relationship between objects in these classes is known as an object link. Object links are therefore the instances of an association. An association is usually a relationship between different classes. However, an association can also be recursive; in this case, the class would have a relationship with itself. In most cases, recursive associations are used to link two different objects in one class. The points below assume that the associations are binary. Each association has two roles, one for each direction of the association (booking customer, customer booking). Roles can have names (for example, the association car reservation could be called "reservation"). Each role has a cardinality that shows how many instances participate in this relationship. The multiplicity is the number of participating objects in one class that have a relationship to an object in the other class. UML notation: An association is represented by a line between the class symbols The cardinality of the relationship can be shown at each end of the line Associations can be given a name for ease of identification (a verb or a short text). This name is written in italics above the line and may have an arrow to show the read direction. Both are optional.

SAP AG


Many Exactly one One or more Zero or one
BC401

Association name

6-12

$JJUHJDWLRQ DQG &RPSRVLWLRQ

$JJUHJDWLRQ LV D VSHFLDO FDVH RI DVVRFLDWLRQ D ZKROHSDUW UHODWLRQVKLS

Aggregation symbol

OFOBYHKLFOH

0..*

OFOBZKHHO

&RPSRVLWLRQ LV D VSHFLDO FDVH RI DJJUHJDWLRQ DQ H[LVWHQFHGHSHQGHQW ZKROHSDUW UHODWLRQVKLS

Composition symbol

OFOBUHQWDO

1..*

OFOBERRNLQJ

SAP AG 2002

Aggregation is a special kind of association. Aggregation describes one object that contains another or consists of other objects (whole-part). A vehicle consists of wheels. The relationship can be described by the words "consists of" or "is a part of". UML notation for aggregation: An aggregation, like an association, is represented by a line between two classes, which then additionally has a small rhombus at one end. The rhombus is always at the aggregate end, that is, the whole object end. Otherwise the notation conventions are the same as for associations. Composition is a special kind of aggregation. Composition describes the fact that the object contained cannot exist without the aggregate (for example, a car reservation cannot exist without the car rental). Differences to aggregation: The cardinality on the aggregate side can only be one. Each part is only part of one composite object, otherwise the existence dependency would be contradictory. The lifetime of the individual parts is linked to the lifetime of the aggregate: Parts are created either with or immediately after the aggregate, and they are destroyed either with or immediately before the aggregate. UML notation for composition: Like aggregation, composition is shown as a line between two classes and marked with a small rhombus on the aggregate side. However, in contrast to aggregation, the rhombus is filled in.

SAP AG

BC401

6-13

*HQHUDOL]DWLRQ DQG 6SHFLDOL]DWLRQ


OFOBYHKLFOH
Generalization
Generalization
6-14

Inheritance arrow

OFOBFDU

OFOBWUXFN

RU
Specialization

OFOBYHKLFOH

OFOBFDU
SAP AG 2002

OFOBWUXFN

UML notation: Generalization and specialization are denoted by triangular arrows that point from the subordinate class to the superclass. Several arrows can be combined into a tree.

SAP AG

BC401

Specialization

6HTXHQFH 'LDJUDPV ([DPSOH

OFOBGULYHU

OFOBFDU

Time

3URFHVV GHVFULSWLRQ RSWLRQDO

0HWKRG SDUDPHWHU

Life line of object

5HWXUQ YDOXH

Control focus

SAP AG 2002

Sequence diagrams, unlike class diagrams, show the dynamics between objects. They are used to represent a particular process or a particular situation. Sequence diagrams focus on the time sequence of the information exchange: a) Creation and deletion of objects b) Message exchange between objects Sequence diagrams have no notation for representing static methods. The object life line is represented by vertical dotted lines. The control focus is shown as a vertical rectangle on the object life line. The control focus shows the objects "active" period: An object is active when actions are executed An object is indirectly active if it is waiting for a subordinate procedure to end.

n n n

Messages are shown as horizontal arrows between the object lines. The message is written above the arrow in the form PHWKRG SDUDPHWHU . There are various ways of representing the reply; in this example, the arrow is shown as a returning arrow. You can also include a description of the process and add comments to the object life line as required.

SAP AG

BC401

6-15

6HTXHQFH 'LDJUDP 'HOHJDWLRQ

OFOBGULYHU

OFOBFDU

OFOBWDQN

 JHWBIXHOBOHYHO

 JHWBIXHOBOHYHO
UHBOHYHO

UHBOHYHO

UHBOHYHO WDQN!JHWBIXHOBOHYHO 

UHBOHYHO IXHO  IXHOBPD[ 

SAP AG 2002

n n

In delegation, two objects are involved in handling a request: The recipient of the request delegates the execution of the request to a delegate. Example: The driver (lcl_driver) calls the method get_fuel_level for the class car (lcl_car). The car cannot carry out this task itself. Therefore, the car calls the get_fuel_level method for the class (lcl_tank), that is the car GHOHJDWHV the execution of the method to the tank. The main advantage of delegation (as a re-use mechanism) lies in the option of changing the behavior of the recipient by substituting the delegate (at runtime). For example, delegation enables the car to be equipped with a new tank, without the call changing for the client or for the car class. Good encapsulation often forces the use of delegation: If tank in the above example were a private attribute of the class lcl_car, the user could not address the tank directly, but only through the car.

SAP AG

BC401

6-16

$QDO\VLV DQG 'HVLJQ 6XPPDU\

l /LVW VRPH GLDJUDP W\SHV LQ 80/ l &UHDWH D FODVV GLDJUDP l &UHDWH D VHTXHQFH GLDJUDP

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

6-17

([HUFLVHV
8QLW $QDO\VLV DQG 'HVLJQ 7RSLF 80/ &ODVV 'LDJUDPV
At the conclusion of these exercises, you will be able to: Create a UML class diagram

An airline needs to manage its airplanes.

1-1

Use a pencil and paper to create a UML class diagram that contains the following classes: - Airline: OFOBFDUULHU Airplane (general): OFOBDLUSODQH Passenger airplane: OFOBSDVVHQJHUBSODQH Cargo plane: OFOBFDUJRBSODQH 1-1-1 Include some appropriate attributes and methods for every class. 1-1-2 Draw lines to represent the relationships between the classes and indicate possible cardinalities.

SAP AG

BC401

6-18

6ROXWLRQV
8QLW $QDO\VLV DQG 'HVLJQ 7RSLF 80/ &ODVV 'LDJUDPV

SAP AG

BC401

6-19

3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ &RQWHQWV


l &ODVVHV l 2EMHFWV l $WWULEXWHV l 0HWKRGV l 9LVLELOLW\HQFDSVXODWLRQ l ,QVWDQWLDWLRQ l &RQVWUXFWRU

l *DUEDJH FROOHFWRU

SAP AG 2002

SAP AG

BC401

7-1

3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 8QLW 2EMHFWLYHV


$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l &UHDWH FODVVHV l &UHDWH REMHFWV l &DOO PHWKRGV

l ([SODLQ KRZ WKH JDUEDJH FROOHFWRU ZRUNV

SAP AG 2002

SAP AG

BC401

7-2

y & & $x g V eb d f e c a ` Y WV dX &b X RX $U !  % # " 0) '  1(!  !   &$  H PQ@ S 6 T6 (A EF9@ DH B(6 ABR89@ 76 5 3   ' 2  % 4 86   4  ! (  H G I6 C A C    ljhef tv t   i g m k i onn mklii ghe dd o pp R n  he qq t rr he q v t us

eW da b h w s WX V a b uvg td a d h i qrX d V b p&f d a d h

SAP AG
SAP AG 2002

3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

7-3

3ULQFLSOHV 

&ODVVHV DWWULEXWHV DQG PHWKRGV &ODVVHV DWWULEXWHV DQG PHWKRGV


2EMHFWV LQVWDQFHV RI FODVVHV 2EMHFWV LQVWDQFHV RI FODVVHV
$FFHVVLQJ DWWULEXWHV DQG PHWKRGV $FFHVVLQJ DWWULEXWHV DQG PHWKRGV
7KH FRQVWUXFWRU 7KH FRQVWUXFWRU
$GGLWLRQDO SULQFLSOHV $GGLWLRQDO SULQFLSOHV

SAP AG 2002

SAP AG

BC401

7-4

([DPSOH RI D &ODVV

3XEOLF components
{ z x w { tv
{ z x w tv{I

3ULYDWH components
t{Itlw ~ } |

0HWKRGV Implementation
3XEOLF DFFHVV Interface Normally methods, events

3ULYDWH DFFHVV Encapsulation Normally attributes

SAP AG 2002

In the graphic, the public component of the class is accessed using the green node or starting point displayed on the left. A user or client can use this node to access the public components and hence also indirectly access the private components. However, the private components of the class cannot be addressed directly . They are not visible to the outside user. Why are the private components of a class hidden? This principle is called LQIRUPDWLRQ KLGLQJ or HQFDSVXODWLRQ and is used to protect the user. Let us assume that a class changes its private components, while its interface remains unchanged. Any user who simply needs to access the interface of the class can carry on working with the class as usual. The user does not notice the change. However, if an class changes its public components, then any user who accesses these public components must take these changes into account.

SAP AG

BC401

h( h vRsh

OFOBYHKLFOH
{ z x Iyw

7-5

'HILQLQJ &ODVVHV

(1'&/$66

components

components

(1'&/$66

SAP AG 2002

n n

A FODVV is a set of objects that have the same structure and the same behavior. A class is therefore like a EOXHSULQW, in accordance with which all objects in that class are created. The components of the class are defined in the definition part. The components are attributes, methods, events, constants, types, and implemented interfaces. Only methods are implemented in the implementation part. The CLASS statement cannot be nested, that is, you cannot define a class within a class.

SAP AG

BC401

1$$ } | t{

&/$66 OFOBYHKLFOH ,03/(0(17$7,21

{ x

~ R

yyf

&/$66 OFOBYHKLFOH '(),1,7,21

OFOBYHKLFOH

7-6

$WWULEXWHV
l $WWULEXWHV FDQ KDYH DQ\ NLQG RI GDWD W\SH l & 1 , 3  675,1* l 'LFWLRQDU\ W\SHV l 8VHUGHILQHG W\SHV

 OFOBFDU
Public PDNH 7<3( 675,1*  UBPRWRU 7<3( 5() 72  Private

l 7<3( 5() 72 GHILQHV D UHIHUHQFH WR DQ REMHFW LQ WKLV FDVH UBFDU

 OFOBUHQWDO
(
SAP AG 2002

n n

Attributes describe the data that can be stored in the objects of a class. Class attributes can be of any type: Data types: scalar (for example data element), structured, in tables ABAP elementary types (C, I, ...) Object references Interface references make modell ser_no color max_seats r_motor ... (car make) (type , model) (serial number) (color) (number of seats) (reference to class lcl_motor)

Examples of attributes for the class lcl_car are:

car_type (estate, convertible, ...)

SAP AG

BC401

7-7

$WWULEXWHV 7\SHV DQG &RQVWDQWV 6\QWD[

&/$66 FODVVQDPH! '(),1,7,21  7<3(6 QRUPDOH 7\SGHILQLWLRQ! &2167$176 FRQVWDQW 7<3( W\SH! 9$/8( YDOXH! '$7$ YDULDEOH YDULDEOH YDULDEOH YDULDEOH YDULDEOH YDULDEOH YDULDEOH 7<3( 7<3( /,.( 7<3( 7<3( 7<3( 7<3( W\SH! GGLFBW\SH! YDULDEOH W\SH! 9$/8( YDOXH! W\SH! 5($'21/< 5() 72 FODVVQDPH! 5() 72 LQWHUIDFH!

&/$66'$7$ RQO\BRQFH 7<3(   (1'&/$66

SAP AG 2002

n n n n

In classes, you can only use the TYPE addition to refer to data types. You can only use the LIKE reference for local data objects. The READ-ONLY addition means that a public attribute declared with DATA can be read from outside, but can only be changed by methods of the class. You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a class declaration or in an interface definition.

SAP AG

BC401

7-8

$WWULEXWHV DQG 9LVLELOLW\

&/$66 OFOBYHKLFOH '(),1,7,21

l 3XEOLF DWWULEXWHV

n n n n n

&DQ EH YLHZHG DQG FKDQJHG E\ DOO XVHUV DQG LQ DOO PHWKRGV 'LUHFW DFFHVV

38%/,& 6(&7,21 '$7$ PDNH 7<3( VWULQJ


35,9$7( 6(&7,21 (1'&/$66

l 3ULYDWH DWWULEXWHV

SAP AG 2002

You can protect attributes against access from outside by characterizing them as private attributes (defined in the PRIVATE SECTION). Attributes and their values that may be used directly by an external user are public attributes and are defined in the PUBLIC SECTION. In the above example for the class lcl_car, the attribute make is defined as a public attribute. Public attributes belong to the LQWHUIDFH of the class, that is their implementation is publicized. If you want to hide the internal implementation from users, you must define internal and external views of attributes. As a general rule, you should define as few public attributes as possible.

SAP AG

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21


...

1R GLUHFW DFFHVV IURP RXWVLGH WKH FODVV

&DQ RQO\ EH YLHZHG DQG FKDQJHG IURP ZLWKLQ WKH FODVV

35,9$7( 6(&7,21 '$7$ PDNH


(1'&/$66

7<3( VWULQJ

BC401

7-9

$FFHVVLQJ 3ULYDWH $WWULEXWHV

 OFOBYHKLFOH
3XEOLF
VHWBPDNH JHWBPDNH
...

SAP AG 2002

n n

You can access an objects private attributes using public methods, which in turn output this attribute or change it. It is not possible to directly access private attributes from outside (for example, main program or other object). The only exception lies in the so-called "friend" concept, which will be dealt with later in the course.

SAP AG

yj &

3ULYDWH
PDNH

hR

BC401

7-10

,QVWDQFH $WWULEXWHV DQG 6WDWLF $WWULEXWHV 

l ,QVWDQFH DWWULEXWHV
2QH SHU LQVWDQFH

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 35,9$7( 6(&7,21 '$7$ PDNH


... ...

6WDWHPHQW '$7$

l 6WDWLF DWWULEXWHV

7<3( VWULQJ

2QO\ RQH SHU FODVV

6WDWHPHQW &/$66'$7$ $OVR NQRZQ DV FODVV DWWULEXWHV

&/$66'$7$ QBRBYHKLFOHV 7<3( L (1'&/$66

SAP AG 2002

There are two kinds of attributes: Static attributes Instance attributes

n n

Instance attributes are attributes that exist separately for each object. Instance attributes are defined using the ABAP keyword DATA. Static attributes exist only once for each class and are visible for all (runtime) instances in that class. Static attributes usually contain information that applies to all instances, such as: Data that is the same in all instances Administrative information about the instances in that class (for example, counters, ...)

n n

Static attributes are defined using the CLASS-DATA keyword. You may come across the expression "class attributes" in documentation, however, the official term in ABAP Objects (as in C++, Java) is "static" attributes.

SAP AG

BC401

7-11

,QVWDQFH $WWULEXWHV DQG 6WDWLF $WWULEXWHV 


,QWHUQDO VHVVLRQ
6WDWLF DWWULEXWHV RQO\ H[LVW RQFH
v  h h v

QBRBYHKLFOHV
$ $7 " s $ $ D 7 $ s

UBYHKLFOH

UBYHKLFOH


SAP AG 2002

n n

The graphic shows the static attribute n_o_vehicles. It exists only once, regardless of how many instances there are of lcl_vehicle. Therefore, you can say that instances share their common attributes.

SAP AG

BC401

v v 1 r

UBYHKLFOH

1 vv 1 t

1 v 1 r

,QVWDQFHV  REMHFWV



7-12

0HWKRGV 6\QWD[

&/$66 FODVVQDPH! '(),1,7,21  0(7+2'6 PHWKRGBQDPH! > ,03257,1* LPBYDU! 7<3( W\SH! (;3257,1* H[BYDU! 7<3( W\SH! &+$1*,1* FKBYDU! 7<3( W\SH! 5(7851,1* 9$/8( UHBYDU! 7<3( W\SH! EXCEPTIONS <exception> 5$,6,1* FODVVBH[FHSWLRQ! @ (1'&/$66

0HWKRGV FDQ KDYH D VLJQDWXUH

&/$66 FODVVQDPH! ,03/(0(17$7,21 0(7+2' PHWKRGBQDPH!  (1'0(7+2' (1'&/$66

0HWKRGV FRQWDLQ WKH VRXUFH FRGH DQG KHQFH GHILQH WKH EHKDYLRU RI DQ REMHFW

SAP AG 2002

n n n

Methods are internal procedures in classes that determine the behavior of an object. They can access all attributes in their class and can therefore change the state of an object. Methods have a parameter interface (called VLJQDWXUH) that enables them to receive values when they are called and pass values back to the calling program. In ABAP Objects, methods can have IMPORTING, EXPORTING, CHANGING, and RETURNING parameters as well as exception parameters. All parameters can be passed by value or reference. (As of SAP R/3 Basis Release 6.10, you should no longer use the EXCEPTIONS parameter for exceptions but use the RAISING addition instead; this will be discussed in more detail later.) You can define a return code for methods using RETURNING. You can only do this for a single parameter, which additionally must be passed as a value. Also, you cannot then define EXPORTING and CHANGING parameters. You can define functional methods using the RETURNING parameter (this will be explained in more detail). All input parameters (IMPORTING, CHANGING parameters) can be defined as optional parameters in the declaration using the OPTIONAL or DEFAULT additions. These parameters then do not necessarily have to be passed when the object is called. If you use the OPTIONAL addition, the parameter remains initialized according to type, whereas the DEFAULT addition allows you to enter a start value.

SAP AG

BC401

7-13

$FFHVVLQJ 3ULYDWH 0HWKRGV

 OFOBYHKLFOH
3XEOLF 3ULYDWH
PDNH

SAP AG 2002

n n n

Methods also have to be assigned to a visibility area. This determines whether the methods can be called from outside or only from within the class. It is not possible to directly access private methods from outside. However, a private method can be called by a public method. Both method types can access the public and private attributes.

SAP AG

u((

yj &

 VHWBPDNH

LQLWBPDNH

BC401

7-14

0HWKRGV DQG 9LVLELOLW\


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 VHWBPDNH LPSRUWLQJ LPBPDNH 7<3( VWULQJ 35,9$7( 6(&7,21 0(7+2'6 LQLWBPDNH '$7$ PDNH 7<3( VWULQJ (1'&/$66
&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' LQLWBPDNH PDNH
QR PDNH
 (1'0(7+2' 0(7+2' VHWBPDNH ,) LPBPDNH ,6 ,1,7,$/ &DOOLQJ LQLWBPDNH  (/6( PDNH LPBPDNH (1',) (1'0(7+2' (1'&/$66

l 3XEOLF PHWKRGV

&DQ EH FDOOHG IURP DQ\ZKHUH

l 3ULYDWH PHWKRGV

&DQ RQO\ EH FDOOHG ZLWKLQ WKH FODVV

SAP AG 2002

In this example, init_make is a private method that is called by the public method set_make.

SAP AG

BC401

7-15

,QVWDQFH 0HWKRGV DQG 6WDWLF 0HWKRGV

5XOHV

l ,QVWDQFH PHWKRGV

&DQ XVH ERWK VWDWLF DQG LQVWDQFH FRPSRQHQWV LQ WKHLU LPSOHPHQWDWLRQ SDUW &DQ EH FDOOHG XVLQJ DQ LQVWDQFH

l 6WDWLF PHWKRGV

SAP AG 2002

Static methods are defined at class level. They are similar to instance methods, but with the restriction that they can only use static components (such as static attributes) in the implementation part. This means that static methods do not need instances and can be called from anywhere. They are defined using the CLASS-METHODS statement, and they are bound by the same syntax and parameter rules as instance methods. The term "class method" is common, but the official term in ABAP Objects (as in C++, Java) is "static method".

SAP AG

&DQ RQO\ XVH VWDWLF FRPSRQHQWV LQ WKHLU LPSOHPHQWDWLRQ SDUW &DQ EH FDOOHG XVLQJ WKH FODVV

BC401

7-16

,QVWDQFH 0HWKRGV DQG 6WDWLF 0HWKRGV ([DPSOH

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 VHWBPDNH ,03257,1* LPBPDNH 7<3( VWULQJ &/$660(7+2'6 JHWBFRXQW (;3257,1* H[BFRXQW 7<3( L 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ &/$66'$7$ QBRBYHKLFOHV 7<3( L
&/$66 OFOBYHKLFOH ,03/(0(17$7,21  0(7+2' JHWBFRXQW H[BFRXQW QBRBYHKLFOHV (1'0(7+2' (1'&/$66
SAP AG 2002

(1'&/$66

In the static method get_count, you can only use the static attribute n_o_vehicles. All other attributes of the class are instance attributes and can only appear in instance methods.

SAP AG

BC401

7-17

6XPPDU\ DQG 80/ 1RWDWLRQ

OFOBYHKLFOH
hv Rhh QR"(u uR F h h( RvQh Q Ru h Q

 VHWBPDNH  LQLWBPDNH  JHWBFRXQW 

 PDNH  PRGHO  QBRBYHKLFOHV 

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 VHWBPDNH ,03257,1* LPBPDNH 7<3( VWULQJ &/$660(7+2'6 JHWBFRXQW (;3257,1* H[BFRXQW 7<3( L 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ  &/$66'$7$ QBRBYHKLFOHV 7<3( L 0(7+2'6 LQLWBPDNH (1'&/$66
SAP AG 2002

n n

A UML class diagram shows firstly the class name and, underneath that, the class attributes and methods. The visibility of components in a class is shown in UML using the characters "+" and "-": + indicates public components  indicates private components Alternatively, public and private can be prefixed to the methods. The third option for providers of modeling tools in UML is to introduce their own symbols for visibility. Representation of visibility characteristics is optional and is normally only used for models that are close to implementation. Static components are marked with an underscore. The method signature is represented as follows (optional): The input and output parameters and the parameters to be changed are shown in brackets. The return code is separated from the type name by a colon.

n n

SAP AG

BC401

7-18

3ULQFLSOHV 

&ODVVHV DWWULEXWHV DQG PHWKRGV &ODVVHV DWWULEXWHV DQG PHWKRGV


2EMHFWV LQVWDQFHV RI FODVVHV 2EMHFWV LQVWDQFHV RI FODVVHV
$FFHVVLQJ DWWULEXWHV DQG PHWKRGV $FFHVVLQJ DWWULEXWHV DQG PHWKRGV
7KH FRQVWUXFWRU 7KH FRQVWUXFWRU
$GGLWLRQDO SULQFLSOHV $GGLWLRQDO SULQFLSOHV

SAP AG 2002

SAP AG

BC401

7-19

&UHDWLQJ 2EMHFWV

5XOHV

l l

2EMHFWV DUH FUHDWHG XVLQJ WKH &5($7( 2%-(&7 VWDWHPHQW

2EMHFWV FDQ RQO\ EH FUHDWHG DQG DGGUHVVHG XVLQJ UHIHUHQFH YDULDEOHV


&5($7( 2%-(&7 UBYHKLFOH

 VHWBPDNH  LQLWBPDNH  JHWBFRXQW 

SAP AG 2002

A class contains the generic description of an object. It describes all the characteristics that are common to all objects in that class. During the program runtime, the class is used to create specific objects (instances). This process is called instantiation. Example: The specific object Car xy with Ser-No. 0815" is created through instantiation from the class lcl_vehicle; it is created in the main memory at runtime. The lcl_vehicle class itself does not exist as an independent runtime object in ABAP Objects. Implementation: Objects are instantiated using the statement: CREATE OBJECT. During instantiation, the runtime environment dynamically requests main memory space and assigns it to the object.

SAP AG

   



BC401

 

 PDNH  PRGHO  VHUBQR  QBRBYHKLFOHV

OFOBYHKLFOH

 OFOBYHKLFOH
Public Private

7-20

5HIHUHQFH 9DULDEOHV

 &/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21  35,9$7( 6(&7,21  (1'&/$66 &/$66 OFOBYHKLFOH ,03/(0(17$7,21  (1'&/$66 '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBYHKLFOH 7<3( 5() 72 OFOBYHKLOFH

:KDW GR WKHVH UHIHUHQFH YDULDEOHV SRLQW WR"

...

SAP AG 2002

DATA: r_vehicle1 TYPE REF TO lcl_vehicle declares a reference variable that acts as a pointer to an object.

SAP AG

BC401

E    #BA

D    @BCBA#@9

"
"

7 6 ' 5  3 1 2 1  ) ( %  "  8!#4!0'!&$#!

7-21

&UHDWLQJ 2EMHFWV 6\QWD[

'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH &5($7( 2%-(&7 UBYHKLFOH &5($7( 2%-(&7 UBYHKLFOH

Public

Public

Private

SAP AG 2002

n n

The CREATE OBJECT statement creates an object in the main memory. The attribute values of this object are either initial values or correspond to the VALUE entry. Reference variables can also be assigned to each other. For the above example this would mean that r_vehicle1 and r_vehicle2 point to the same object.

SAP AG

BC401

W T $4U T RPH S Y X W V S S QI
Private

W T $$U 4T QRPH S Y X W V S S `

D    GBF
E    BCBA#@
7-22

*DUEDJH &ROOHFWRU
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH &5($7( 2%-(&7 UBYHKLFOH &5($7( 2%-(&7 UBYHKLFOH
Public

Public

Private

SAP AG 2002

n n

As soon as no more references point to an object, the Garbage Collector removes it from the memory. The Garbage Collector is a system routine that automatically deletes objects that can no longer be addressed from the main memory and releases the memory space they occupied.

SAP AG

BC401

W T $4U T RPH S Y X W V S S QI
Private

UBYHKLFOH

UBYHKLFOH

W T $$U 4T RPH S Y X W V S S Q`

D    @BBA#C@

E   #BCBA#@9

7-23

*DUEDJH &ROOHFWRU 3URFHGXUH


$OO LQGHSHQGHQW UHIHUHQFHV LQ WKH JOREDO PDLQ PHPRU\ DUH FKHFNHG 7KH UHIHUHQFHV SRLQW WR DFWLYH REMHFWV ZKLFK DUH PDUNHG LQWHUQDOO\ 5HIHUHQFHV RI FODVV RU LQVWDQFH DWWULEXWHV WR RWKHU REMHFWV DUH IROORZHG 7KHVH REMHFWV DUH DOVR PDUNHG 2EMHFWV WKDW DUH QRW PDUNHG DUH GHOHWHG IURP WKH PDLQ PHPRU\

l l l

SAP AG 2002

Independent references are references that have not been defined within a class.

SAP AG

BC401

h T We d c S Q q !pgf&$U S4T GrBH

h T e c S ` !pWgfd#$U S4T QRPH

higefCU T GbPH T W d c S S Q a
7-24

u  CtG# s

%XIIHULQJ 5HIHUHQFHV
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH LWDE 7<3( 7$%/( 2) 5() 72 OFOBYHKLFOH &5($7( 2%-(&7 UBYHKLFOH $33(1' UBYHKLFOH 72 LWDE

&5($7( 2%-(&7 UBYHKLFOH $33(1' UBYHKLFOH 72 LWDE

/223 $7 LWDE ,172 UBYHKLFOH ZRUN ZLWK WKH FXUUHQW LQVWDQFH (1'/223
...

SAP AG 2002

n n

If you want to keep several objects from the same class in your program, you can define an internal table, which, for example, only consist of one column containing the object references for this class. You can process the objects using a LOOP through the internal table.

SAP AG

BC401

wpgfdCU 4T GvPH h T We c S S Q I

h We d c S S Q a iTgfCU T GbPH
7-25

$JJUHJDWLRQ ([DPSOH

SAP AG 2002

If a class defines object references to a second class as attributes (in the above example: References to objects of the class lcl_wheel), only these object references will be stored in an object belonging to that class. The objects in the second class lcl_wheel have their own identity. They are encapsulated in the first class lcl_vehicle and can only be addressed from this class using reference variables.

SAP AG

BC401

S W W X S S Q I Cwp4U T 4PH

SCwpU T rPH W W X S S Q a

S W X S S Qq a 4WppU T GRPH

!p

y R7

Public

Private

S W W X S S Q $ppU T GiH
7-26

 OFOBYHKLFOH
x

3ULQFLSOHV 

&ODVVHV DWWULEXWHV DQG PHWKRGV &ODVVHV DWWULEXWHV DQG PHWKRGV


2EMHFWV LQVWDQFHV RI FODVVHV 2EMHFWV LQVWDQFHV RI FODVVHV
$FFHVVLQJ DWWULEXWHV DQG PHWKRGV $FFHVVLQJ DWWULEXWHV DQG PHWKRGV
7KH FRQVWUXFWRU 7KH FRQVWUXFWRU
$GGLWLRQDO SULQFLSOHV $GGLWLRQDO SULQFLSOHV

SAP AG 2002

SAP AG

BC401

7-27

&DOOLQJ 0HWKRGV

UBYHKLFOH!PRWRUBRQ
0HWKRG

5HIHUHQFH

SAP AG 2002

Every object behaves in a certain way. This behavior is determined by its methods. There are three types of method: 1. Methods that trigger the behavior and do not return values (see example) 2. Methods that return a value 3. Methods that return or change several values

An object that requires the services of another object sends a message to the object providing the services. This message names the operation to be executed. The implementation of this operation is known as a method. For the sake of simplicity, method will henceforth be used as a synonym for operation and message. The example shows the new syntax for method calls, in which the CALL-METHOD prefix is omitted.

n n

SAP AG

d e
&RPSRQHQW VHOHFWRU
BC401

 OFOBYHKLFOH
Public
PRWRUBRQ

Private

7-28

&DOOLQJ ,QVWDQFH 0HWKRGV 6\QWD[


&$// 0(7+2' LQVWDQFH!!LQVWDQFHBPHWKRG! (;3257,1* LPBYDU! YDULDEOH! ,03257,1* H[BYDU! YDULDEOH! &+$1*,1* FKBYDU! YDULDEOH! 5(&(,9,1* UHBYDU! YDULDEOH! EXCEPTIONS <exception> = <nr> LQVWDQFH!!LQVWDQFHBPHWKRG! >DGGLWLRQV@

VHH RQOLQH GRFXPHQWDWLRQ

'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH '$7$ PDNHBQDPH 7<3( 675,1*


...

PDNHBQDPH

&$// 0(7+2' UBYHKLFOH!VHWBPDNH (;3257,1* LPBPDNH PDNHBQDPH RWKHU YDULDQW VLQFH  DYDLODEOH VHH GRFXPHQWDWLRQ UBYHKLFOH!VHWBPDNH PDNHBQDPH QRZ JHWWLQJ VRPHWKLQJ EDFN IURP PHWKRG UBYHKLFOH!JHWBPDNH ,03257,1* H[BPDNH PDNHBQDPH 
SAP AG 2002

n n n n

Instance methods are called using CALL METHOD <reference>-><instance_method>. Since SAP R/3 Basis Release 6.10, the shortened form is also supported; CALL METHOD is omitted. For further information, refer to the online documentation. Special case: When calling an instance method from within another instance method, you can omit the instance name. The method is automatically executed for the current object. For the method call set_make, you can omit the EXPORTING addition in the brackets. In this case, it is sufficient to set the actual parameter of the caller (in the example: make_name). However, if the method has two or more parameters in its interface, all actual and formal parameters within the brackets must have the EXPORTING addition. In the case of methods that return parameters to the caller, the IMPORTING addition must always be used and all actual and formal parameters must be listed. In method calls, multiple parameters are separated by spaces.

n n

SAP AG

y wv t ot it u o p CxuAo h i g BPGPso g g rtwA q

i h g p u t k Ch Aonml g jt ABf

WKH PDNH RI WKH FDU




BC401

7-29

&DOOLQJ 6WDWLF 0HWKRGV 6\QWD[


6WDWLF PHWKRGV &$// 0(7+2' FODVVQDPH! !FODVVBPHWKRG! >DGGLWLRQV@ FODVVQDPH! !FODVVBPHWKRG! >DGGLWLRQV@
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH '$7$ PDNHBQDPH 7<3( 675,1* FRXQW 7<3( ,
...

JHWBFRXQW KDV RQH LQWHUIDFHSDUDPHWHU UHBFRXQW &$// 0(7+2' OFOBYHKLFOH !JHWBFRXQW ,03257,1* UHBFRXQW RWKHU YDULDQW VLQFH  DYDLODEOH VHH GRFXPHQWDWLRQ OFOBYHKLFOH !JHWBFRXQW ,03257,1* UHBFRXQW FRXQW 

FRXQW 

SAP AG 2002

n n n

Static methods (also referred to as class methods) are called using CALL METHOD <classname>=><class_method>. Static methods are addressed with their class name, since they do not need instances. Note: If you are calling a static method from within the class, you can omit the class name.

SAP AG

BC401

7-30

)XQFWLRQDO 0HWKRGV
 k m#i
SAP AG 2002

E 

)XQFWLRQDO PHWKRG

Methods that have a RETURNING parameter are described as functional methods. These methods cannot have EXPORTING or CHANGING parameters, but has many (or as few) IMPORTING parameters and exceptions as required. Functional methods can be used directly in various expressions: Logical expressions (IF, ELSEIF, WHILE, CHECK, WAIT) The CASE statement (CASE, WHEN) The LOOP statement Arithmetic expressions (COMPUTE) Bit expressions (COMPUTE) The MOVE statement.

SAP AG

es9F~|z   } {

  }  } { ~s9s9~|z

2QO\ RQH 5(7851,1* SDUDPHWHU

2QO\ ,03257,1* SDUDPHWHUV DQG H[FHSWLRQV DUH SRVVLEOH D 5(&(,9,1* SDUDPHWHUV SRVVLEOH

E 9DULRXV IRUPV RI GLUHFW FDOO SRVVLEOH

w w w

029( &$6( /223

/RJLFDO H[SUHVVLRQV ,) (/6(,) :+,/( &+(&. :$,7

$ULWKPHWLF H[SUHVVLRQV DQG ELW H[SUHVVLRQV &20387(  VHH H[DPSOH D E  F

BC401

7-31

)XQFWLRQDO 0HWKRGV ([DPSOH


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 JHWBDYHUDJHBIXHO ,03257,1* LPBGLVWDQFH 7<3( VBGLVWDQFH LPBIXHO 7<3( W\BIXHO 5(7851,1* 9$/8( UHBIXHO 7<3( W\BIXHO (1'&/$66
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH DYJBIXHO 7<3( W\BIXHO
...

H[DPSOH IRU VKRUW V\QWD[ LQ DULWPHW RSHUDWLRQ DYJBIXHO UBYHKLFOH!JHWBDYHUDJHBIXHO LPBGLVWDQFH  LPBIXHO  UBYHKLFOH!JHWBDYHUDJHBIXHO LPBGLVWDQFH  LPBIXHO

  

SAP AG 2002

Depending on the number of IMPORTING parameters, the syntax for functional methods is as follows (same for static functional methods): No IMPORTING parameters: Exactly 1 IMPORTING parameter: Several IMPORTING parameters: p2 ) ref->func_method( ) ref->func_method( p1 ) or ref->func_method( im_1 = p1 ) ref->func_method( im_1 = p1 im_2 =

Example of detailed syntax for functional method call: CALL METHOD r_vehicle->get_average_fuel EXPORTING im_distance = 500 im_fuel = 50 RECEIVING re_fuel = avg_fuel. Here, re_fuel is the formal parameter of the interface and avg_fuel is the actual parameter of the calling program.

SAP AG

BC401

7-32

([WHUQDO $FFHVV WR 3XEOLF $WWULEXWHV


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 '$7$ PDNH 7<3( VWULQJ 5($'21/< &/$66'$7$ QBRBYHKLFOHV 7<3( L 5($'21/< (1'&/$66 '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH '$7$ PDNHBQDPH 7<3( VWULQJ FRXQW 7<3( L 67$572)6(/(&7,21  * PDLQ SURJUDP WKLQN RI D FOLHQW &5($7( 2%-(&7 UBYHKLFOH UBYHKLFOH!PDNH PDNHBQDPH OFOBYHKLFOH !QBRBYHKLFOHV FRXQW

just a demo just a demo


 OFOBYHKLFOH
PDNH

RQO\ LI SXEOLF DWWU RQO\ LI SXEOLF DWWU

SAP AG 2002

There are different ways of accessing public attributes from outside the class:
n n n

You access static attributes using <classname> !<class_attribute> You access instance attributes using <instance>!<instance_attribute> ! and ! are the component selectors

SAP AG

BC401

!0

} s{}

80

7-33

3ULQFLSOHV 

&ODVVHV DWWULEXWHV DQG PHWKRGV &ODVVHV DWWULEXWHV DQG PHWKRGV


2EMHFWV LQVWDQFHV RI FODVVHV 2EMHFWV LQVWDQFHV RI FODVVHV
$FFHVVLQJ DWWULEXWHV DQG PHWKRGV $FFHVVLQJ DWWULEXWHV DQG PHWKRGV
7KH FRQVWUXFWRU 7KH FRQVWUXFWRU
$GGLWLRQDO SULQFLSOHV $GGLWLRQDO SULQFLSOHV

SAP AG 2002

SAP AG

BC401

7-34

&RQVWUXFWRU

l l l

6SHFLDO PHWKRG IRU FUHDWLQJ REMHFWV ZLWK GHILQHG LQLWLDO VWDWH 2QO\ KDV ,03257,1* SDUDPHWHUV DQG (;&(37,216

,V H[HFXWHG RQO\ RQFH SHU LQVWDQFH

 FRQVWUXFWRU 

 PDNH  PRGHO  QBRBYHKLFOHV 

OFOBYHKLFOH

&5($7( 2%-(&7

Public

0(7+2'6 FRQVWUXFWRU ,03257,1* LPBSDUDPHWHU! (;&(37,216 H[FHSWLRQ!


SAP AG 2002

The constructor is a special instance method in a class with the name constructor. The following rules apply: Each class can have one constructor. The constructor is automatically called at runtime within the CREATE OBJECT statement. If you need to implement the constructor, then you must define and implement it in the PUBLIC SECTION.

When exceptions are raised in the constructor, instances are QRW created, so no main memory space is occupied.

SAP AG

BC401

W S4T $$U 4T RPH Y X W V S S QI


Private

7-35

&RQVWUXFWRU ([DPSOH
&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 7<3( VWULQJ 0(7+2'6 FRQVWUXFWRU ,03257,1* LPBPDNH LPBPRGHO 7<3( VWULQJ 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ ZHLJKW 7<3( S &/$66'$7$ QBRBYHKLFOHV 7<3( L (1'&/$66

'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH  &5($7( 2%-(&7 UBYHKLFOH (;3257,1* LPBPDNH
)HUUDUL
LPBPRGHO
)

SAP AG 2002

You need to implement the constructor when, for example: You need to allocate (external) resources You need to initialize attributes that cannot be covered by the VALUE addition to the DATA statement You need to modify static attributes

You cannot normally call the constructor explicitly.

SAP AG

BC401

BAPArnABPGmP
&DOO IURP WKH PDLQ SURJUDP
7-36

&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' FRQVWUXFWRU PDNH LPBPDNH PRGHO LPBPRGHO $''  72 QBRBYHKLFOHV (1'0(7+2' (1'&/$66

3UHYLHZ 6WDWLF &RQVWUXFWRU


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 &/$660(7+2'6 &/$66B&216758&725 35,9$7( 6(&7,21 &/$66'$7$ QBRBYHKLFOHV 7<3( , (1'&/$66
&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' &/$66B&216758&725 ... (1'0(7+2' ... (1'&/$66

l l l

6SHFLDO VWDWLF PHWKRG

2QO\ H[HFXWHG RQFH SHU SURJUDP

$XWRPDWLFDOO\ FDOOHG EHIRUH WKH FODVV LV ILUVW DFFHVVHG

FRXQWHU OFOBYHKLFOH !QBRBYHKLFOHV LI DWWULEXWH LV SXEOLF GHPR  FRXQWHU  OFOBYHKLFOH !JHWBFRXQW LPSRUWLQJ UHBFRXQW
SAP AG 2002

6WDWLF &RQVWUXFWRU LV FDOOHG EHIRUH IROORZLQJ VWDWHPHQWV GHPR  &5($7( 2%-(&7 UBYHKLFOH GHPR 

The static constructor is a special static method in a class with the name class_constructor. It is executed precisely once per program. The static constructor of a class <classname> is called automatically when the class is first accessed, but before any of the following actions are executed: Creating an instance in the class using CREATE OBJECT <obj>, where <obj> has the data type REF TO <classname> Addressing a static attribute using <classname>=><attribute> Calling a static attribute using CALL METHOD <classname>=><classmethod> Registering a static event handler method using SET HANDLER <classname>=><handler_method> FOR <obj> Registering an event handler method for a static event in class <classname>.

The static constructor cannot be called explicitly.

SAP AG

BC401

7-37

3ULQFLSOHV 

&ODVVHV DWWULEXWHV DQG PHWKRGV &ODVVHV DWWULEXWHV DQG PHWKRGV


2EMHFWV LQVWDQFHV RI FODVVHV 2EMHFWV LQVWDQFHV RI FODVVHV
$FFHVVLQJ DWWULEXWHV DQG PHWKRGV $FFHVVLQJ DWWULEXWHV DQG PHWKRGV
7KH FRQVWUXFWRU 7KH FRQVWUXFWRU
$GGLWLRQDO SULQFLSOHV $GGLWLRQDO SULQFLSOHV

SAP AG 2002

SAP AG

BC401

7-38

1DPHVSDFH :LWKLQ D &ODVV

l 7KH VDPH QDPHVSDFH IRU


0HWKRGV

r$&

OFOBYHKLFOH
components

(YHQWV

7\SHV

0HWKRG PDNH

$WWULEXWH PDNH

components

$/,$6 QDPHV

l 7KHUH LV D ORFDO QDPHVSDFH ZLWKLQ PHWKRGV

SAP AG 2002

n n

Within a class, attribute names, method names, event names, constant names, type names and alias names all share the same namespace. There is a local namespace within methods. Definitions of local variables can cover components in one class.

SAP AG

BC401

p gvnv! i $i

&RQVWDQWV

i &

$WWULEXWHV

7-39

7KH 5HIHUHQFH 9DULDEOH 0(


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 FRQVWUXFWRU ,03257,1* LPBPDNH 7<3( VWULQJ LPBPRGHOO 7<3( VWULQJ 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ ... (1'&/$66
&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' FRQVWUXFWRU just a demo '$7$ PDNH 7<3( VWULQJ 9$/8(
%HQ]
 &21&$7(1$7( LPBPDNH PDNH ,172 0(!PDNH (1'0(7+2' 0(7+2' '800< * calling own methods from inside class * call method display_attributes( ) * with the short syntax variant: GLVSOD\BDWWULEXWHV  (1'0(7+2' (1'&/$66
SAP AG 2002

OFOBYHKLFOH
components

$WWULEXWH PDNH

components

n n

You can address the object itself within instance methods using the implicitly available reference variable me. Description of example: In the constructor, the instance attribute make is covered by the locally defined variable make. In order to still be able to address the instance attribute, you need to use me. The dummy method demonstrates how to call a classs own method. You can omit the prefix me->. Other important use: An object calls another objects method and passes its own address.

n n

SAP AG

BC401

 9vnv!0 i $i

9i &

rC$&

0(!PDNH

7-40

3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 8QLW 6XPPDU\


<RX DUH QRZ DEOH WR

l &UHDWH FODVVHV l &UHDWH REMHFWV l &DOO PHWKRGV

l ([SODLQ KRZ WKH JDUEDJH FROOHFWRU ZRUNV

SAP AG 2002

SAP AG

BC401

7-41

([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF &UHDWLQJ D &ODVV

At the conclusion of these exercises, you will be able to: Create a local class

An airline needs to manage its airplanes.

0RGHO VROXWLRQ 6$3%&B$,56B0$,1B$ 6$3%&B$,56B$ include program

1-1

Create the package =%&B (where ##: is your group number) and save all the repository objects you have created during the course in this package. Create the main program =%&B B0$,1B$,53/$1( and the include program =%&B B$,53/$1( the main program contains (##: group number). Create the class OFOBDLUSODQH in the include program. - QDPH - SODQHW\SH. The attribute for the airplane name should be of the type STRING. Define the attribute for the plane type using the table field VDSODQHSODQHW\SH. 1-3-2 The class has a SULYDWH VWDWLF DWWULEXWH: - QBRBDLUSODQHV. This attribute should be type I. 1-3-1 This class has two SULYDWH LQVWDQFH DWWULEXWHV:

1-2

1-3

SAP AG

BC401

7-42

1-3-3 The class has a SXEOLF LQVWDQFH PHWKRG VHWBDWWULEXWHV to set the private instance attributes name and plane type. Enter two corresponding importing parameters for the declaration of the method in the definition part. The definition of these parameters should be analogous to the two attributes. Implement the method in the implementation part; set both attributes. Each time the method is called, the static attribute QBRBDLUSODQHV should increase by one. (This is actually not right but will be corrected later). 1-3-4 The class is to have another SXEOLF LQVWDQFH PHWKRG GLVSOD\BDWWULEXWHV to display the instance attributes. Declare this method and, in the implementation part, output the attributes using the WRITE statement (you can also display the icon icon_ws_plane, in which case you must add TYPE-POOLS icon to the program. 1-3-5 Declare and implement a SXEOLF VWDWLF PHWKRG GLVSOD\BQBRBDLUSODQHV to display the static attribute QBRBDLUSODQHV. (In the remaining exercises in this course, you can always start with the program you created in the previous exercise or copy the corresponding model solution and continue working with this program.)

SAP AG

BC401

7-43

([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF ,QVWDQWLDWLQJ 2EMHFWV
At the conclusion of these exercises, you will be able to: Instantiate objects

0RGHO VROXWLRQ 6$3%&B$,56B0$,1B% 6$3%&B$,56B$ include program

2-1 Create a reference to the class OFOBDLUSODQH. 2-2 Define an internal table for buffering objects of the class OFOBDLUSODQH. The type of the internal table should therefore be 5() 72 OFOBDLUSODQH. 2-3 Create several objects of the class OFOBDLUSODQH and store them in the internal table. 2-4 Follow the program flow in the Debugger. (In the remaining exercises in this course, you can always start with the program you created in the previous exercise or copy the corresponding model solution and continue working with this program.)

SAP AG

BC401

7-44

([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF 0HWKRG &DOOV
At the conclusion of these exercises, you will be able to: Call instance methods and static methods

0RGHO VROXWLRQ 6$3%&B$,56B0$,1B& 6$3%&B$,56B& include program

3-1 Call the static method GLVSOD\BQBRBDLUSODQHV (before instantiating an object in class OFOBDLUSODQH). 3-2 Use the VHWBDWWULEXWHV method to set the attributes for all objects already created. Choose an airplane name and airplane type and pass them as text literals. (For the plane type you can use a type from the table SAPLANE, for example 747-400.) 3-3 Display the object attributes using the GLVSOD\BDWWULEXWHV method. 3-4 Call the static method GLVSOD\BQBRBDLUSODQHV a second time. 3-5 Add a functional static method gHWBQBRBDLUSODQHV to the class OFOBDLUSODQH. The method must be public and have the UHBFRXQW (type I) return parameter (no input parameters). 3-6 Test your functional method by calling it from the main program.

SAP AG

BC401

7-45

([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF &RQVWUXFWRU
At the conclusion of these exercises, you will be able to: Create a constructor for a class Create an object using the constructor 0RGHO VROXWLRQ 6$3%&B$,56B0$,1B' 6$3%&B$,56B' include program

4-1 Create a FRQVWUXFWRU for the class OFOBDLUSODQH (in your include program =%&B B$,53/$1() The simplest way of doing this is to copy the method VHWBDWWULEXWHV, which you now no longer need. 4-1-1 The constructor must have two importing parameters that fill the instance attributes QDPH and SODQHW\SH. The static attribute QBRBDLUSODQHV should have an ascending sequence of one in the constructor.

4-1-2

4-2 In the method VHWBDWWULEXWHV, comment out the line in which the static attribute QBRBDLUSODQHV is increased by one (if you still want to use this method). 4-3 In the main program =%&B B0$,1B$,53/$1(, extend the creation of the object with the constructor interface. Fill the constructors interface parameters with the same values you used when calling the VHWBDWWULEXWHV method. 4-4 2SWLRQDO Comment out the method call VHWBDWWULEXWHV or delete the call.

SAP AG

BC401

7-46

([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF &DOOLQJ D 3ULYDWH 0HWKRG

At the conclusion of these exercises, you will be able to: Call methods within a class

0RGHO VROXWLRQ 6$3%&B$,56B0$,1B( 6$3%&B$,56B( include program

Create the SULYDWH PHWKRG JHWBWHFKQLFDOBDWWULEXWHV for the class OFOBDLUSODQH (in your include program =%&B B$,53/$1().

5-1 The airplane type is the import parameter. (Type saplane-planetype) 5-2 The export parameter is the weight WEIGHT and the tank capacity TANKCAP. Refer to the types in the table SAPLANE. 5-3 The result, both export parameters are ascertained in a database access to the table SAPLANE. Therefore, both attributes are read, depending on the airplane type. 5-4 If the imported plane type does not exist in the table, set default values (weight 100000, tank capacity 10000). 5-5 Test your method JHWBWHFKQLFDOBDWWULEXWHV: 5-5-1 5-5-2 5-3-3 By calling it from the main program By calling it within the GLVSOD\BDWWULEXWHV method Which call was successful, and why?

SAP AG

BC401

7-47

6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF &UHDWLQJ D &ODVV

*&---------------------------------------------------------------------* *& Report SAPBC401_AIRS_MAIN_a * *&---------------------------------------------------------------------* 5(3257 VDSEFBDLUVBPDLQBD

7<3(322/6 LFRQ

,1&/8'( 6$3%&BDLUVB$ *&-----------------------------------------------------------------* *& Include SAPBC401_AIRS_A * *&-----------------------------------------------------------------* *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* &/$66 OFOBDLUSODQH '(),1,7,21 38%/,& 6(&7,21



&2167$176 SRVB 7<3( L 9$/8(  0(7+2'6 VHWBDWWULEXWHV ,03257,1* LPBQDPH

GLVSOD\BDWWULEXWHV

LPBSODQHW\SH 7<3( VDSODQHSODQHW\SH

7<3( VWULQJ

&/$660(7+2'6 GLVSOD\BQBRBDLUSODQHV 35,9$7( 6(&7,21 '$7$ QDPH

 SODQHW\SH 7<3( VDSODQHSODQHW\SH 7<3( VWULQJ

&/$66'$7$ QBRBDLUSODQHV 7<3( L

SAP AG

BC401

7-48

(1'&/$66 *------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* &/$66 OFOBDLUSODQH ,03/(0(17$7,21 0(7+2' VHWBDWWULEXWHV QDPH SODQHW\SH (1'0(7+2'

LPBQDPH

QBRBDLUSODQHV

LPBSODQHW\SH

QBRBDLUSODQHV  

0(7+2' GLVSOD\BDWWULEXWHV

:5,7(  LFRQBZVBSODQH DV LFRQ


1DPH RI DLUSODQH
  $7 SRVB QDPH

(1'0(7+2'


$LUSODQH W\SH
  $7 SRVB SODQHW\SH

0(7+2' GLVSOD\BQBRBDLUSODQHV

:5,7(  
7RWDO QXPEHU RI SODQHV
FD 

(1'0(7+2' (1'&/$66

$7 SRVB QBRBDLUSODQHV /()7-867,),(' 

SAP AG

BC401

7-49

6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF ,QVWDQWLDWLQJ 2EMHFWV

*&---------------------------------------------------------------------* *& Report SAPBC401_AIRS_MAIN_b * * *&---------------------------------------------------------------------* *& create and insert planes into internal table REPORT sapbc401_airs_main_b. *&---------------------------------------------------------------------*

TYPE-POOLS icon. INCLUDE SAPBC401_airs_A. '$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH

SODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH

START-OF-SELECTION. *##############################

&5($7( 2%-(&7 UBSODQH

$33(1' UBSODQH 72 SODQHBOLVW &5($7( 2%-(&7 UBSODQH

$33(1' UBSODQH 72 SODQHBOLVW &5($7( 2%-(&7 UBSODQH

$33(1' UBSODQH 72 SODQHBOLVW

SAP AG

BC401

7-50

6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF 0HWKRG &DOOV

*&---------------------------------------------------------------------* *& Report *& *& SAPBC401_AIRS_MAIN_C * * * *&---------------------------------------------------------------------* call method set_Attributes to initialize objects and visualize content of objects via display_attributes

*&---------------------------------------------------------------------* REPORT sapbc401_airs_main_c.

TYPE-POOLS icon. INCLUDE sapbc401_airs_c. '$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH FRXQW W\SH L

SODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH

START-OF-SELECTION. *############################## OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV  &5($7( 2%-(&7 UBSODQH

$33(1' UBSODQH 72 SODQHBOLVW

UBSODQH!VHWBDWWULEXWHV LPBQDPH

LPBSODQHW\SH

/+ %HUOLQ




&5($7( 2%-(&7 UBSODQH

$33(1' UBSODQH 72 SODQHBOLVW

UBSODQH!VHWBDWWULEXWHV LPBQDPH

LPBSODQHW\SH

$$ 1HZ <RUN




/223 $7 SODQHBOLVW ,172 UBSODQH (1'/223

UBSODQH!GLVSOD\BDWWULEXWHV 

SAP AG

BC401

7-51

OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV  FDOOLQJ WKH IXQFWLRQDO PHWKRG JHWBQBRBDLUSODQHV ORQJ V\QWD[ IRU IXQFWLRQDO FDOO OFOBDLUSODQH !JHWBQBRBDLUSODQHV UHFHLYLQJ UHBFRXQW VKRUW V\QWD[ IRU IXQFWLRQDO FDOO FRXQW OFOBDLUSODQH !JHWBQBRBDLUSODQHV  FRXQW 

ZULWH 
1XPEHU RI DLUSODQHV 
 FRXQW

*&---------------------------------------------------------------------* *& *& Include SAPBC401_AIRS_C * * Develops functional static method get_n_o_airplanes

*&---------------------------------------------------------------------* *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* &/$66 OFOBDLUSODQH '(),1,7,21 38%/,& 6(&7,21



&2167$176 SRVB 7<3( L 9$/8(  0(7+2'6 VHWBDWWULEXWHV ,03257,1* LPBQDPH

GLVSOD\BDWWULEXWHV

LPBSODQHW\SH 7<3( VDSODQHSODQHW\SH

7<3( VWULQJ

&/$660(7+2'6 GLVSOD\BQBRBDLUSODQHV

JHWBQBRBDLUSODQHV UHWXUQLQJ 9$/8( UHBFRXQW W\SH ,

35,9$7( 6(&7,21 '$7$ QDPH

 SODQHW\SH 7<3( VDSODQHSODQHW\SH 7<3( VWULQJ

&/$66'$7$ QBRBDLUSODQHV 7<3( L (1'&/$66

SAP AG

BC401

7-52

*------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* &/$66 OFOBDLUSODQH ,03/(0(17$7,21 0(7+2' VHWBDWWULEXWHV QDPH SODQHW\SH (1'0(7+2'

LPBQDPH

QBRBDLUSODQHV

LPBSODQHW\SH

QBRBDLUSODQHV  

0(7+2' GLVSOD\BDWWULEXWHV

:5,7(  LFRQBZVBSODQH DV LFRQ


1DPH RI DLUSODQH
  $7 SRVB QDPH

(1'0(7+2'


$LUSODQH W\SH
  $7 SRVB SODQHW\SH

0(7+2' GLVSOD\BQBRBDLUSODQHV

:5,7(  
7RWDO QXPEHU RI SODQHV
FD 

(1'0(7+2'

$7 SRVB QBRBDLUSODQHV /()7-867,),(' 

PHWKRG JHWBQBRBDLUSODQHV HQGPHWKRG (1'&/$66 UHBFRXQW

QBRBDLUSODQHV

SAP AG

BC401

7-53

6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF &RQVWUXFWRU
*&---------------------------------------------------------------------* *& Report *& SAPBC401_AIRS_MAIN_d * * *&---------------------------------------------------------------------* call constructor of class lcl_airplane *&---------------------------------------------------------------------* REPORT sapbc401_airs_main_d.

TYPE-POOLS icon. INCLUDE sapbc401_airs_d. '$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH

SODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH

START-OF-SELECTION. *############################## lcl_airplane=>display_n_o_airplanes( ). &5($7( 2%-(&7 UBSODQH H[SRUWLQJ LPBQDPH APPEND r_plane TO plane_list. &5($7( 2%-(&7 UBSODQH H[SRUWLQJ LPBQDPH
$$ 1HZ <RUN

/+ %HUOLQ

LPBSODQHW\SH




LPBSODQHW\SH




LOOP AT plane_list INTO r_plane. r_plane->display_attributes( ). ENDLOOP. lcl_airplane=>display_n_o_airplanes( ).

SAP AG

BC401

7-54

*&---------------------------------------------------------------------* *& Include SAPBC401_AIRS_d * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* CLASS lcl_airplane DEFINITION. PUBLIC SECTION. "-------------------------------CONSTANTS: pos_1 TYPE i VALUE 30. 0(7+2'6 FRQVWUXFWRU ,03257,1* LPBQDPH

GLVSOD\BDWWULEXWHV

LPBSODQHW\SH 7<3( VDSODQHSODQHW\SH

7<3( VWULQJ

CLASS-METHODS: display_n_o_airplanes. PRIVATE SECTION. "---------------------------------DATA: name TYPE string, planetype TYPE saplane-planetype. CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. *------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* CLASS lcl_airplane IMPLEMENTATION. 0(7+2' FRQVWUXFWRU QDPH LPBQDPH SODQHW\SH LPBSODQHW\SH QBRBDLUSODQHV QBRBDLUSODQHV   (1'0(7+2' METHOD display_attributes. WRITE: / icon_ws_plane as icon, / Name of airplane(001), AT pos_1 name, / Airplane type: (002), AT pos_1 planetype. ENDMETHOD. METHOD display_n_o_airplanes.

SAP AG

BC401

7-55

WRITE: /, / Number of airplanes: (ca1), AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. ENDMETHOD. ENDCLASS.

SAP AG

BC401

7-56

6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ 7RSLF

&DOOLQJ D 3ULYDWH 0HWKRG

*&---------------------------------------------------------------------* *& Report *& *& *& inside display_attributes a private method is called to get further details on technical aspects of the planetype sapbc401_airs_main_e. SAPBC401_AIRS_MAIN_e * * * *

*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------* REPORT

TYPE-POOLS icon. INCLUDE sapbc401_airs_e. DATA: r_plane TYPE REF TO lcl_airplane, plane_list TYPE TABLE OF REF TO lcl_airplane. START-OF-SELECTION. *############################## lcl_airplane=>display_n_o_airplanes( ). CREATE OBJECT r_plane exporting im_name = LH Berlin im_planetype = 747-400. APPEND r_plane TO plane_list. r_plane->display_attributes( ). CREATE OBJECT r_plane exporting im_name = AA New York im_planetype = 727-200. r_plane->display_attributes( ). lcl_airplane=>display_n_o_airplanes( ).

SAP AG

BC401

7-57

*&---------------------------------------------------------------------* *& Include SAPBC401_AIRS_e * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* CLASS lcl_airplane DEFINITION. PUBLIC SECTION. "-------------------------------CONSTANTS: pos_1 TYPE i VALUE 30. METHODS: constructor IMPORTING im_name display_attributes. CLASS-METHODS: display_n_o_airplanes. PRIVATE SECTION. "---------------------------------0(7+2'6 JHWBWHFKQLFDOBDWWULEXWHV ,03257,1* LPBW\SH (;3257,1* H[BZHLJKW W\SH VDSODQHSODQHW\SH 7<3( VBSODQBZHL 7<3( VBFDSDFLW\ TYPE string im_planetype TYPE saplane-planetype,

H[BWDQNFDS

DATA: name

TYPE string,

planetype TYPE saplane-planetype. CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. "lcl_airplane DEFINITION

SAP AG

BC401

7-58

*------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. name planetype ENDMETHOD. METHOD display_attributes. data: weight type saplane-weight, cap type saplane-tankcap. WRITE: / icon_ws_plane AS ICON, / Name of airplane(001), AT pos_1 name, / Airplane type: (002), AT pos_1 planetype. get_technical_attributes( exporting im_type = planetype importing ex_weight = weight ex_tankcap = cap ). write: / wheight: (003), weight, tankcap: (004), 60 cap. ENDMETHOD. METHOD display_n_o_airplanes. WRITE: /, / Number of airplanes: (ca1), AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. ENDMETHOD. "display_n_o_airplanes "display_attributes = im_name. = im_planetype. "constructor

n_o_airplanes = n_o_airplanes + 1.

0(7+2' JHWBWHFKQLFDOBDWWULEXWHV

6(/(&7 6,1*/( ZHLJKW WDQNFDS )520 VDSODQH ,172 H[BZHLJKW H[BWDQNFDS :+(5( SODQHW\SH   JHWBWHFKQLFDOBDWWULEXWHV "lcl_airplane IMPLEMENTATION LPBW\SH

,) V\VXEUF !  H[BZHLJKW H[BWDQNFDS

(1'0(7+2' ENDCLASS.

(1',)

SAP AG

BC401

7-59

,QKHULWDQFH &RQWHQWV
l *HQHUDOL]DWLRQ  VSHFLDOL]DWLRQ RI FODVVHV

SAP AG 2002

SAP AG

BC401

8-1

,QKHULWDQFH 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HILQH DQ LQKHULWDQFH UHODWLRQVKLS EHWZHHQ FODVVHV l 5HGHILQH PHWKRGV

SAP AG 2002

SAP AG

BC401

8-2

f  P#WF

hX gc d q y w XY V c d xp uvg c g q sr tY g V d fi cg g q

Q I RA T S 7 7 DB #@A G FI D7 B C 9@A 87 6 I H P7 E B E C 9 7

p V hd g i hg e a XV Y fd c Y b` Y WU &$ " 1) '  20("  "   % #!   4     ! ' 3  & 5   5 !  " !       g jkiRfRvd R #F l h g e n m o q p e l # q o { z x vv t ss vv |ss yy iwwPuxrr {| z xiPtux

SAP AG
SAP AG 2002

,QKHULWDQFH ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

8-3

,QKHULWDQFH 80/ ([DPSOH


OFOBYHKLFOH

LV D UHODWLRQVKLS

 PDNH  PRGHOO  VHUBQR  FRORU  QBRBYHKLFOHV FRQVWUXFWRU GLVSOD\BDWWULEXWHV JHWBFRXQW

 FDUBW\SH  PD[BVHDWV  DFFHOHUWLRQ


 JHWBW\SH

OFOBFDU

 JHWBFDUJR

 PD[BFDUJR  QBRBWDQNV  QBRBZKHHOV

OFOBWUXFN

 JHWBSDVVHQJHUV

 PD[BSDVVHQJHUV  ODYDWRU\  WHOHYLVLRQ

OFOBEXV

SAP AG 2002

Inheritance is a relationship, in which one class (the subclass) inherits all the main characteristics of another class (the superclass). The subclass can also add new components (attributes, methods, and so on) and replace inherited methods with its own implementations. Inheritance is an implementation relationship that emphasizes similarities between classes. In the example above, the similarities between the car, bus, and truck classes are extracted to the vehicle superclass. This means that common components are only defined/implemented in the superclass and are automatically present in the subclasses. The inheritance relationship is often described as an "is a" relationship: A truck LV D vehicle.

SAP AG

BC401

8-4

0XOWLSOH ,QKHULWDQFH"
Generalization
OFOBVXE

OFOBVXSHU

OFOBVXSHU

OFOBVXE

OFOBVXE

SAP AG 2002

n n

Inheritance should be used to implement generalization and specialization relationships. A superclass is a JHQHUDOL]DWLRQ of its subclasses. The subclass in turn is a VSHFLDOL]DWLRQ of its superclasses. The situation in which a class, for example lcl_sub2, inherits from two classes (lcl_super1 and lcl_super2) simultaneously, is known as multiple inheritance. However, this is QRW implemented in ABAP Objects. ABAP Objects only has single inheritance. You can, however, simulate multiple inheritance in ABAP Objects using interfaces (see the section on interfaces). Single inheritance does not mean that the inheritance tree only has one level. On the contrary, the direct superclass of one class can in turn be the subclass of a further superclass. In other words: The inheritance tree can have any number of levels, so that a class can inherit from several superclasses, as long as it only has one direct superclass. Inheritance is a "one-sided relationship": Subclasses know their direct superclasses, but (super)classes do not know their subclasses.

SAP AG

BC401

Specialization
8-5

buRuuxx u ~}

5HODWLRQVKLSV %HWZHHQ 6XSHUFODVVHV DQG 6XEFODVVHV

l &RPPRQ FRPSRQHQWV RQO\ H[LVW RQFH LQ WKH VXSHUFODVV


1HZ FRPSRQHQWV LQ WKH VXSHUFODVV DUH DXWRPDWLFDOO\ DYDLODEOH LQ VXEFODVVHV $PRXQW RI QHZ FRGLQJ LV UHGXFHG SURJUDPPLQJ E\ GLIIHUHQFH

n n

OFOBVXSHU

l 6XEFODVVHV DUH H[WUHPHO\ GHSHQGHQW RQ VXSHUFODVVHV

SAP AG 2002

If inheritance is used properly, it provides a significantly better structure, as FRPPRQ FRPSRQHQWV RQO\ QHHG WR EH VWRUHG RQFH FHQWUDOO\ (in the superclass) and are then automatically available to subclasses. Subclasses also benefit from modifications (however, they can also be invalidated as a result). Inheritance provides very strong links between the superclass and the subclass. The subclass must possess detailed knowledge of the implementation of the superclass, particularly for redefinition, but also in order to use inherited components. Even if the superclass does not technically know its subclasses, the subclass often makes additional requirements of the superclass, for example, because a subclass needs certain protected components or because implementation details in the superclass need to be changed in the subclass in order to redefine methods. The basic reason is that the developer of a (super)class cannot normally predict all the requirements that subclasses will later need to make of the superclass.

SAP AG

:KLWH %R[ 5HXVH  6XEFODVV PXVW SRVVHVV GHWDLOHG NQRZOHGJH RI WKH LPSOHPHQWDWLRQ RI WKH VXSHUFODVV

1HZ FRPSRQHQWV

OFOBVXE

,QKHULWHG FRPSRQHQWV

BC401

8-6

,QKHULWDQFH 6\QWD[

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 HVWLPDWHBIXHO ,03257,1* LPBGLVWDQFH 7<3( VBGLVWDQFH 5(7851,1* 9$/8( UHBIXHO 7<3( W\BIXHO 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ (1'&/$66
...

&/$66 OFOBWUXFN '(),1,7,21 ,1+(5,7,1* )520 OFOBYHKLFOH 38%/,& 6(&7,21 0(7+2'6 JHWBFDUJR 5(7851,1* 9$/8( UHBFDUJR 7<3( W\BFDUJR 35,9$7( 6(&7,21 '$7$ PD[BFDUJR 7<3( W\BFDUJR (1'&/$66
SAP AG 2002

Normally the only other entry required for subclasses is what has changed in relation to the direct superclass. Only DGGLWLRQV are permitted in ABAP Objects, that is, in a subclass you can "never take something away from a superclass". All components from the superclass are automatically present in the subclass. The attributes of the superclass lcl_vehicle exist in the subclass lcl_truck; the method estimate_fuel is also available in the subclass. The subclass defines a method get_cargo. It is not visible in the superclass.

n n

SAP AG

BC401

8-7

5HGHILQLQJ 0HWKRGV
&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 HVWLPDWHBIXHO ,03257,1* LPBGLVWDQFH 7<3( W\BGLVWDQFH 5(7851,1* 9$/8( UHBIXHO 7<3( W\BIXHO (1'&/$66
&/$66 OFOBWUXFN '(),1,7,21 ,1+(5,7,1* )520 OFOBYHKLFOH 38%/,& 6(&7,21 0(7+2'6 HVWLPDWHBIXHO 5('(),1,7,21  (1'&/$66

&/$66 OFOBWUXFN ,03/(0(17$7,21 0(7+2' HVWLPDWHBIXHO  VXSHU!HVWLPDWHBIXHO  (1'0(7+2' (1'&/$66
SAP AG 2002

&DQQRW FKDQJH WKH LQWHUIDFH

$OZD\V SRLQWV WR WKH VXSHUFODVV FRPSRQHQW

1HZ LPSOHPHQWDWLRQ RI PHWKRG

The REDEFINITION statement for the inherited method must be in the same SECTION as the definition of the original method. (It can therefore not be in the PRIVATE SECTION, since a classs private methods are not visible and therefore cannot be redefined in subclasses). If you redefine a method, you do not need to enter its interface again in the subclass, but only the name of the method. The reason for this is that ABAP Objects does not support overloading. In the case of redefined methods, changing the interface (RYHUORDGLQJ) is not permitted; exception: Overloading is possible with the constructor. Within the redefined method, you can access components of the direct superclass using the SUPER reference. To implement a redefined method in a subclass, you often need to call the method of the same name in the LPPHGLDWH superclass. In ABAP Objects you can call the method from the superclass using the pseudo-reference super: The pseudo-reference super can only be used in redefined methods.

n n n n

SAP AG

BC401

8-8

,QKHULWDQFH DQG 5HGHILQLQJ WKH &RQVWUXFWRU


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 FRQVWUXFWRU ,03257,1* LPBPDNH 7<3( VWULQJ (1'&/$66
&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' &216758&725 PDNH LPBPDNH (1'0(7+2' (1'&/$66

&/$66 OFOBWUXFN '(),1,7,21 ,1+(5,7,1* )520 OFOBYHKLFOH 38%/,& 6(&7,21 0(7+2'6 FRQVWUXFWRU ,03257,1* LPBPDNH 7<3( VWULQJ LPBFDUJR 7<3( W\BFDUJR 35,9$7( 6(&7,21 '$7$ PD[BFDUJR 7<3( W\BFDUJR (1'&/$66 &DQ FKDQJH WKH LQWHUIDFH &/$66 OFOBWUXFN ,03/(0(17$7,21 0(7+2' FRQVWUXFWRU LPBPDNH  &$// 0(7+2' VXSHU!FRQVWUXFWRU LPBPDNH PD[BFDUJR LPBFDUJR (1'0(7+2' &DOO FRQVWUXFWRU RI LPPHGLDWH (1'&/$66 VXSHUFODVV
SAP AG 2002

The constructor of the superclass PXVW be called within the constructor of the subclass. The reason for this is the special function of the constructor: To ensure that objects are initialized correctly. Only the class itself, however, can initialize its own (private) components correctly; this task cannot be carried out by the subclass. Therefore it is essential that all (instance) constructors are called in an inheritance hierarchy (in the correct sequence). For static constructors, unlike instance constructors, the static constructor in the superclass is called automatically, that is the runtime system automatically ensures that the static constructors of all its superclasses have already been executed before the static constructor in a particular class is executed.

SAP AG

BC401

8-9

5XOHV IRU &DOOLQJ WKH &RQVWUXFWRU


'$7$ UB 7<3( 5() 72 OFOB UB 7<3( 5() 72 OFOB &5($7( 2%-(&7 UB (;3257,1* LPBD &5($7( 2%-(&7 UB (;3257,1* LPBD LPBD   

OFOB
a1 constructor (im_a1:i)

SAP AG 2002

n n

The model described for instance constructors must also be taken into account for CREATE OBJECT. There are two main methods of creating an instance of a class using CREATE OBJECT: 1. The class has a defined (and implemented) instance constructor In this case, when you are using CREATE OBJECT, the parameters have to be filled according to the constructor interface, that is, optional parameters may, and non-optional parameters must be filled with actual parameters. If the constructor does not have any (formal) parameters, no parameters may or can be filled. 2. The instance constructor for that class has not been defined In this case, you must search the inheritance hierarchy for the next highest superclass in which the instance constructor has been defined and implemented. Then, when you are using CREATE OBJECT, the parameters of that class must be filled (similarly to the first method above). If there is no superclass with a defined instance constructor, then no parameters may or can be filled.

If no instance constructor has been defined for a class, then a GHIDXOW FRQVWUXFWRU ZKLFK LV LPSOLFLWO\ DOZD\V SUHVHQW is used. This default constructor calls the constructor from the immediate superclass.

SAP AG

BC401

&ODVV RI LQVWDQFH WR EH FUHDWHG FRQVWUXFWRU 6HDUFK IRU WKH QH[W VXSHUFODVV ZLWK D FRQVWUXFWRU LQ WKH LQKHULWDQFH WUHH )LOO LWV SDUDPHWHUV

OFOB

&ODVV RI LQVWDQFH WR EH FUHDWHG )LOO LWV SDUDPHWHUV

FRQVWUXFWRU

OFOB
a2 constructor (im_a1, im_a2)

8-10

,QKHULWDQFH DQG 9LVLELOLW\


&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21
...

3XEOLF FRPSRQHQWV
9LVLEOH WR DOO

3URWHFWHG FRPSRQHQWV

'LUHFW DFFHVV

3527(&7(' 6(&7,21 '$7$ WDQN 7<3( 5() 72 OFOBWDQN 35,9$7( 6(&7,21 '$7$ PDNH 7<3( VWULQJ (1'&/$66

3ULYDWH FRPSRQHQWV

FRQVWUXFWRU GLVSOD\BDWWULEXWHV JHWBFRXQW


SAP AG 2002

Inheritance provides an extension of the visibility concept: There are protected components. The visibility of these components lies between that of the public components (visible to all users, all subclasses, and the class itself), and private (visible only to the class itself). Protected components are visible to and can be used by all subclasses and the class itself. Subclasses cannot access the private components (particularly attributes) of the superclass. Private components are genuinely private. This is particularly important if a (super)class needs to make local enhancements to handle errors: It can use private components to do this without knowing or invalidating subclasses. In ABAP Objects, you must keep to the section sequence PUBLIC, PROTECTED, PRIVATE.

SAP AG

BC401

|x bD ~ xxu

2QO\ YLVLEOH ZLWKLQ WKH FODVV DQG LWV VXEFODVVHV 2QO\ YLVLEOH ZLWKLQ WKH FODVV

1R DFFHVV IURP RXWVLGH WKH FODVV QRW HYHQ IURP WKH VXEFODVV

 PDNH  PRGHOO WDQN ...

OFOBYHKLFOH

8-11

9LVLELOLW\ RI 3URWHFWHG &RPSRQHQWV


&DQ EH DGGUHVVHG IURP RXWVLGH IRU DOO FOLHQWV

 OFOBEXV
Public

,QKHULWHG

Private

JHWBPDNH VHWBPDNH JHWBFRXQW GLVSOD\BDWWULEXWHV HVWLPDWHBIXHO

Protected

,QKHULWHG

WDQN

&DQ RQO\ EH DGGUHVVHG ZLWKLQ WKH FODVV


SAP AG 2002

In this example, lcl_bus, a subclass of lcl_vehicle, can directly access the protected attribute tank. If the attribute was private, the subclasses would only be able to access tank using nonprivate methods.

SAP AG

BC401

8-12

5XOHV IRU 5HGHILQLQJ 0HWKRGV


l ,QKHULWHG PHWKRGV FDQ EH UHGHILQHG LQ VXEFODVVHV

n n n n

5HGHILQHG PHWKRGV PXVW EH UHLPSOHPHQWHG LQ VXEFODVVHV 7KH VLJQDWXUH RI UHGHILQHG PHWKRGV FDQQRW EH FKDQJHG 6WDWLF PHWKRGV FDQQRW EH UHGHILQHG

SAP AG 2002

In ABAP Objects, you can not only add new components, but also provide inherited methods with new implementations. This is known as UHGHILQLWLRQ. You can only redefine (public and protected) instance methods, other components (static methods, attributes and so on) cannot be redefined. Changes to method parameters (signature changes) are not possible. In UML, the redefinition of a method is represented by listing the method again in the subclass. Methods (and all other components) that are inherited but not redefined are not listed in the subclass, as their existence there is clear from the specialization relationship. You should not confuse redefinition with "overloading". The latter describes the ability of a class to have methods with the same name but a different signature. This is not available in ABAP Objects. There is only one static event per roll area. In this way, a class that defines a public or protected static attribute shares this attribute with all its subclasses. The significant point here is that subclasses do not each receive their own copy of the static attribute.

SAP AG

BC401

0iFvRF0%

DDb|%DdxuxDbDxx ~ u

0i8P%W%

i8v%%R

,Q LQKHULWDQFH VWDWLF FRPSRQHQWV DUH VKDUHG  $ FODVV VKDUHV LWV QRQSULYDWH VWDWLF DWWULEXWHV ZLWK DOO LWV VXEFODVVHV

OFOBYHKLFOHV

HVWLPDWHBIXHO

OFOBFDU

OFOBWUXFN

OFOBEXV

8-13

5HGHILQLQJ 0HWKRGV ([DPSOH


OFOBEXV
OFOBWUXFN

 PD[BSDVVHQJHUV ...
 FRQVWUXFWRU  HVWLPDWHBIXHO

 PD[BFDUJR ...

 FRQVWUXFWRU  HVWLPDWHBIXHO

0(7+2' HVWLPDWHBIXHO '$7$ WRWDOBZHLJKW  * just an example! WRWDOBZHLJKW PD[BSDVVHQJHUV DYHUDJHBZHLJKW  ZHLJKW UHBIXHO WRWDOBZHLJKW LPBGLVWDQFH IDFWRU (1'0(7+2'

0(7+2' HVWLPDWHBIXHO '$7$ WRWDOBZHLJKW  * just an example! WRWDOBZHLJKW PD[BFDUJR  ZHLJKW UHBIXHO WRWDOBZHLJKW LPBGLVWDQFH IDFWRU (1'0(7+2'

SAP AG 2002

In the above example, both redefined methods calculate the return code in different ways. The important point is that the semantics stay the same.

SAP AG

BC401

8-14

,QKHULWDQFH 8QLW 6XPPDU\

l 'HILQH DQ LQKHULWDQFH UHODWLRQVKLS EHWZHHQ FODVVHV l 5HGHILQH PHWKRGV

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

8-15

,QKHULWDQFH ([HUFLVHV
8QLW ,QKHULWDQFH 7RSLF &UHDWLQJ &ODVV +LHUDUFKLHV
At the conclusion of these exercises, you will be able to: Define subclasses Redefine superclass methods in subclasses 0RGHO VROXWLRQ 6$3%&B,1+6B0$,1B$ 6$3%&B,1+6B$ include program <RXU SURJUDP =%&B 1-1 =%&B B0$,1B$,53/$1( B$,53/$1( include program

Make both instance attributes of the class OFOBDLUSODQH visible to their subclasses (PRIVATE SECTION -> PROTECTED SECTION). 1-2 Create the subclass OFOBSDVVHQJHUBSODQH for the class OFOBDLUSODQH. Also, create this subclass in your include program. 1-2-1 The class is to have a private instance attribute PD[BVHDWV with the same type as table field VIOLJKWVHDWVPD[. 1-2-2 A public constructor is to be defined and implemented in the class. This constructor provides DOO instance attributes in the class with values. 1-2-3 Redefine the method GLVSOD\BDWWULEXWHV of the class OFOBDLUSODQH, so that, using the redefined method, the WRITE statement displays DOO instance attributes. 1-3 Create the subclass OFOBFDUJRBSODQH for the class OFOBDLUSODQH. Also, create this subclass in your include program. 1-3-1 The class is to have a private instance attribute PD[BFDUJR with the same type as the table field VFSODQHFDUJRPD[. 1-3-2 A public constructor is to be defined and implemented in the class. This constructor provides DOO instance attributes in the class with values. 1-3-3 Redefine the method GLVSOD\BDWWULEXWHV of the class OFOBDLUSODQH, so that, using the redefined method, the WRITE statement displays DOO instance attributes.

SAP AG

BC401

8-16

1-4

Switch to your main program. 1-4-1 Use the DATA statement to create a reference for each subclass (OFOBSDVVHQJHUBSODQH, OFOBFDUJRBSODQH). 1-4-2 Call the static method GLVSOD\BQBRBDLUSODQHV (before instantiating any objects). 1-4-3 Use the two references to create one instance each of the subclasses OFOBSDVVHQJHUBSODQH and OFOBFDUJRBSODQH. Decide for yourself how to fill the attributes. 1-4-4 Call the GLVSOD\BDWWULEXWHV method for both instances. 1-4-5 Call the static method GLVSOD\BQBRBDLUSODQHV a second time.

1-5

Follow the program flow in the Debugger, paying special attention to the call of GLVSOD\BDWWULEXWHV. Can the method JHWBWHFKQLFDOBDWWULEXWHV be called directly from the redefined method GLVSOD\BDWULEXWHV of the subclass? Is it necessary for the subclasses to directly access the attributes QDPH and SODQHW\SH of the superclass to initialize them? Or, to formulate it differently: If these attributes remained in the private visibility area of the superclass, how would the subclasses have to access the attributes?

1-6

1-7

SAP AG

BC401

8-17

,QKHULWDQFH 6ROXWLRQV
7RSLF 8QLW ,QKHULWDQFH &UHDWLQJ &ODVV +LHUDUFKLHV

*&---------------------------------------------------------------------* *& Report *& *& SAPBC401_INHS_MAIN_a * * * *&---------------------------------------------------------------------* the classes lcl_passenger_plane and lcl_cargo_plane are instantiated. Inheritance is shown

*&---------------------------------------------------------------------* REPORT sapbc401_inhs_main_a.

INCLUDE <icon>. INCLUDE sapbc401_inhs_a. '$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH

UBFDUJR W\SH UHI WR OFOBFDUJRBSODQH

UBSDVVHQJHU W\SH UHI WR OFOBSDVVHQJHUBSODQH

SODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH START-OF-SELECTION. *############################## lcl_airplane=>display_n_o_airplanes( ). &5($7( 2%-(&7 UBSDVVHQJHU (;3257,1* LPBQDPH LPBSODQHW\SH LPBVHDWV &5($7( 2%-(&7 UBFDUJR (;3257,1* LPBQDPH

/+ %(5/,1




LPBSODQHW\SH UBFDUJR!GLVSOD\BDWWULEXWHV  LPBFDUJR

86 +(UFXOHV




UBSDVVHQJHU!GLVSOD\BDWWULEXWHV  lcl_airplane=>display_n_o_airplanes( ).

SAP AG

BC401

8-18

*&---------------------------------------------------------------------* *& Include SAPBC401_INHS_a * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * *... CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------*

*---------------------------------------------------------------------* * * *---------------------------------------------------------------------* &/$66 OFOBFDUJRBSODQH '(),1,7,21 ,1+(5,7,1* )520 OFOBDLUSODQH 38%/,& 6(&7,21 CLASS lcl_cargo_plane DEFINITION *---------------------------------------------------------------------*

0(7+2'6 FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ



LPBSODQHW\SH 7<3( VDSODQHSODQHW\SH LPBFDUJR 7<3( VFSODQHFDUJRPD[

0(7+2'6 GLVSOD\BDWWULEXWHV 5('(),1,7,21 35,9$7( 6(&7,21

'$7$ PD[BFDUJR 7<3( VFSODQHFDUJRPD[ (1'&/$66 OFOBFDUJRBSODQH '(),1,7,21



SAP AG

BC401

8-19

*---------------------------------------------------------------------* * * *---------------------------------------------------------------------* &/$66 OFOBFDUJRBSODQH ,03/(0(17$7,21 CLASS lcl_cargo_plane IMPLEMENTATION *---------------------------------------------------------------------*

0(7+2' FRQVWUXFWRU

&$// 0(7+2' VXSHU!FRQVWUXFWRU LPBQDPH LPBSODQHW\SH PD[BFDUJR LPBFDUJR FRQVWUXFWRU

LPBQDPH LPBSODQHW\SH 

(1'0(7+2'

0(7+2' GLVSOD\BDWWULEXWHV :5,7( 


0D[ &DUJR (1'0(7+2' 8/,1(

VXSHU!GLVSOD\BDWWULEXWHV 

 PD[BFDUJR GLVSOD\BDWWULEXWHV

(1'&/$66

OFOBFDUJRBSODQH ,03/(0(17$7,21

*---------------------------------------------------------------------* * * *---------------------------------------------------------------------* &/$66 OFOBSDVVHQJHUBSODQH '(),1,7,21 ,1+(5,7,1* )520 OFOBDLUSODQH 38%/,& 6(&7,21 CLASS lcl_passenger_plane DEFINITION *---------------------------------------------------------------------*

0(7+2'6 FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ

LPBSODQHW\SH 7<3( VDSODQHSODQHW\SH LPBVHDWV 7<3( VIOLJKWVHDWVPD[

0(7+2'6 GLVSOD\BDWWULEXWHV 5('(),1,7,21 35,9$7( 6(&7,21 (1'&/$66

'$7$ PD[BVHDWV 7<3( VIOLJKWVHDWVPD[

OFOBSDVVHQJHUBSODQH '(),1,7,21

SAP AG

BC401

8-20

*---------------------------------------------------------------------* * * *---------------------------------------------------------------------* &/$66 OFOBSDVVHQJHUBSODQH ,03/(0(17$7,21 CLASS lcl_passenger_plane IMPLEMENTATION *---------------------------------------------------------------------*

0(7+2' FRQVWUXFWRU

&$// 0(7+2' VXSHU!FRQVWUXFWRU LPBQDPH PD[BVHDWV LPBVHDWV

LPBSODQHW\SH

LPBQDPH

LPBSODQHW\SH 

(1'0(7+2'

FRQVWUXFWRU

0(7+2' GLVSOD\BDWWULEXWHV :5,7( 


0D[ 6HDWV (1'0(7+2' ENDCLASS. 8/,1(

VXSHU!GLVSOD\BDWWULEXWHV 

 PD[BVHDWV GLVSOD\BDWWULEXWHV "lcl_passenger_plane IMPLEMENTATION

SAP AG

BC401

8-21

&DVWLQJ &RQWHQWV
l &DVW l 3RO\PRUSKLVP

SAP AG 2002

SAP AG

BC401

9-1

&DVWLQJ 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 8VH FDVWV l 'HYHORS JHQHULF SURJUDPV XVLQJ SRO\PRUSKLVP ZLWK LQKHULWDQFH

SAP AG 2002

SAP AG

BC401

9-2

y $x R P S U T 8 8 EB "@A H FGP E8 CD 9@A 8 A 8 P QI8 F B D B 9 7 h W fc e g fY &c CY 5CY $V e d b a ` XW  &$ "   %# ! ) '  ' 3  %  120(!  !  6   6  ! 45        igSSQ C "$ j hf e d e k & m o on m l j " C p nj q y xw v s QvGutr

fX eb c i w u s XY W b c vh te b e i qp r Y e W c &g be e i

SAP AG
SAP AG 2002

&DVWLQJ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

9-3

&DVWLQJ 

1DUURZLQJ FDVW 1DUURZLQJ FDVW


:LGHQLQJ FDVW :LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP ,QKHULWDQFH DQG SRO\PRUSKLVP

SAP AG 2002

SAP AG

BC401

9-4

3ULQFLSOH RI 1DUURZLQJ &DVW


'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBWUXFN 7<3( 5() 72 OFOBWUXFN

&5($7( 2%-(&7 UBWUXFN


Public

 OFOBWUXFN
*HHUEW
JHWBPDNH JHWBFRXQW

1DUURZLQJ &DVW UBYHKLFOH UBWUXFN

SAP AG 2002

After the narrowing cast, you can use the r_vehicle reference to access the components of the truck instance that were inherited from lcl_vehicle - obviously, in some cases with the limitations entailed by their visibility. You can no longer access the truck-specific part of the instance (get_cargo in the above example) using the r_vehicle reference.

SAP AG

{ v5vz

{ v5vz

S~|z z} {
BC401

JHWBFDUJR 

GLVSOD\BDWWULEXWHV VHWBDWWULEXWHV HVWLPDWHBIXHO

5HGHILQLHUW

9-5

&RPSDWLELOLW\ DQG 1DUURZLQJ &DVW


l l l ,QVWDQFHV IURP VXEFODVVHV FDQ EH XVHG LQ DQ\ FRQWH[W LQ ZKLFK WKH LQVWDQFHV RI WKH VXSHUFODVV DSSHDU 7KH FRPSRQHQWV LQKHULWHG IURP WKH VXSHUFODVV FDQ EH DGGUHVVHG *HQHULF DFFHVV E\ WKH FOLHQW LV SRVVLEOH

'$7$ UBYHKLFOH UBWUXFN OHYHO

7<3( 5() 72 OFOBYHKLFOH 7<3( 5() 72 OFOBWUXFN 7<3( W\BOHYHO

JHQHULF DFFHVV WR VXEFODVV VHUYLFHV OHYHO UBWUXFN!HVWLPDWHBIXHO   1DUURZLQJ &DVW UBYHKLFOH UBWUXFN 8VH RI WKH VXEFODVV LQVWDQFH LQ WKH VXSHUFODVV FRQWH[W OHYHO UBYHKLFOH!HVWLPDWHBIXHO  
SAP AG 2002

One of the significant principles of inheritance is that an instance from a subclass can be used in any context, in which an instance from the superclass appears. This is possible because the subclass has inherited DOO components from the superclass and therefore has the same interface as the superclass. The user can therefore address the subclass instance in the same way as the superclass instance. Variables that point to a superclass instance can also refer to subclass instances at runtime. The assignment of a subclass instance to a reference variable of the type "reference to superclass" is described as a narrowing cast, because you are switching from a more detailed view to a one with less detail. The description "up-cast" is also used. What is a narrowing cast used for? A user who is not interested in the finer points of cars, trucks, and busses (but only, for example, in the fuel consumption and tank gauge) does not need to know about them. This user only wants and needs to work with (references to) the lcl_vehicle class. However, in order to allow the user to work with cars, busses, or trucks, you generally need a narrowing cast.

n n

n n

SAP AG

BC401

9-6

*HQHULF $FFHVV"

OFOBUHQWDO
7KH FOLHQW

1..*

OFOBYHKLFOHV
OFOBEXV

OFOBWUXFN
 OFOBUHQWDO
Public Private

SAP AG 2002

A possible client (car rental company for example) wants to access the list of vehicles with identically-named services. The client wishes to, for example, calculate the required amount of fuel. The client is not concerned with the details of how the car, bus, or truck do this.

SAP AG

BC401

i i i C

2| i
i2 i C
9-7

LWDE

3UHSDUDWLRQV IRU *HQHULF $FFHVV 

7KH FOLHQW
Public

Private

... ...

0(7+2'6 DGGBYHKLFOH ,03257,1* LPBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH


SAP AG 2002

Objects from different classes (lcl_bus, lcl_truck, and lcl_car in the above example ) can be stored in an internal table consisting of references to the superclass (lcl_vehicle in the above example),and then processed in a uniform manner using the same access technique. A client, the car rental company in this example, can then generically access the identically-named service of the different classes. (This will be discussed in more detail.)

SAP AG

BC401

2 i 

i i

DGGBYHKLFOH

v i
9-8

 OFOBUHQWDO
0tg

3UHSDUDWLRQV IRU *HQHULF $FFHVV 


&/$66 OFOBUHQWDO '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 DGGBYHKLFOH ,03257,1* LPBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH 0(7+2'6 FDOFBHVWLPDWHGBIXHO 5(7851,1* 9$/8( UHBIXHO ... 3527(&7(' 6(&7,21 '$7$ YHKLFOHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBYHKLFOH (1'&/$66
&/$66 OFOBUHQWDO ,03/(0(17$7,21 0(7+2' DGGBYHKLFOH $33(1' LPBYHKLFOH 72 YHKLFOHBOLVW (1'0(7+2' 0(7+2' FDOFBHVWLPDWHGBIXHO '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH /223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH UHBIXHO UHBIXHO  UBYHKLFOH!HVWLPDWHBIXHO LPBGLVWDQFH . (1'/223 (1'0(7+2' (1'&/$66
SAP AG 2002

When objects from different classes react differently to the same method call, this is known as SRO\PRUSKLVP. To do this, the classes implement the same method in different ways. This can be done using inheritance, by redefining a method from the superclass in subclasses and implementing it differently. (,QWHUIDFHV will be discussed later; they too can enable polymorphic behavior.) When an instance receives a message to execute a particular method, then that method is executed if it has been implemented by the class the instance belongs to. If the class has not implemented that method, but only inherited and not redefined it, then a search up through the inheritance hierarchy is carried out until an implementation of that method is found. The dynamic type, not the static type of the reference variable is used to search for the implementation of a method (will be discussed later). r_vehicle->estimate_fuel above therefore uses the class of the instance that r_vehicle actually refers to to search for the implementation of estimate_fuel. The static type for r_vehicle, which is always REF TO lcl_vehicle is not used. Polymorphism is one of the main strengths of inheritance: The user can work in the same way with different classes, regardless of their implementation. The search for the right implementation of a method is carried out by the runtime system, not the user.

SAP AG

BC401

9-9

3RO\PRUSKLVP  *HQHULF $FFHVV


0(7+2' FDOFBHVWLPDWHGBIXHO '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH /223 $7 YHKLFOHBOLVW LQWR UBYHKLFOH UHBIXHO UHBIXHO  UBYHKLFOH!HVWLPDWHBIXHO LPBGLVWDQFH (1'/223 (1'0(7+2'
0(7+2' HVWLPDWHBIXHO ... WRWDOBZHLJKW PD[BSDVVHQJHUV DYHUDJHBZHLJKW  ZHLJKW UHBIXHO WRWDOBZHLJKW LPBGLVWDQFH IDFWRU (1'0(7+2'
0(7+2' HVWLPDWHBIXHO ... WRWDOBZHLJKW PD[BFDUJR  ZHLJKW UHBIXHO WRWDOBZHLJKW LPBGLVWDQFH IDFWRU (1'0(7+2'
Gi  E&
Public

SAP AG 2002

Which coding is actually executed when estimate_fuel is called depends on the dynamic type of the reference variable r_vehicle, that is it depends on which object from which (sub)class r_vehicle refers to. You can use polymorphism to write programs that are generic to a high degree and that do not even need to be changed if use cases are added. In the simple example above, this means that, should a further subclass be added, for example for motorbikes, the above coding would not need to be changed. A redefined method will be created in the server class lcl_motorbike.

SAP AG

$ &

|v&v|i vvii ECiii

|v&vi vv&ii Ciii

Public

0tg

BC401

9-10

6WDWLF DQG '\QDPLF 7\SHV RI 5HIHUHQFHV

7KH VWDWLF W\SH RI D UHIHUHQFH YDULDEOH


,V GHILQHG XVLQJ 7<3( 5() 72

'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH

5HPDLQV FRQVWDQW WKURXJKRXW WKH SURJUDP IORZ

7KH G\QDPLF W\SH RI D UHIHUHQFH YDULDEOH


,V GHILQHG E\ DVVLJQPHQW &DQ FKDQJH GXULQJ WKH SURJUDP UXQ

'HILQHV ZKLFK DWWULEXWHV DQG PHWKRGV FDQ EH DGGUHVVHG

'HILQHV ZKLFK FRGH LV WR EH H[HFXWHG IRU UHGHILQHG PHWKRGV

SAP AG 2002

A reference variable always has two types, static and dynamic: The static type of a reference variable is determined by variable definition using TYPE REF TO. It cannot and does not change. It specifies which attributes and methods can be addressed The dynamic type of a reference variable is the type of the instance currently being referred to, it is therefore determined by assignment and can change during the program run. It defines what code is to be executed for redefined methods.

n n

In the example, the static type of the r_vehicle variable is always REF TO lcl_vehicle, but its dynamic type after the cast is REF TO lcl_bus or REF TO lcl_truck. In the Debugger, the reference me can be used to determine the dynamic type.

SAP AG

BC401

|v&v|i

|v&vi
9-11

||2 2&i iii i|i|i 2| i

||2 22&i iii i|i|i i v v

vv&v|

&RPSDULVRQ WR 3URFHGXUDO 3URJUDPPLQJ


l l ,Q REMHFWRULHQWHG SURJUDPPLQJ \RX GR QRW QHHG WR FKDQJH WKH FRGLQJ LI \RX DGG XVH FDVHV ,Q D SURFHGXUDO SURJUDP \RX ZRXOG KDYH WR DGMXVW &$6( FRQVWUXFWLRQV IRU H[DPSOH

3URFHGXUDO UHDOLVDWLRQ RI WKH H[DPSOH ZLWKRXW SRO\PRUSKLVP '$7$ YHKLFOHBOLVW 7<3( 7$%/( 2) YHKLFOHBOLVWBW\SH YHKLFOH 7<3( YHKLFOHBOLVWBW\SH   YHKLFOHBOLVW /223 $7 YHKLFOHBOLVW ,172 YHKLFOH name FDWHJRU\ &$6( YHKLFOHFDWHJRU\ :+(1
758&.
 truck1 3(5)250 HVWLPDWHBIXHOBWUXFN 86,1*  bus1 &+$1*,1* IXHO bus2 :+(1
%86
 truck2 3(5)250 HVWLPDWHBIXHOBEXV 86,1*  &+$1*,1* IXHO truck3 (1'&$6( $'' IXHO 72 QHHGHGBIXHO (1'/223
SAP AG 2002

...

Using polymorphism makes generic programming easier. Instead of implementing a CASE or IF statement, you can have one access or call, which improves readability and does not need to be changed if you extend the program by adding further subclasses.

SAP AG

BC401

9-12

&DVWLQJ 

1DUURZLQJ FDVW 1DUURZLQJ FDVW


:LGHQLQJ FDVW :LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP ,QKHULWDQFH DQG SRO\PRUSKLVP

SAP AG 2002

SAP AG

BC401

9-13

8VH RI :LGHQLQJ &DVW

0(7+2' JHWBPD[BFDUJR '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH /223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH 3UREOHP LV WKLV UHDOO\ D WUXFN " 'HWHUPLQH WKH PD[ FDUJR (1'/223 (1'0(7+2'
SAP AG 2002

7UXFN RU RWKHU YHKLFOH"

The client, the car rental company in the above example, wants to execute a function for specific vehicles form the list (vehicle_list). For example, the client wants to ascertain the truck with the largest cargo capacity. However, not all vehicles are in the trucks list, it also includes references to cars and busses.

SAP AG

BC401

0 g Q

i i i

2 i

JHWBPD[BFDUJR

0  t

Public

Private

i| v i
9-14

7KH FOLHQW

 OFOBUHQWDO
0Sg

v| i

:LGHQLQJ &DVW ([DPSOH

(1'75< (1'/223 (1'0(7+2'


SAP AG 2002

&$7&+ F[BV\BPRYHBFDVWBHUURU UHDFW RQ WKDW FDVW HUURU

The type of case described above is known as a widening cast (or "down cast") because it changes from a less detailed view to one with more detail. The target reference (r_truck in the above example) must correspond to the object reference (r_vehicle in the above example), that is the instance must have the details implied by the reference. The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be checked statically, only at runtime. The &DVW 2SHUDWRU ?= (or the equivalent MOVE ... ?TO ) must be used to make this visible. With this kind of cast, a check is carried out at runtime to ensure that the current content of the source variable corresponds to the type requirements of the target variables. In this example, it checks that the dynamic type of the source reference r_vehicle is compatible with the static type of the target reference r_truck. If it is, the assignment is carried out. Otherwise, an exception that can be handled is raised, and the original value of the target variable remains the same. This exception of the error class CX_SY_MOVE_CAST_ERROR can be caught using TRY-ENDTRY and the CATCH statement. (This will be discussed in more detail later.) Another way of preventing the runtime error would be to use RTTI (Runtime Type Identification). This is a class library for ascertaining type attributes at runtime.

SAP AG

BC401

0 g Q

0(7+2' JHWBPD[BFDUJR '$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH UBWUXFN 7<3( 5() 72 OFOBWUXFN /223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH 75< UBWUXFN " UBYHKLFOH SXW PD[ FDUJR LQ YDULDEOH UHBFDUJR

0  t

i 2 Q

i| i t
2 i Q

iii2|vv

 OFOBUHQWDO
Qi0tE |

v| i
9-15

CGSi

&DVWLQJ 

1DUURZLQJ FDVW 1DUURZLQJ FDVW


:LGHQLQJ FDVW :LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP ,QKHULWDQFH DQG SRO\PRUSKLVP

SAP AG 2002

SAP AG

BC401

9-16

6HPDQWLFV DQG 8VH ZLWK ,QKHULWDQFH

5XOHV

l l l

:KHQ \RX UHGHILQH D PHWKRG LWV VHPDQWLFV PXVW UHPDLQ WKH VDPH \RX FDQQRW FKDQJH WKH VLJQDWXUH 8VLQJ LQKHULWDQFH l $OORZV \RX WR HQKDQFH FODVVHV XVLQJ JHQHUDOL]DWLRQVSHFLDOL]DWLRQ DQG KHQFH DFKLHYH EHWWHU VRIWZDUH VWUXFWXUH

,QKHULWHG FRPSRQHQWV PXVW EHKDYH LQ VXEFODVVHV H[DFWO\ DV WKH\ GR LQ VXSHUFODVVHV IRU DOO XVHUV

l 3URYLGHV SRVVLELOLW\ RI SRO\PRUSKLF EHKDYLRU JHQHULF SURJUDPPLQJ  &$6( FRQVWUXFWLRQV QR ORQJHU QHHGHG 0XVW QRW EH XVHG VROHO\ IRU FRGH LQKHULWDQFH

SAP AG 2002

A subclass instance can be used in any context in which a superclass instance also appears. Moreover: The user does not and is not intended to know whether they are dealing with a subclass or a superclass instance. The user works only with references to the superclass and must rely on the inherited components behaving in the subclass instances exactly as they do in the superclass instances, otherwise the program will not work. On the other hand, this ensures useful restrictions on the implementation of the subclasses: Inherited components must keep their inherited semantics. You cannot use inherited attributes or events in any way other than intended in the superclass, and you cannot change method semantics by redefinition. You must avoid "code inheritance": It is not correct for one class to inherit from another simply because SDUW of the functionality required is already implemented there.

SAP AG

BC401

9-17

,QFRUUHFW 8VH RI ,QKHULWDQFH

SAP AG 2002

FKDQJHBKHLJKW

Using inheritance instead of attributes, or a misunderstanding of inheritance as an is-one relationship often leads to the following kind of design: the superclass FDU has the subclasses UHG FDU, JUHHQ FDU, and so on. These subclasses all have an identical structure and identical behavior. Because an instance cannot change its class, in circumstances like the following, you should not use inheritance directly, but use a so-called role design pattern instead: The class HPSOR\HH has the subclasses IXOOWLPH HPSOR\HH and SDUWWLPH HPSOR\HH. What happens when a part-time employee becomes a full-time employee? A new full-time employee object would have to be instantiated and all the information copied from the part-time employee object. However, users who still have a reference to the part-time employee instance would then be working with a part-time employee object that logically does not exist anymore. The use of inheritance does not always correspond to expectations in the real world: For example, if the class VTXDUH inherits from the class UHFWDQJOH, the latter will have two separate methods for changing length or width, although the sides of the square actually need to be changed by the same measurement.

SAP AG

BC401

 C 

  

5XOHV

,QKHULWDQFH LV RIWHQ XVHG LQFRUUHFWO\ 7R VLPSO\ UHXVH FRGH ,QVWHDG RI DGGLWLRQDO DWWULEXWHVDJJUHJDWLRQUROH FRQFHSWV
FDU
GH VL JQ "

FDUBUHG

FDUBEOXH

7KH XVH RI LQKHULWDQFH GRHV QRW DOZD\V FRUUHVSRQG WR H[SHFWDWLRQV LQ WKH UHDO ZRUOG
FKDQJHBZLGWK 6XSHUFODVV
GH VL JQ "

6XEFODVV

9-18

&DVWLQJ 8QLW 6XPPDU\

l 8VH FDVWV

<RX DUH QRZ DEOH WR

l 'HYHORS JHQHULF SURJUDPV XVLQJ SRO\PRUSKLVP ZLWK LQKHULWDQFH

SAP AG 2002

SAP AG

BC401

9-19

&DVWLQJ ([HUFLVHV
8QLW &DVWLQJ 7RSLF 3RO\PRUSKLVP
At the conclusion of these exercises, you will be able to: Describe polymorphism and inheritance Use generic programming for inheritance relationships and implement polymorphic method calls 0RGHO VROXWLRQ 6$3%&B&$66B0$,1B$ 6$3%&B,1+6B$ include program

1-1

Extend your existing program =%&B B0$,1B$,53/$1( or start with the model solution from the last chapters exercises. In your main program, define an internal table for buffering airplane objects. The type of the internal table should be 5() 72 OFOBDLUSODQH. Try to insert the planes (passenger and cargo) into this internal table and execute the GLVSOD\BDWWULEXWHV method for every plane in a LOOP. Read the internal table in the LOOP using the auxiliary reference variable UBDLUSODQH (type 5() 72 OFOBDLUSODQH). 1-3-1 Was this successful? 1-3-2 Check the internal table and the execution of the GLVSOD\BDWWULEXWHV method in the Debugger 1-3-3 Which source code is executed when the GLVSOD\BDWWULEXWHV method is called, the original method from the superclass or the relevant redefined methods from the subclasses? What would happen if one of these methods had not been redefined in the subclass? 0RGHO VROXWLRQ 6$3%&B&$66B0$,1B% 6$3%&B&$66B% include program 6$3%&B&$67B%

1-2

1-3

7HPSODWH

SAP AG

BC401

9-20

2-1

Copy the definition of the class OFOBFDUULHU from the template 6$3%&B&$67B% to your own include program =%&B (hence adding this class to your include program).

B$,53/$1(

2-2

Add the following public instance methods to the class OFOBFDUULHU:

2-2-1 The first method is DGGBDLUSODQH, which adds planes to the list of planes DLUSODQHBOLVW already defined in the class. The transfer parameter is a reference to the class OFOBDLUSODQH. Have a close look at the definition of the internal table DLUSODQHBOLVW. 2-2-2 The second method is GLVSOD\BDLUSODQHV, which displays the planes from the list DLUSODQHBOLVW. The GLVSOD\BDWWULEXWHV method from class OFOBDLUSODQH should be called at this point. 2-2-3 The third method is GLVSOD\BDWWULEXWHV, which displays the airline attributes. This includes the airline name and the list of planes (you can use LFRQBIOLJKW as an icon before the name).

2-3

Go back to your main program. 2-3-1 At the DATA statement, create a reference to the class OFOBFDUULHU. 2-3-2 Comment out all statements affecting the internal table (or delete them). 2-3-3 Create an airline and fill the transfer parameters with data of your own choice. 2-3-4 Add the planes already created in the last exercise (passenger and cargo planes) to the list of planes DLUSODQHBOLVW. To do this, call the method DGGBDLUSODQH of the class OFOBFDUULHU. Create more planes and add them to the airplane list. 2-3-5 Display the airline attributes by calling the GLVSOD\BDWWULEXWHV method of the class OFOBFDUULHU. 2-3-6 Question: Which code is executed when you execute GLVSOD\BDWWULEXWHV within GLVSOD\BDLUSODQHV.

SAP AG

BC401

9-21

&DVWLQJ 6ROXWLRQV
7RSLF 8QLW &DVWLQJ 3RO\PRUSKLVP

*&---------------------------------------------------------------------* *& Report *& *& show casting operations ... SAPBC401_CASS_MAIN_a * * *

*&---------------------------------------------------------------------* *&---------------------------------------------------------------------* REPORT sapbc401_cass_main_a.

INCLUDE <icon>. INCLUDE sapbc401_inhs_a.

DATA: r_plane TYPE REF TO lcl_airplane, r_cargo TYPE REF TO lcl_cargo_plane, r_passenger TYPE REF TO lcl_passenger_plane, SODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH

START-OF-SELECTION. *############################## lcl_airplane=>display_n_o_airplanes( ). CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. $33(1' UBSDVVHQJHU 72 SODQHBOLVW CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500

SAP AG

BC401

9-22

im_cargo = 533. $33(1' UBFDUJR 72 SODQHBOLVW /223 $7 SODQHBOLVW ,172 UBSODQH (1'/223

UBSODQH!GLVSOD\BDWWULEXWHV 

lcl_airplane=>display_n_o_airplanes( ). *&---------------------------------------------------------------------* *& Report *& *& carrier adds airplanes to its airplane_list --> polymorphism SAPBC401_CASS_MAIN_b * * *

*&---------------------------------------------------------------------* *&---------------------------------------------------------------------* REPORT sapbc401_cass_main_b.

INCLUDE <icon>. INCLUDE sapbc401_cass_b.

DATA: r_plane TYPE REF TO lcl_airplane, r_cargo TYPE REF TO lcl_cargo_plane, r_passenger TYPE REF TO lcl_passenger_plane, UBFDUULHU W\SH UHI WR OFOBFDUULHU

START-OF-SELECTION. *############################## ***** Create Carrier ******************************************** FUHDWH REMHFW UBFDUULHU H[SRUWLQJ LPBQDPH
6PLOH )O\7UDYHO


***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************

SAP AG

BC401

9-23

CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ***** insert planes into itab if client *************************** UBFDUULHU!DGGBDLUSODQH UBSDVVHQJHU  UBFDUULHU!DGGBDLUSODQH UBFDUJR  ***** show all airplanes inside carrier *************************** UBFDUULHU!GLVSOD\BDWWULEXWHV 

*---------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION *---------------------------------------------------------------------* * Class definitions of ... lcl_airplane, cargo- and passenger_plane * ... * ... *---------------------------------------------------------------------* * CLASS lcl_carrier DEFINITION *---------------------------------------------------------------------* &/$66 OFOBFDUULHU '(),1,7,21 38%/,& 6(&7,21

0(7+2'6 FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ DGGBDLUSODQH ,03257,1* GLVSOD\BDLUSODQHV 35,9$7( 6(&7,21 '$7$ QDPH (1'&/$66



JHWBQDPH 5(7851,1* YDOXH H[BQDPH 7<3( VWULQJ LPBSODQH 7<3( 5() 72 OFOBDLUSODQH

GLVSOD\BDWWULEXWHV

 DLUSODQHBOLVW W\SH 7$%/( 2) 5() 72 OFOBDLUSODQH 7<3( VWULQJ

SAP AG

BC401

9-24

*---------------------------------------------------------------------* * CLASS lcl_carrier IMPLEMENTATION *---------------------------------------------------------------------* &/$66 OFOBFDUULHU ,03/(0(17$7,21 0(7+2' DGGBDLUSODQH (1'0(7+2'

$33(1' LPBSODQH 72 DLUSODQHBOLVW

0(7+2' GLVSOD\BDWWULEXWHV GLVSOD\BDLUSODQHV 

:5,7( LFRQBIOLJKW $6 ,&21 QDPH  XOLQH XOLQH

(1'0(7+2'

0(7+2' GLVSOD\BDLUSODQHV

GDWD UBSODQH W\SH UHI WR OFOBDLUSODQH ORRS DW DLUSODQHBOLVW LQWR UBSODQH HQGORRS UBSODQH!GLVSOD\BDWWULEXWHV 

(1'0(7+2'

0(7+2' FRQVWUXFWRU (1'0(7+2' QDPH LPBQDPH

0(7+2' JHWBQDPH (1'0(7+2' (1'&/$66 H[BQDPH

QDPH

SAP AG

BC401

9-25

,QWHUIDFHV &RQWHQWV
l ,QWHUIDFHV l &RPSRXQG LQWHUIDFHV l 3RO\PRUSKLVP

SAP AG 2002

SAP AG

BC401

10-1

,QWHUIDFHV 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HILQH DQG LPSOHPHQW LQWHUIDFHV l 'HYHORS JHQHULF SURJUDPV XVLQJ SRO\PRUSKLVP ZLWK LQWHUIDFHV

SAP AG 2002

SAP AG

BC401

10-2

x c c %yw G P Q 7 R7 (A #9@ E CDG (7 A B 89@ 8I7 @ 7 HF7 C A B G 6 g T ea d f eV c `a V YW V %S d b X UT &$ " "   % #!   1) '   ! ' 3  &  20("  5   5 !  " ! 4       20PdPH #D h gf e e i c k m ml k jc h # P n l h p o k h l q z vv u {{ zy w ust vv ycxx w(ssstrr

eU d` a h v r UV T ` a tug sd ` d h pi qV d T a cf `d d h

SAP AG
SAP AG 2002

,QWHUIDFHV ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

10-3

,QWHUIDFHV 

,QWHUIDFHV 3ULQFLSOHV ,QWHUIDFHV 3ULQFLSOHV


:RUNLQJ ZLWK LQWHUIDFHV :RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV 3UHYLHZ &RPSRXQG LQWHUIDFHV

SAP AG 2002

SAP AG

BC401

10-4

,QWHUIDFHV 8VH
6HUYLFH  6HUYLFH  6HUYLFH  
,QWHUIDFHV

"

OFOBFDUULHU

OFOBDLUSODQH

SAP AG 2002



In ABAP Objects, interfaces are implemented in addition to and independently of classes. Interfaces only describe the external point of contact of a class (protocols), they do not contain any implementation. Interfaces are usually defined by a user. The user describes in the interface which services (technical and semantic) it needs in order to carry out a task. The user never actually knows the providers of these services, but communicates with them through the interface. In this way the user is protected from actual implementations and can work in the same way with different classes/objects, as long as they provide the services required. This is known as SRO\PRUSKLVP with interfaces.

SAP AG

0}~| 

OFOBKRWHO

OFOBUHQWDO

OFOBYHKLFOHV
 

BC401

10-5

,QWHUIDFHV 6HUYLFHV
,QWHUIDFH

GLVSOD\BSDUWQHU FKHFNBDYDLODELOLW\ 

OFOBFDUULHU

OFOBDLUSODQH

SAP AG 2002



In the above example, a client wants to use the same services of different server classes (lcl_carrier, lcl_hotel, and lcl_rental) in the same way. There are different implementations of the services in the server classes (polymorphism). The following are examples of shared services of the server classes in the above case: display_partner, check_availability, booking, or cancel_booking.

SAP AG

} 0~|

(s(uu

OFOBKRWHO

OFOBUHQWDO

OFOBYHKLFOHV
 

BC401

10-6

,QWHUIDFH 80/ ([DPSOH


OFOBWUDYHOBDJHQF\
XVHV
,QWHUIDFH!!

&OLHQW

,W LV SRVVLEOH WR ,PSOHPHQWDWLRQ LPSOHPHQW VHYHUDO PHKUHUHU ,QWHUIDFHV LQWHUIDFHV P|JOLFK


,QWHUIDFH!!

OLIBSDUWQHUV
LPSOHPHQWV

OLIBOLFHQVH



OFOBFDUULHU

OFOBKRWHO

OFOBUHQWDO

OFOBDLUSODQH

SAP AG 2002

OFOBYHKLFOHV
  

n n

In UML, interfaces can represented in the same way as classes. However, they always have the stereotype interface above their name and can therefore be distinguished from classes. The use of an interface is represented by a dotted line with a two-sided arrow from the user to the interface, the stereotype uses is optional. The fact that a class implements an interface is represented by a dotted line with a three-sided arrow from the class to the interface. The similarity to the representation of inheritance is intentional, because the interface can be seen as a generalization of the class implemented or the class can be seen as a specialization of the interface. In ABAP Objects, the same components can be defined in interfaces and in classes. This allows you to shift part of the public point of contact of a class into an interface, even though the class is already in use; users will not notice the difference as long as you use alias names (see appendix) for the components that are now in the interface. A class can implement any number of interfaces and an interface can be implemented by any number of classes. By implementing several interfaces, a class can simulate multiple inheritance. In the example, lcl_rental implements the interfaces lif_partners and lif_license.

SAP AG

BC401

10-7

'HILQLQJ DQG ,PSOHPHQWLQJ DQ ,QWHUIDFH

l 'HFODULQJ WKH LQWHUIDFH

,17(5)$&( OLIBSDUWQHUV 0(7+2'6 GLVSOD\BSDUWQHU (1',17(5)$&(


&/$66 OFOBUHQWDO '(),1,7,21 38%/,& 6(&7,21 ,17(5)$&(6 OLIBSDUWQHUV (1'&/$66
&/$66 OFOBUHQWDO ,03/(0(17$7,21 0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU MXVW FDOO H[LVWLQJ PHWKRG WKDW ILWV GLVSOD\BDWWULEXWHV  (1'0(7+2' (1'&/$66
...

l 7KH VHUYHU FODVVHV PDNH WKH LQWHUIDFH SXEOLF l ,QWHUIDFHV DUH LPSOHPHQWHG LQ FODVVHV l ,QWHUIDFHV GR QRW KDYH YLVLELOLW\ VHFWLRQV

,QWHUIDFH UHVROXWLRQ RSHUDWRU

SAP AG 2002

In ABAP Objects, the same components (attributes, methods, constants, types, alias names) can be defined in an interface in largely the same way as in classes. However, interfaces do not have component visibility levels. Interfaces are implemented in classes. The interface name is listed in the definition part of the class. Interfaces can only be implemented publicly and are therefore always in the PUBLIC SECTION. If you do not do this, you risk multiple implementations if a superclass and a subclass both implement the same interface privately. The operations defined in the interface are implemented as methods of a class. A check is carried out to ensure that all the methods defined in the interfaces are actually present in the implementation part of the class (for global interfaces, a missing or superfluous implementation of an interface method results in a warning). The attributes, events, constants, and types defined in the interface are automatically available to the class carrying out the implementation.

Interface components are addressed in the class carrying out the implementation by prefixing the interface name, followed by a tilde (the ,QWHUIDFH 5HVROXWLRQ 2SHUDWRU): <interfacename>~<componentname>.
Changes to an interface usually invalidate all the classes implementing it.

SAP AG

BC401

10-8

,QWHUIDFHV )HDWXUHV
l 6HSDUDWLRQ RI H[WHUQDO SRLQW RI FRQWDFW LQWHUIDFH DQG LPSOHPHQWDWLRQ FODVV

7KH FOLHQW GHILQHV WKH SURWRFRO WKH VHUYHU LPSOHPHQWV LW

%ODFN %R[ SULQFLSOH  &OLHQW RQO\ NQRZV WKH LQWHUIDFH QRW WKH LPSOHPHQWDWLRQ /RRVHU OLQNDJH EHWZHHQ FOLHQW DQG VHUYHU

*HQHULF KDQGOLQJ RI REMHFWV RI GLIIHUHQW FODVVHV

$EVWUDFWLRQ
,QWHUIDFH DV D JHQHUDOL]DWLRQ RI WKH LPSOHPHQWLQJ FODVV
2

6LPXODWLRQ RI PXOWLSOH LQKHULWDQFH


SAP AG 2002

Interfaces are the means of choice for describing external points of contact or protocols, without linking them to a type of implementation. An extra layer is introduced between the client and the server to protect the client explicitly from the server, thereby making the client independent. Interfaces enable you to work uniformly with different classes (providers/servers). In particular, they always ensure polymorphic behavior as they do not have their own implementation, but instead allow the providers to carry it out. The definition of an interface is always an abstraction: The user wants to handle various providers in the same way and must therefore abstract concrete implementations to a description of the services required to fulfill the task. You can also use interfaces to achieve multiple inheritance by defining the functionality to be inherited by a second class as an interface that the inheriting class then has to implement.

SAP AG

BC401

P H  p  D H

u p

yy0

3RO\PRUSKLVP

10-9

,QWHUIDFHV 

,QWHUIDFHV 3ULQFLSOHV ,QWHUIDFHV 3ULQFLSOHV


:RUNLQJ ZLWK LQWHUIDFHV :RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV 3UHYLHZ &RPSRXQG LQWHUIDFHV

SAP AG 2002

SAP AG

BC401

10-10

:RUNLQJ ZLWK ,QWHUIDFH &RPSRQHQWV

&/$66 OFOBUHQWDO ,03/(0(17$7,21  0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU VSHFLDO FRGLQJ WR GLVSOD\ SDUWQHUV RU MXVW FDOOLQJ H[LVWLQJ PHWKRG WKDW ILWV GLVSOD\BDWWULEXWHV  (1'0(7+2' (1'&/$66
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
...

OLIBSDUWQHUV
GLVSOD\BSDUWQHU FKHFNBDYDLODELOLW\ 

LPSOHPHQWV
 QDPH  YHKLFOHBOLVW

OFOBUHQWDO

&5($7( 2%-(&7 UBYHKLFOH V\QWDFWLFDOO\ SRVVLEOH EXW QRW XVHIXO UBYHKLFOH!OLIBSDUWQHUVaGLVSOD\BSDUWQHU 


,QWHUIDFH UHVROXWLRQ RSHUDWRU
SAP AG 2002

 DGGBYHKLFOH  GLVSOD\BDWWULEXWHV 

You can access interface components using an object reference, whose class implements the interface. Syntactically this is done with the interface resolution operator, just as with the method definitions in the implementation part of the class. This allows you to differentiate between components defined in the interface and components of the same name that are defined in the class itself. The reason for this is the shared namespace. To simplify accessing interface methods you can work with DOLDV names. Alias names can only appear in the in the declaration part of a class or in the interface definition. Example for an alias in the interface: ALIASES a1 FOR lif_interface~method1 An alias defined in this way can be directly addressed using r_ref->a1.

n n

SAP AG

BC401

10-11

,QWHUIDFH 5HIHUHQFHV 1DUURZLQJ &DVW


'$7$ UBUHQWDO 7<3( 5() 72 OFOBUHQWDO UBOLI 7<3( 5() 72 OLIBSDUWQHUV

&5($7( 2%-(&7 UBUHQWDO


Public
YY

 OFOBUHQWDO
Private

1DUURZLQJ &DVW UBOLI UBUHQWDO


ut

SAP AG 2002

Interfaces are addressed using interface references. Interface references always refer to instances in the classes carrying out the implementation. Interface references always have both static and dynamic types. The assignment of an object reference to an interface reference is known as a narrowing cast since, as with inheritance, only a part of the object interface is visible once you have assigned the reference. With an interface reference, you can no longer address all components in the class carrying out the implementation, but only the components defined in the interface. These components are now addressed using the interface reference exclusively with their own short name. When an object reference is assigned to an interface reference, you must be able to convert the dynamic type of the interface reference to the static type of the object reference - that is, the class that was used to define the object reference must have implemented the interface of the interfacereference.

n n

SAP AG

uu
BC401

GLVSOD\BSDUWQHU FKHFNBDYDLODELOLW\ 

,QWHUIDFH FRPSRQHQWV

10-12

8VLQJ WKH ,QWHUIDFH

Public

Private

... ...

0(7+2'6 DGGBSDUWQHU ,03257,1* LPBSDUWQHU 7<3( 5() 72 OLIBSDUWQHUV


SAP AG 2002

The travel agent is a user (client) of the created interface. The travel agency keeps references to its business partners in an internal table. Th type of this internal table is REF TO lif_partners, that is the reference to the interface lif_partners. When it is called, the clients public method add_partner is transferred the business partner references. In the method, the references are inserted into the table partner_list. Therefore, the interface parameter of the add_partner method has the type REF TO lif_partners. The aim here is polymorphism: It needs to be possible to generically access the services of the interface later.

SAP AG

BC401

2t 2

t 2 y

DGGBSDUWQHU

22 2
10-13

 OFOBWUDYHOBDJHQF\
0s 

3RO\PRUSKLVP DQG ,QWHUIDFHV


0(7+2' GLVSOD\BDJHQF\BSDUWQHUV '$7$ UBSDUWQHU 7<3( 5() 72 OLIBSDUWQHUV /223 $7 SDUWQHUBOLVW ,172 UBSDUWQHU UBSDUWQHU!GLVSOD\BSDUWQHU  (1'/223 (1'0(7+2'
0(7+2' aGLVSOD\BSDUWQHU GLVSOD\BKRWHOBDWWULEXWHV  (1'0(7+2'
0(7+2' aGLVSOD\BSDUWQHU GLVSOD\BUHQWDOBGDWD  ... (1'0(7+2'
0(7+2' aGLVSOD\BSDUWQHU GLVSOD\BDWWULEXWHV  ... (1'0(7+2'
2 t 2
SAP AG 2002

...

n n

Polymorphism can also be used for interfaces: you can use interface references to call methods that can have a GLIIHUHQW LPSOHPHQWDWLRQ depending on the object behind the reference. The dynamic type, not the static type of the reference variable is used to search for the implementation of a method. In the above example, r_partner->display_partner( ) therefore uses the class of the instance that r_partner actually refers to to search for the implementation of display. The static type for r_partner, which is always REF TO lif_partners, is not used.

SAP AG

2t 2

y0sP

BC401

10-14

,QWHUIDFH 5HIHUHQFHV :LGHQLQJ &DVW


t 2
BC401

0(7+2' ERRNBIOLJKW '$7$ UBFDUULHU 7<3( 5() 72 OFOBFDUULHU UBSDUWQHU 7<3( 5() 72 OFOBSDUWQHU /223 $7 SDUWQHUBOLVW ,172 UBSDUWQHU 75< UBFDUULHU " UBSDUWQHU FDOO PHWKRG RI OFOBFDUULHU WR ERRN IOLJKW UHDFW RQ WKDW FDVW HUURU (1'75< (1'/223 (1'0(7+2'
SAP AG 2002

&$7&+ F[BV\BPRYHBFDVWBHUURU

The widening cast is, as with inheritance, the opposite of the narrowing cast: Here it is used to retrieve a class reference from an interface reference. Obviously it cannot be statically checked, since an interface can be implemented by more than one class. An object reference cannot be assigned to an interface reference if it has itself not implemented the corresponding interface. It cannot be assigned even if a subclass has implemented the interface and the interface reference points to an object in this class. Assignments between interface references whose interfaces are not related to each other cannot be checked statically and must therefore be formulated using the cast operator ?=. For this type of assignment, a check must be carried out at runtime to see whether the class of the instance that the source reference points to also supports the interface that the target reference refers to. If this is the case, the cast is carried out, otherwise the exception that can be handled of the class CX_SY_MOVE_CAST_ERROR is raised.

n n

SAP AG

2t 2

2 2
10-15

222pt2c

HHs H

 OFOBUHQWDO
%  %

,QWHUIDFHV 

,QWHUIDFHV 3ULQFLSOHV ,QWHUIDFHV 3ULQFLSOHV


:RUNLQJ ZLWK LQWHUIDFHV :RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV 3UHYLHZ &RPSRXQG LQWHUIDFHV

SAP AG 2002

SAP AG

BC401

10-16

&RPSRXQG ,QWHUIDFHV
,QWHUIDFH

OFOBKRWHO

OFOBFDUULHU

OFOBUHQWDO

6ROXWLRQ &RPSRXQG LQWHUIDFHV

,QWHUIDFH

OLIBSDUWQHUV
,QWHUIDFH

OLIBURRPBERRNLQJ

OFOBKRWHO
SAP AG 2002

OFOBFDUULHU

ABAP Objects contains a composition model for interfaces. A compound interface contains other interfaces as components (component interfaces) and summarizes the extension of these component interfaces. An elementary interface does not itself contain other interfaces. One interface can be used as a component interface in several compound interfaces. UML only deals with the specialization and generalization of interfaces. This relationship is represented by a dotted line with a three-sided arrow from the specialized to the generalized interface. Compound interfaces in ABAP Objects can always be seen as specializations of their component interfaces and represented as such in UML.

n n

SAP AG

BC401

    

OLIBSDUWQHUV

"

3UREOHP ([WHQGLQJ LQWHUIDFHV

OFOBUHQWDO

10-17

&RPSRXQG ,QWHUIDFHV ([DPSOH


,17(5)$&( OLIBSDUWQHUV 0(7+2'6 GLVSOD\BSDUWQHU (1',17(5)$&(
,17(5)$&( OLIBURRPBERRNLQJ ,17(5)$&(6 OLIBSDUWQHUV 0(7+2'6 ERRNBURRP (1',17(5)$&(
&/$66 OFOBKRWHO '(),1,7,21 38%/,& 6(&7,21 ,17(5)$&(6 OLIBURRPBERRNLQJ (1'&/$66 &/$66 OFOBKRWHO ,03/(0(17$7,21 0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU (1'0(7+2' 0(7+2' OLIBURRPBERRNLQJaERRNBURRP (1'0(7+2' (1'&/$66

'$7$ LBSDUWQHU LBURRPBERRN


...

7<3( 5() 72 OLIBSDUWQHUV 7<3( 5() 72 OLIBURRPBERRNLQJ


1DUURZLQJ &DVW

LBSDUWQHU

LBURRPBERRN

LBURRPBERRN!OLIBSDUWQHUVaGLVSOD\BSDUWQHU  LBSDUWQHU!GLVSOD\BSDUWQHU DOVR SRVVLEOH LBURRPBERRN "


SAP AG 2002

LBSDUWQHU

:LGHQLQJ &DVW

In a compound interface, the components of the component interface keep their original names, that is <component-interfacename>~<componentname>; no more prefixes are added. In other words, all components in a compound interface are on the same level, and components inherited from component interfaces are marked with the usual interface prefix. This equality principle for compound interfaces also affects how they are implemented. The procedure is as follows: First, implement the elementary interfaces, then the additional methods from the compound interfaces. For multiple compound interfaces, the process is simply repeated. In the class carrying out the implementation, all components of all interfaces implemented are again on the same level. This means that interface components only ever exist once and are known by their original names <interfacename>~<componentname>. This is true both for compound interfaces and for the classes that implement them.

SAP AG

BC401

10-18

,QWHUIDFHV 8QLW 6XPPDU\

l 'HILQH DQG LPSOHPHQW LQWHUIDFHV

<RX DUH QRZ DEOH WR

l 'HYHORS JHQHULF SURJUDPV XVLQJ SRO\PRUSKLVP ZLWK LQWHUIDFHV

SAP AG 2002

SAP AG

BC401

10-19

,QWHUIDFHV ([HUFLVHV
8QLW ,QWHUIDFHV 7RSLF 'HILQLQJ ,QWHUIDFHV
At the conclusion of these exercises, you will be able to: Define and implement interfaces

0RGHO VROXWLRQ 6$3%&B,176B0$,1B$ 6$3%&B9(+'B) include program 6$3%&B,176B$ include program 1 The classes OFOBUHQWDO OFOBYHKLFOH, and their subclasses are to be added to the existing UML diagram. The classes OFOBUHQWDO OFOBFDUULHU, and later OFOBKRWHO will all store separate services and provide a possible client (to be defined later) using an interface OLIBSDUWQHUV. 1-1 Add the interface OLIBSDUWQHUV to your UML diagram. GLVSOD\BSDUWQHU is to be the only interface method. What are the roles of the classes OFOBUHQWDO OFOBFDUULHU, and OFOBKRWHO? Depict this in the UML diagram by adding text and arrows. Fill in the UML diagram on the next page. Copy the include program 6$3%&B9(+'B) to your own include program =%&B B9(+,&/(. As well as the classes OFOBUHQWDO OFOBYHKLFOH OFOBWUXFN OFOBEXV, the program also contains the interface OLIBSDUWQHUV. Incorporate this include program into your main program. Add the types W\BIXHO and W\BFDUJR (both TYPE P DECIMALS 2) to the main program. Pay attention to the position of the interface definition; you may have to change the order of the include programs or move the interface into the other include program. Familiarize yourself with the interface definition.

1-2

1-3

SAP AG

BC401

10-20

1-4 1-5

Now implement the interface of your class OFOBFDUULHU. In the main program, create some instances for OFOBUHQWDO, OFOBYHKLFOH, and so on by copying the relevant code from the template 6$3%&B9(+7B0$,1B$. Call the method GLVSOD\BDWWULEXWHV of the class OFOBUHQWDO. Start your program. You should see an airline with your planes and a car rental company with various vehicles. (You will use the interface in the next exercise.)

UML-Template for Exercise 1-1

SAP AG

BC401

10-21

([HUFLVHV
8QLW ,QWHUIDFHV 7RSLF 8VLQJ ,QWHUIDFHV
At the conclusion of these exercises, you will be able to: Define and implement interfaces Use polymorphism with interfaces 0RGHO VROXWLRQ 6$3%&B,176B0$,1B% 6$3%&B9(+'B* include program 6$3%&B,176B$ include program 2 Now the class OFOBWUDYHOBDJHQF\ (travel agency) is to be added to the existing UML diagram. It will use the interface to access the classes OFOBUHQWDO OFOBFDUULHU OFOBKRWHO (not yet implemented). 2-1 Add the class OFOBWUDYHOBDJHQF\ to your UML diagram. What is the role of this class? Depict this in the UML diagram by adding text and arrows. Fill in the UML diagram on the next page. Copy the definition of the class OFOBWUDYHOBDJHQF\ from the template 6$3%&B9(+7B% to your own include program. Then complete this new class. 2-2-1 The travel agency uses the public method DGGBSDUWQHU to add business partners (that is car rental companies and airlines) to the internal table SDUWQHUBOLVW. 2-2-2 What are the types of the internal table and the interface parameter of DGGBSDUWQHU? 2-2-3 The travel agency will use the method GLVSOD\BDJHQF\BSDUWQHUV to display the data of its business partners.

2-2

SAP AG

BC401

10-22

2-3 2-4

Go back to your main program. Create an instance of the class OFOBWUDYHOBDJHQF\. 2-4-1 Use the DGGBSDUWQHU method to add the travel agencys business partners to the internal table. 2-4-2 Use the GLVSOD\BDJHQF\BSDUWQHUV method to display the travel agencys business partners. 2-4-3 At which point does polymorphism take place? Follow this in the Debugger.

UML-Template for Exercise 2-1

SAP AG

BC401

10-23

,QWHUIDFHV 6ROXWLRQV
7RSLF 8QLW ,QWHUIDFHV 'HILQLQJ ,QWHUIDFHV

*&---------------------------------------------------------------------* *& Report *& *& SAPBC401_INTS_MAIN_A * * include interface lif_partners and include file with lcl_vehicle.. *

*&---------------------------------------------------------------------* *&---------------------------------------------------------------------* REPORT sapbc401_ints_main_a.

types ty_fuel type p decimals 2. types ty_cargo type p decimals 2. INCLUDE <icon>. INCLUDE sapbc401_vehd_f. include sapbc401_ints_a. DATA: r_plane TYPE REF TO lcl_airplane, r_cargo TYPE REF TO lcl_cargo_plane, r_passenger TYPE REF TO lcl_passenger_plane, UBFDUULHU W\SH UHI WR OFOBFDUULHU UBUHQWDO W\SH UHI WR OFOBUHQWDO UBWUXFN W\SH UHI WR OFOBWUXFN UBEXV W\SH UHI WR OFOBEXV UBDJHQF\ W\SH UHI WR OFOBWUDYHOBDJHQF\

START-OF-SELECTION. *############################## ***** Create Carrier ******************************************** FUHDWH REMHFW UBFDUULHU H[SRUWLQJ LPBQDPH
6PLOH )O\7UDYHO


***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING

SAP AG

BC401

10-24

im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************ CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ***** insert planes into itab if client *************************** UBFDUULHU!DGGBDLUSODQH UBSDVVHQJHU  UBFDUULHU!DGGBDLUSODQH UBFDUJR 

***** show all airplanes inside carrier *************************** UBFDUULHU!GLVSOD\BDWWULEXWHV 

******* create RENTAL ***************************************** &5($7( 2%-(&7 UBUHQWDO (;3257,1* LPBQDPH


+$33< &$5 5(17$/


******* create truck ***************************************** &5($7( 2%-(&7 UBWUXFN (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBWUXFN  LPBFDUJR
0$1


******* create truck ***************************************** &5($7( 2%-(&7 UBEXV (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBEXV  LPBSDVVHQJHUV
0HUFHGHV


******* create truck ***************************************** &5($7( 2%-(&7 UBWUXFN (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBWUXFN  LPBFDUJR
92/92


SAP AG

BC401

10-25

*&---------------------------------------------------------------------* *& Include SAPBC401_INTS_a * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * ... *---------------------------------------------------------------------* * CLASS lcl_carrier DEFINITION *---------------------------------------------------------------------* *---------------------------------------------------------------------* &/$66 OFOBFDUULHU '(),1,7,21 38%/,& 6(&7,21 CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------*

,17(5)$&(6 OLIBSDUWQHUV



0(7+2'6 FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ DGGBDLUSODQH ,03257,1* GLVSOD\BDLUSODQHV 35,9$7( 6(&7,21 '$7$ QDPH (1'&/$66

JHWBQDPH 5(7851,1* YDOXH H[BQDPH 7<3( VWULQJ LPBSODQH 7<3( 5() 72 OFOBDLUSODQH

GLVSOD\BDWWULEXWHV

 DLUSODQHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBDLUSODQH OFOBFDUULHU '(),1,7,21 7<3( VWULQJ

*---------------------------------------------------------------------* * CLASS lcl_carrier IMPLEMENTATION *---------------------------------------------------------------------* &/$66 OFOBFDUULHU ,03/(0(17$7,21 GLVSOD\BDLUSODQHV  0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU (1'0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU

0(7+2' DGGBDLUSODQH (1'0(7+2'

$33(1' LPBSODQH 72 DLUSODQHBOLVW

DGGBDLUSODQH

0(7+2' GLVSOD\BDWWULEXWHV

:5,7( LFRQBIOLJKW $6 ,&21 QDPH  8/,1( 8/,1(

SAP AG

BC401

10-26

(1'0(7+2'

GLVSOD\BDLUSODQHV 

GLVSOD\BDWWULEXWHV

0(7+2' GLVSOD\BDLUSODQHV

'$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH /223 $7 DLUSODQHBOLVW ,172 UBSODQH (1'/223 UBSODQH!GLVSOD\BDWWULEXWHV 

(1'0(7+2' 0(7+2' FRQVWUXFWRU (1'0(7+2' QDPH LPBQDPH

GLVSOD\BDLUSODQHV

FRQVWUXFWRU

0(7+2' JHWBQDPH (1'0(7+2' (1'&/$66 H[BQDPH

QDPH

JHWBQDPH OFOBFDUULHU ,03/(0(17$7,21

SAP AG

BC401

10-27

6ROXWLRQV
7RSLF
*& Report *& *& *&

8QLW ,QWHUIDFHV

8VLQJ ,QWHUIDFHV
* *

*&---------------------------------------------------------------------* SAPBC401_INTS_MAIN_B

*&---------------------------------------------------------------------* include interface lif_partners and include file with lcl_vehicle.. * the client (travel_agency) uses the interface... *

*&---------------------------------------------------------------------* REPORT sapbc401_ints_main_b.

types ty_fuel type p decimals 2. types ty_cargo type p decimals 2. INCLUDE <icon>. INCLUDE sapbc401_vehd_g. include sapbc401_ints_a. DATA: r_plane TYPE REF TO lcl_airplane, r_cargo TYPE REF TO lcl_cargo_plane, r_passenger TYPE REF TO lcl_passenger_plane, r_carrier type ref to lcl_carrier, UBDJHQF\ W\SH UHI WR OFOBWUDYHOBDJHQF\ UBUHQWDO W\SH UHI WR OFOBUHQWDO UBWUXFN W\SH UHI WR OFOBWUXFN UBEXV W\SH UHI WR OFOBEXV START-OF-SELECTION. *############################## ***** Create TRAVEL_AGENCY ************************************** &5($7( 2%-(&7 UBDJHQF\ (;3257,1* LPBQDPH
)O\ 6PLOH 7UDYHO


SAP AG

BC401

10-28

***** Create CARRIER ******************************************** create object r_carrier exporting im_name = Smile&Fly-Travel. ***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************ CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ***** insert planes into itab if client *************************** r_carrier->add_airplane( r_passenger ). r_carrier->add_airplane( r_cargo ). ***** insert business-parnter of agency into partner_list*********** UBDJHQF\!DGGBSDUWQHU UBFDUULHU  ******* create RENTAL ***************************************** &5($7( 2%-(&7 UBUHQWDO (;3257,1* LPBQDPH
+$33< &$5 5(17$/


******* create truck ***************************************** &5($7( 2%-(&7 UBWUXFN (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBWUXFN  LPBFDUJR
0$1


******* create truck ***************************************** &5($7( 2%-(&7 UBEXV (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBEXV  LPBSDVVHQJHUV
0HUFHGHV


******* create truck ***************************************** &5($7( 2%-(&7 UBWUXFN (;3257,1* LPBPDNH UBUHQWDO!DGGBYHKLFOH UBWUXFN  LPBFDUJR
92/92


***** insert business-partner of agency into partner_list*********** UBDJHQF\!DGGBSDUWQHU UBUHQWDO  ******* show attributes of all partners of travel_agency ****** UBDJHQF\!GLVSOD\BDJHQF\BSDUWQHUV 

SAP AG

BC401

10-29

*&---------------------------------------------------------------------* *& Include SAPBC401_VEHD_g * *&---------------------------------------------------------------------* *---------------------------------------------------------------------* * define client lcl_travel_agency * it will use the interface lif_partners *---------------------------------------------------------------------* ,17(5)$&( OLIBSDUWQHUV (1',17(5)$&( 0(7+2'6 GLVSOD\BSDUWQHU

OLIBSDUWQHUV

*---------------------------------------------------------------------* * CLASS lcl_vehicle DEFINITION *---------------------------------------------------------------------* CLASS lcl_vehicle DEFINITION. PUBLIC SECTION. "------------------METHODS: get_average_fuel IMPORTING im_distance TYPE s_distance im_fuel TYPE ty_fuel RETURNING value(re_avgfuel) TYPE ty_fuel. METHODS METHODS METHODS METHODS constructor IMPORTING im_make TYPE string. display_attributes. set_make IMPORTING im_make TYPE string. get_make EXPORTING ex_make TYPE string.

CLASS-METHODS get_count EXPORTING re_count TYPE i. PRIVATE SECTION. "------------------DATA: make METHODS CLASS-DATA: ENDCLASS. TYPE string. init_make. n_o_vehicles TYPE i. "lcl_vehicle DEFINITION

SAP AG

BC401

10-30

*---------------------------------------------------------------------* * CLASS lcl_vehicle IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_vehicle IMPLEMENTATION. METHOD get_average_fuel. re_avgfuel = im_distance / im_fuel. ENDMETHOD. METHOD constructor. make = im_make. ADD 1 TO n_o_vehicles. ENDMETHOD. METHOD set_make. IF im_make IS INITIAL. init_make( ). ELSE. make = im_make. ENDIF. ENDMETHOD. METHOD init_make. make = default make. ENDMETHOD. METHOD get_make. ex_make = make. ENDMETHOD. METHOD display_attributes. WRITE: make. ENDMETHOD. METHOD get_count. re_count = n_o_vehicles. ENDMETHOD. ENDCLASS. "get_count "lcl_vehicle IMPLEMENTATION "display_attributes "get_make "init_make "set_make " me->init_make( ). also possible "constructor "get_average_fuel

SAP AG

BC401

10-31

*---------------------------------------------------------------------* * CLASS lcl_truck DEFINITION *---------------------------------------------------------------------* CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle. PUBLIC SECTION. "------------------METHODS: constructor IMPORTING im_make TYPE string im_cargo TYPE ty_cargo. METHODS METHODS PRIVATE SECTION. "------------------DATA: max_cargo TYPE ty_cargo. ENDCLASS. "lcl_vehicle DEFINITION display_attributes REDEFINITION. get_cargo RETURNING value(re_cargo) TYPE ty_cargo.

*---------------------------------------------------------------------* * CLASS lcl_truck IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_truck IMPLEMENTATION. METHOD constructor. super->constructor( im_make ). max_cargo = im_cargo. ENDMETHOD. METHOD display_attributes. WRITE: / icon_ws_truck AS ICON. super->display_attributes( ). WRITE: 20 Cargo = , max_cargo. ULINE. ENDMETHOD. METHOD get_cargo. re_cargo = max_cargo. ENDMETHOD. ENDCLASS. "get_cargo "lcl_vehicle DEFINITION "display_attributes "constructor

SAP AG

BC401

10-32

*---------------------------------------------------------------------* * CLASS lcl_bus DEFINITION *---------------------------------------------------------------------* CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle. PUBLIC SECTION. "------------------METHODS: constructor IMPORTING im_make TYPE string im_passengers TYPE i. METHODS PRIVATE SECTION. "------------------DATA: max_passengers TYPE i. ENDCLASS. "lcl_vehicle DEFINITION display_attributes REDEFINITION.

*---------------------------------------------------------------------* * CLASS lcl_bus IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_bus IMPLEMENTATION. METHOD constructor. super->constructor( im_make ). max_passengers = im_passengers. ENDMETHOD. METHOD display_attributes. WRITE: / icon_transportation_mode AS ICON. super->display_attributes( ). WRITE: ULINE. ENDMETHOD. ENDCLASS. "display_attributes "lcl_vehicle DEFINITION 20 Passengers = , max_passengers. "constructor

SAP AG

BC401

10-33

*---------------------------------------------------------------------* * CLASS lcl_rental DEFINITION *---------------------------------------------------------------------* &/$66 OFOBUHQWDO '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 0(7+2'6 0(7+2'6



FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ DGGBYHKLFOH ,03257,1* LPBYHKLFOH GLVSOD\BDWWULEXWHV 7<3( 5() 72 OFOBYHKLFOH

35,9$7( 6(&7,21

,17(5)$&(6 OLIBSDUWQHUV 

'$7$ QDPH 7<3( VWULQJ (1'&/$66

YHKLFOHBOLVW 7<3( 7$%/( 2) 5() 72 OFOBYHKLFOH OFOBUHQWDO '(),1,7,21

*---------------------------------------------------------------------* * CLASS lcl_rental IMPLEMENTATION *---------------------------------------------------------------------* &/$66 OFOBUHQWDO ,03/(0(17$7,21 GLVSOD\BDWWULEXWHV  0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU (1'0(7+2' 0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU

(1'0(7+2' 0(7+2'

QDPH

FRQVWUXFWRU LPBQDPH FRQVWUXFWRU

(1'0(7+2' 0(7+2'

$33(1' LPBYHKLFOH 72 YHKLFOHBOLVW

DGGBYHKLFOH

DGGBYHKLFOH

'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH :5,7(  :5,7(

GLVSOD\BDWWULEXWHV

/223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH (1'/223 UBYHKLFOH!GLVSOD\BDWWULEXWHV 

+HUH FRPHV WKH YHKLFOH OLVW


 8/,1( 8/,1(

LFRQBWUDQVSRUWBSURSRVDO $6 ,&21 QDPH

(1'&/$66

(1'0(7+2'

OFOBUHQWDO ,03/(0(17$7,21

GLVSOD\BDWWULEXWHV

SAP AG

BC401

10-34

*---------------------------------------------------------------------* * CLASS lcl_travel_agency DEFINITION *---------------------------------------------------------------------* &/$66 OFOBWUDYHOBDJHQF\ '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 0(7+2'6 0(7+2'6



FRQVWUXFWRU ,03257,1* LPBQDPH 7<3( VWULQJ DGGBSDUWQHU ,03257,1* LPBSDUWQHU GLVSOD\BDJHQF\BSDUWQHUV 7<3( 5() 72 OLIBSDUWQHUV

35,9$7( 6(&7,21

'$7$ QDPH 7<3( VWULQJ (1'&/$66



SDUWQHUBOLVW 7<3( 7$%/( 2) 5() 72 OLIBSDUWQHUV

OFOBWUDYHOBDJHQF\ '(),1,7,21

*---------------------------------------------------------------------* * CLASS lcl_travel_agency IMPLEMENTATION *---------------------------------------------------------------------* &/$66 OFOBWUDYHOBDJHQF\ ,03/(0(17$7,21 0(7+2' GLVSOD\BDJHQF\BSDUWQHUV '$7$ UBSDUWQHU 7<3( 5() 72 OLIBSDUWQHUV :5,7( LFRQBGHSHQGHQWV $6 ,&21 QDPH :5,7( /223 $7 SDUWQHUBOLVW ,172 UBSDUWQHU (1'/223 UBSDUWQHU!GLVSOD\BSDUWQHU 
+HUH DUH WKH SDUWQHUV RI WKH WUDYHO DJHQF\
8/,1(8/,1(

(1'0(7+2' 0(7+2'

GLVSOD\BDJHQF\BSDUWQHUV

(1'0(7+2' 0(7+2'

QDPH

FRQVWUXFWRU LPBQDPH FRQVWUXFWRU

(1'0(7+2' (1'&/$66

$33(1' LPBSDUWQHU 72 SDUWQHUBOLVW

DGGBSDUWQHU

OFOBWUDYHOBDJHQF\ ,03/(0(17$7,21

DGGBSDUWQHU

SAP AG

BC401

10-35

(YHQWV &RQWHQWV

l 'HILQLQJ DQG WULJJHULQJ HYHQWV

l 5HJLVWHULQJ DQG KDQGOLQJ HYHQWV

SAP AG 2002

SAP AG

BC401

11-1

(YHQWV 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HILQH DQG WULJJHU HYHQWV l +DQGOH HYHQWV l 5HJLVWHU DQG GHUHJLVWHU HYHQWV

l 5HFHLYH D UHIHUHQFH IURP WKH VHQGHU

l ([SODLQ WKH FRQFHSWXDO GLIIHUHQFHV EHWZHHQ PHWKRGV DQG HYHQWV

SAP AG 2002

SAP AG

BC401

11-2

y d dF%Dx H Q R 7 S7 A F9@ E CDH 7 A B 89@ 8P7 @ 7 IG7 C A B H 6 h U fb e g fW d ab W `X W %T e c Y VU $  &% "#!   1)  ! ' 3  &  20'("  "    5 5 !  " ! 4        e 20eQQI FD h gf d i d k m ml k jd h F lh o n k h l p d l d h n xw u r Ivtsq

fV ea b i w s VW U a b uvh te a e i qp rW e U b dg ae e i

SAP AG
SAP AG 2002

(YHQWV ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

11-3

(YHQWV 2YHUYLHZ

PDLQ SURJUDP &5($7( 2%-(&7 UBFDU (;3257,1*


...

&DU

SAP AG 2002

n n

By triggering an event, an object or class announces a change of state, or that a certain state has been achieved. In the example, the class FDU triggers the event FUHDWHG. Other classes subscribe to this event (triggered when a car is instantiated) and process it. The car rental company wants to be informed of completion; the car is registered at the vehicle registration office. Note: The events discussed here are QRW the events of the ABAP runtime system (such as INITIALIZATION, START-OF-SELECTION, and so on) and QRW the events of background processing or workflow.

SAP AG

BC401

z d~}z | z z

&DU UHQWDO FRPSDQ\

9HKLFOH UHJLVWUDWLRQ RIILFH

 ``Q`Q`0 `

|}{y z ~ z



11-4

)HDWXUHV

l /RRVHU OLQNDJH WKDQ IRU D PHWKRG FDOO


7ULJJHULQJ REMHFW PDNHV VWDWXV FKDQJH NQRZQ

n n n

l 'LIIHUHQW FRPPXQLFDWLRQ PRGHO

+DQGOHU REMHFWV FDQ VKRZ LQWHUHVW DQG UHDFW

l $UHDV RI XVH

SAP AG 2002

Events link objects or classes more loosely than direct method calls do. Method calls establish precisely when and in which statement sequence the method is called. However, with events, the reaction of the object to the event is triggered by the event itself. Events are most often used in GUI implementations. Other external object models, such as COM, ActiveX controls, and so on, also provide events.

SAP AG

7KH WULJJHU GRHV QRW NQRZ WKH XVHU XVHU


V UHDFWLRQ LV RI QR LQWHUHVW WR WULJJHU

*8, LPSOHPHQWDWLRQV

&RQIRUPLW\ ZLWK RWKHU REMHFW PRGHOV &20 $FWLYH; FRQWUROV 2SHQ'RF

BC401

11-5

7ULJJHULQJ DQG +DQGOLQJ (YHQWV 2YHUYLHZ

5XOHV

SAP AG 2002

l 7ULJJHULQJ HYHQWV

&ODVV GHILQHV HYHQW (9(176 &/$66(9(176

2EMHFW RU FODVV WULJJHUV HYHQW 5$,6( (9(17

l +DQGOLQJ HYHQWV

(YHQW KDQGOHU REMHFW RU KDQGOHU FODVV UHJLVWHUV LWVHOI WR VSHFLILF HYHQWV DW UXQWLPH 6(7 +$1'/(5

(YHQW KDQGOHU FODVV GHILQHV DQG LPSOHPHQWV HYHQW KDQGOHU PHWKRG >&/$66@0(7+2'6 )25 (9(17  2) 

At the moment of implementation, a class defines its: Instance events (using the EVENTS statement) Static events (using the CLASS-EVENTS statement)

Classes or their instances that receive a message when an event is triggered at runtime and want to react to this event define event handler methods. Statement: [CLASS-]METHODS <handler_method> FOR EVENT <event> OF <classname>. These classes or their instances are registered to one or more events at runtime. Statement: SET HANDLER <handler_method> FOR <reference>. (for instance events) SET HANDLER <handler_method>. (for static events) A class or instance can trigger an event at runtime using the RAISE EVENT statement.

SAP AG

BC401

11-6

'HILQLQJ DQG 7ULJJHULQJ (YHQWV 6\QWD[


&/$66 FODVVQDPH! '(),1,7,21 (9(176 HYHQW! (;3257,1* 9$/8( H[BSDU! 7<3( W\SH!
&/$66 FODVVQDPH! ,03/(0(17$7,21 0(7+2' P! 5$,6( (9(17 HYHQW! (;3257,1* H[BSDU!

DFWBSDU!

&/$66 OFOBYHKLFOH '(),1,7,21 38%/,& 6(&7,21 0(7+2'6 FRQVWUXFWRU ,03257,1* (9(176 YHKLFOHBFUHDWHG (1'&/$66
&/$66 OFOBYHKLFOH ,03/(0(17$7,21 0(7+2' FRQVWUXFWRU 5$,6( (9(17 YHKLFOHBFUHDWHG (1'0(7+2' (1'&/$66

...

...


FDU
s(vt0

SAP AG 2002

n n n n

Both instance and static events can be triggered in instance methods. Only static events can be triggered in static methods. Events can only have EXPORTING parameters which must be passed by value. Triggering an event using the statement RAISE EVENT has the following effect: The program flow is interrupted at that point The event handler methods registered to this event are called and processed Once all event handler methods have been executed, the program flow continues

If an event handler method in turn triggers an event, then the program flow is again interrupted and all event handler methods are executed (nesting).

SAP AG

...

BC401

11-7

+DQGOLQJ DQG 5HJLVWHULQJ (YHQWV

SAP AG 2002

n n n

Events are registered using the SET HANDLER statement. Registration is only active at program runtime. Events cannot be persistent. You want to register an object to an event belonging to another object. The SET HANDLER statement enters the registration in that objects list. All handlers for one event are entered in this list. When the event is triggered, the list shows which event handler methods need to be called.

SAP AG

BC401

z d~ |
`I``Q`Q`

d`(Qv

z{y ~ | z

UHQWDO

Id

t
t0

UHJLVWUDWLRQ RIILFH

FDUULHU

11-8

(YHQW +DQGOHU 0HWKRGV


&/$66 FODVVBKDQGOH! '(),1,7,21 0(7+2'6 RQBHYHQW! )25 (9(17 HYHQW! 2) FODVVQDPH! _ LQWHUIDFH! ,03257,1* H[BSDU!  H[BSDU1! >VHQGHU@
&/$66 OFOBUHQWDO '(),1,7,21  35,9$7( 6(&7,21 0(7+2'6 DGGBYHKLFOH )25 (9(17 YHKLFOHBFUHDWHG 2) OFOBYHKLFOH ,03257,1* VHQGHU (1'&/$66

n n

FDU

DGGBYHKLFOH
Q`vvv`

UHQWDO

SAP AG 2002

Event handler methods are triggered by events (RAISE EVENT), although they can also be called like normal methods (CALL METHOD). The interface of the event handler method consists solely of IMPORTING parameters. You can only use parameters from the definition of the corresponding events (event interface). An event interface, which only has EXPORTING parameters, is defined using the EVENTS statement in the declaration of the event. The parameters are typed in the event definition and the typing is passed to the event handler method, that is, the interface parameters of the event handler method cannot be typed in the definition of the event handler method. In addition to the explicitly defined event interface parameters, the implicit parameter sender can also be listed as an IMPORTING parameter for instance events. This passes on a reference to the object that triggered the event.

SAP AG

BC401

11-9

5HJLVWHULQJ IRU DQ (YHQW 6\QWD[


6(7 +$1'/(5 UHIBKDQGOH!!RQBHYHQW! )25 UHIBVHQGHU! _ )25 $// ,167$1&(6 >$&7,9$7,21 YDU!@
&/$66 OFOBUHQWDO '(),1,7,21 35,9$7( 6(&7,21 0(7+2'6 DGGBYHKLFOH )25 (9(17 YHKLFOHBFUHDWHG 2) (1'&/$66
... ...

&/$66 OFOBUHQWDO ,03/(0(17$7,21 0(7+2' FRQVWUXFWRU 6(7 +$1'/(5 DGGBYHKLFOH )25 $// ,167$1&(6 (1'0(7+2' (1'&/$66

n n n
...

FDU

VHW KDQGOHU
s0It

UHQWDO

SAP AG 2002

When an event is triggered, only those event handler methods are executed that have, by this point, registered themselves using SET HANDLER. You can register an event using ACTIVATION X, and deregister it using ACTIVATION space. If you do not specify ACTIVATION, then the event registers (default behavior). You can register several methods in one SET HANDLER statement: SET HANDLER <ref_handle1>-><handler_method1> ... <ref_handleN>-><handler_methodN> FOR <ref_sender> | FOR ALL INSTANCES.

SAP AG

BC401

11-10

5HJLVWUDWLRQ'HUHJLVWUDWLRQ +DQGOHU 7DEOHV


` `Q

FDU

YHKLFOHBFUHDWHG HYHQW 5HJLVWHUHG +DQGOHU REMHFW PHWKRG UHJBYHKLFOH

FDU

YHKLFOHBFUHDWHG HYHQW 5HJLVWHUHG +DQGOHU REMHFW PHWKRG DGGBYHKLFOH UHJBYHKLFOH

SAP AG 2002

n n

Every object that has defined events has an internal table, the handler table. All objects that have registered for events are entered in this table together with their event handler methods. Objects that have registered themselves for an event that is still "active" also remain "active". The methods of these objects are called when the event is triggered, even if they can no longer be reached using main memory references.

SAP AG

` Q

v
(v

UHJLVWUDWLRQ RIILFH

UHQWDO

UHJLVWUDWLRQ RIILFH

BC401

11-11

(YHQW +DQGOLQJ )HDWXUHV

l l l

(YHQW KDQGOLQJ LV VHTXHQWLDO

6HTXHQFH LQ ZKLFK HYHQW KDQGOHU PHWKRGV DUH FDOOHG LV QRW GHILQHG

:LWK UHJDUG WR WKH *DUEDJH &ROOHFWRU UHJLVWUDWLRQ KDV WKH VDPH HIIHFW DV D UHIHUHQFH WR WKH UHJLVWHUHG REMHFW 7KH YLVLELOLW\ RI DQ HYHQW GHILQHV DXWKRUL]DWLRQ IRU HYHQW KDQGOLQJ
5HJLVWHUHG REMHFWV DUH QHYHU GHOHWHG

l l

7KH YLVLELOLW\ LQ DQ HYHQW KDQGOHU PHWKRG GHILQHV DXWKRUL]DWLRQ IRU XVLQJ 6(7 +$1'/(5 VWDWHPHQWV

(YHQW KDQGOHU PHWKRGV KRZHYHU FDQ RQO\ KDYH WKH VDPH YLVLELOLW\ RU PRUH UHVWULFWHG YLVLELOLW\ WKDQ WKH HYHQWV WKH\ UHIHU WR

SAP AG 2002

If several objects have registered for an event, then the sequence in which the event handler methods are called is not defined, that is, there is no guaranteed sequence in which the event handler methods are called. If a new event handler is registered in an event handler method for an event that has just been triggered, then this event handler is added to the end of the sequence and is then also executed when its turn comes. If an existing event handler is deregistered in an event handler method, then this handler is deleted from the event handler method sequence. Events are also subject to the visibility concept and can therefore be either public, protected, or private. Visibility specifies who can handle an event: PUBLIC: PROTECTED: PRIVATE: All users Only users within that class or its subclasses Only users within that class

Event handler methods also have visibility attributes. Event handler methods, however, can only have the same visibility or more restricted visibility than the events they refer to. The visibility of event handler methods establishes authorization for SET HANDLER statements; SET HANDLER statements can be used: Everywhere, in the class and its subclasses, or only within the class.

SAP AG

BC401

11-12

(YHQWV 8QLW 6XPPDU\

l 'HILQH DQG WULJJHU HYHQWV l +DQGOH HYHQWV l 5HJLVWHU DQG GHUHJLVWHU HYHQWV

<RX DUH QRZ DEOH WR

l ([SODLQ WKH FRQFHSWXDO GLIIHUHQFHV EHWZHHQ PHWKRGV DQG HYHQWV

SAP AG 2002

SAP AG

BC401

11-13

(YHQWV ([HUFLVHV
8QLW (YHQWV 7RSLF 7ULJJHULQJ DQG +DQGOLQJ (YHQWV
At the conclusion of these exercises, you will be able to: Define and trigger events Handle events Register event handler methods
As soon as a new airplane is created, this event must be made known to the airline. .

0RGHO VROXWLRQ 6$3%&B(9(6B0$,1B$ 6$3%&B9(+'B+ include program 6$3%&B(9(6B$ include program The DGGBDLUSODQH method of the class OFOBFDUULHU will no longer be explicitly called in the PDLQ SURJUDP, but be triggered automatically from the class OFOBDLUSODQH. Triggering an event when a plane is created (CREATE OBJECT) will result in the automatic execution of the airline method DGGBDLUSODQH. 1-1 In the UML diagram decide what steps are needed where for the triggering and handling of the event DLUSODQHBFUHDWHG. See next page for UML diagram. Trigger the event DLUSODQHBFUHDWHG in a suitable method within the class OFOBDLUSODQH. Handle the event within the class OFOBFDUULHU using the handler method DGGBDLUSODQH. This method requires a new interface, its implementation must also be changed slightly. In the main program, comment out the calls r_carrier->add_airplane. In the debugger, check whether the event is triggered and handled by the event handler method when the planes are created. If this does not happen, check whether one of the four important steps for implementing events was perhaps left out.
BC401 11-14

1-2 1-3

1-4 1-5

SAP AG

 2SWLRQDO
Implement the event YHKLFOHBFUHDWHG for the car rental company and its corresponding vehicles.  2SWLRQDO DGYDQFHG 
In the UML, there is the possibility of using events when creating the business partners of the travel agency. If airlines, car rental companies, or hotels are created, these business partners should automatically be made known to the travel agency. 2-1 You could solve this using the events FDUULHUBFUHDWHG, UHQWDOBFUHDWHG, and KRWHOBFUHDWHG. Would this be problematic? What would be the best solution? Implement your solution for OFOBFDUULHU and OFOBUHQWDO.

2-2

SAP AG

BC401

11-15

6ROXWLRQV
7RSLF 8QLW (YHQWV 7ULJJHULQJ DQG +DQGOLQJ (YHQWV

*&---------------------------------------------------------------------* *& Report *& *& Implement Events in lcl_vehicle and lcl_airplane SAPBC401_EVES_MAIN_A * * *

*&---------------------------------------------------------------------* *&---------------------------------------------------------------------* REPORT sapbc401_eves_main_a.

TYPES: ty_fuel TYPE p DECIMALS 2, ty_cargo TYPE p DECIMALS 2. INCLUDE <icon>. include sapbc401_vehd_h. INCLUDE sapbc401_eves_a.

DATA: r_vehicle TYPE REF TO lcl_vehicle, r_truck TYPE REF TO lcl_truck, r_bus TYPE REF TO lcl_bus, r_passenger type ref to lcl_passenger_plane, r_cargo type ref to lcl_cargo_plane, r_carrier type ref to lcl_carrier, r_rental type ref to lcl_rental, r_agency type ref to lcl_travel_agency. START-OF-SELECTION. *######################## ***** Create TRAVEL_AGENCY ************************************** CREATE OBJECT r_agency EXPORTING im_name = Fly&Smile Travel.

***** Create CARRIER ********************************************

SAP AG

BC401

11-16

create object r_carrier exporting im_name = Smile&Fly-Travel. ***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************ CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ***** insert planes into itab if client *************************** UBFDUULHU!DGGBDLUSODQH UBSDVVHQJHU  UBFDUULHU!DGGBDLUSODQH UBFDUJR 

***** insert business-parnter of agency into partner_list*********** r_agency->add_partner( r_carrier ). ******* create RENTAL ***************************************** CREATE OBJECT r_rental EXPORTING im_name = HAPPY CAR RENTAL. ******* create truck ***************************************** CREATE OBJECT r_truck EXPORTING im_make = MAN im_cargo = 45. UBUHQWDO!DGGBYHKLFOH UBWUXFN  CREATE OBJECT r_bus EXPORTING im_make = Mercedes im_passengers = 80. UBUHQWDO!DGGBYHKLFOH UBEXV  CREATE OBJECT r_truck EXPORTING im_make = VOLVO im_cargo = 48. UBUHQWDO!DGGBYHKLFOH UBWUXFN  ******* create truck ***************************************** ******* create truck *****************************************

***** insert business-parnter of agency into partner_list*********** r_agency->add_partner( r_rental ). ******* show attributes of all partners of travel_agency ****** r_agency->display_agency_partners( ).

SAP AG

BC401

11-17

*&---------------------------------------------------------------------* *& Include SAPBC401_EVES_A * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* CLASS lcl_airplane DEFINITION. PUBLIC SECTION. "--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30. METHODS: constructor IMPORTING im_name display_attributes. CLASS-METHODS: display_n_o_airplanes. HYHQWV DLUSODQHBFUHDWHG PRIVATE SECTION. "---------------------------------------------METHODS: get_technical_attributes IMPORTING im_type EXPORTING ex_weight ex_tankcap DATA: name TYPE string, TYPE saplane-planetype TYPE s_plan_wei TYPE s_capacity. TYPE string im_planetype TYPE saplane-planetype,

planetype TYPE saplane-planetype. CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. "lcl_airplane DEFINITION

SAP AG

BC401

11-18

*------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. name planetype = im_name. = im_planetype.

n_o_airplanes = n_o_airplanes + 1. UDLVH HYHQW DLUSODQHBFUHDWHG ENDMETHOD. METHOD display_attributes. DATA: weight TYPE saplane-weight, cap TYPE saplane-tankcap. WRITE: / icon_ws_plane AS ICON, / Name of airplane(001), AT pos_1 name, / Type of airplane: (002), AT pos_1 planetype. get_technical_attributes( EXPORTING im_type = planetype IMPORTING ex_weight = weight ex_tankcap = cap ). WRITE: / Weight:(003), weight, Tankkap:(004), cap. ENDMETHOD. METHOD display_n_o_airplanes. WRITE: /, / Number of airplanes: (ca1), AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. ENDMETHOD. "display_n_o_airplanes "display_attributes "constructor

METHOD get_technical_attributes. SELECT SINGLE weight tankcap FROM saplane INTO (ex_weight, ex_tankcap) WHERE planetype = im_type. IF sy-subrc <> 0. ex_weight = 100000. ex_tankcap = 10000. ENDIF. ENDMETHOD. ENDCLASS. "get_technical_attributes "lcl_airplane IMPLEMENTATION

SAP AG

BC401

11-19

*---------------------------------------------------------------------* * CLASS lcl_cargo_plane DEFINITION *---------------------------------------------------------------------* CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane. PUBLIC SECTION. "---------------------METHODS: constructor IMPORTING im_name TYPE string im_planetype TYPE saplane-planetype im_cargo TYPE scplane-cargomax. METHODS: display_attributes REDEFINITION. PRIVATE SECTION. "---------------------DATA: max_cargo TYPE scplane-cargomax. ENDCLASS. "lcl_cargo_plane DEFINITION

*---------------------------------------------------------------------* * CLASS lcl_cargo_plane IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_cargo_plane IMPLEMENTATION.

METHOD constructor. CALL METHOD super->constructor EXPORTING im_name = im_name im_planetype = im_planetype. max_cargo = im_cargo. ENDMETHOD. "constructor

METHOD display_attributes. super->display_attributes( ). WRITE: / Max Cargo = , max_cargo. ULINE. ENDMETHOD. ENDCLASS. "display_attributes "lcl_cargo_plane IMPLEMENTATION

SAP AG

BC401

11-20

*---------------------------------------------------------------------* * CLASS lcl_passenger_plane DEFINITION *---------------------------------------------------------------------* CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane.. PUBLIC SECTION. METHODS: constructor IMPORTING im_name TYPE string im_planetype TYPE saplane-planetype im_seats TYPE sflight-seatsmax. METHODS: display_attributes REDEFINITION. PRIVATE SECTION. DATA: max_seats TYPE sflight-seatsmax. ENDCLASS. "lcl_passenger_plane DEFINITION

*---------------------------------------------------------------------* * * *---------------------------------------------------------------------* CLASS lcl_passenger_plane IMPLEMENTATION. CLASS lcl_passenger_plane IMPLEMENTATION *---------------------------------------------------------------------*

METHOD constructor. CALL METHOD super->constructor EXPORTING im_name = im_name im_planetype = im_planetype. max_seats = im_seats. ENDMETHOD. "constructor

METHOD display_attributes. super->display_attributes( ). WRITE: / Max Seats = , max_seats. ULINE. ENDMETHOD. ENDCLASS. "display_attributes "lcl_passenger_plane IMPLEMENTATION

SAP AG

BC401

11-21

*---------------------------------------------------------------------* * CLASS lcl_carrier DEFINITION *---------------------------------------------------------------------* CLASS lcl_carrier DEFINITION. PUBLIC SECTION. "---------------------------------------INTERFACES lif_partners. METHODS: constructor IMPORTING im_name TYPE string, get_name RETURNING value(ex_name) TYPE string, DGGBDLUSODQH IRU HYHQW DLUSODQHBFUHDWHG RI OFOBDLUSODQH ,03257,1* VHQGHU display_airplanes, display_attributes. PRIVATE SECTION. "----------------------------------DATA: name ENDCLASS. TYPE string, "lcl_carrier DEFINITION airplane_list TYPE TABLE OF REF TO lcl_airplane.

*---------------------------------------------------------------------* * CLASS lcl_carrier IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_carrier IMPLEMENTATION. METHOD lif_partners~display_partner. display_airplanes( ). ENDMETHOD. METHOD add_airplane. APPEND sender TO airplane_list. ENDMETHOD. METHOD display_attributes. WRITE: icon_flight AS ICON, name . ULINE. ULINE. display_airplanes( ). ENDMETHOD. METHOD display_airplanes. DATA: r_plane TYPE REF TO lcl_airplane. LOOP AT airplane_list INTO r_plane. r_plane->display_attributes( ). ENDLOOP. ENDMETHOD. "display_airplanes "display_attributes "add_airplane "lif_partners~display_partner

SAP AG

BC401

11-22

METHOD constructor. name = im_name. VHW KDQGOHU DGGBDLUSODQH IRU DOO LQVWDQFHV ENDMETHOD. METHOD get_name. ex_name = name. ENDMETHOD. ENDCLASS. "get_name "lcl_carrier IMPLEMENTATION "constructor

SAP AG

BC401

11-23

6ROXWLRQV RSWLRQDO
7RSLF
*& Report *& *& *& Implement Events in LCL_vehicle and lcl_airplane Implement Events in LCL_carrier and lcl_rental

8QLW

(YHQWV

7ULJJHULQJ DQG +DQGOLQJ (YHQWV


* * * *

*&---------------------------------------------------------------------* SAPBC401_EVES_MAIN_B

*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------* REPORT sapbc401_eves_main_b.

TYPES: ty_fuel TYPE p DECIMALS 2, ty_cargo TYPE p DECIMALS 2. INCLUDE <icon>. include sapbc401_vehd_i. INCLUDE sapbc401_eves_b. DATA: r_vehicle TYPE REF TO lcl_vehicle, r_truck TYPE REF TO lcl_truck, r_bus TYPE REF TO lcl_bus, r_passenger type ref to lcl_passenger_plane, r_cargo type ref to lcl_cargo_plane, r_carrier type ref to lcl_carrier, r_rental type ref to lcl_rental, r_agency type ref to lcl_travel_agency. START-OF-SELECTION. *######################## ******* create travel_agency ***************************************** CREATE OBJECT r_agency EXPORTING im_name = Fly&Smile Travel. ******* create rental ***************************************** CREATE OBJECT r_rental EXPORTING im_name = HAPPY CAR RENTAL.

SAP AG

BC401

11-24

******* create truck ***************************************** CREATE OBJECT r_truck EXPORTING im_make = MAN im_cargo = 45. ******* create truck ***************************************** CREATE OBJECT r_bus EXPORTING im_make = Mercedes im_passengers = 80. ******* create truck ***************************************** CREATE OBJECT r_truck EXPORTING im_make = VOLVO im_cargo = 48. ***** Create CARRIER ******************************************** create object r_carrier exporting im_name = Smile&Fly-Travel. ***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************ CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ******* show attributes of all partners of travel_agency ****** r_agency->display_agency_partners( ).

SAP AG

BC401

11-25

*&---------------------------------------------------------------------* *& Include SAPBC401_EVES_B * *&---------------------------------------------------------------------* *------------------------------------------------------------------* * events in: lcl_airplane and lcl_carrier ! * *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* CLASS lcl_airplane DEFINITION. PUBLIC SECTION. "--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30. METHODS: constructor IMPORTING im_name display_attributes. CLASS-METHODS: display_n_o_airplanes. EVENTS:airplane_created. PRIVATE SECTION. "---------------------------------------------METHODS: get_technical_attributes IMPORTING im_type EXPORTING ex_weight ex_tankcap DATA: name TYPE string, TYPE saplane-planetype TYPE s_plan_wei TYPE s_capacity. TYPE string im_planetype TYPE saplane-planetype,

planetype TYPE saplane-planetype. CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. "lcl_airplane DEFINITION

SAP AG

BC401

11-26

*------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. name planetype = im_name. = im_planetype.

n_o_airplanes = n_o_airplanes + 1. RAISE EVENT airplane_created. ENDMETHOD. METHOD display_attributes. DATA: weight TYPE saplane-weight, cap TYPE saplane-tankcap. WRITE: / icon_ws_plane AS ICON, / Name of airplane(001), AT pos_1 name, / Type of airplane: (002), AT pos_1 planetype. get_technical_attributes( EXPORTING im_type = planetype IMPORTING ex_weight = weight ex_tankcap = cap ). WRITE: / Weight:(003), weight, Tankkap:(004), cap. ENDMETHOD. METHOD display_n_o_airplanes. WRITE: /, / Number of airplanes: (ca1), AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. ENDMETHOD. "display_n_o_airplanes "display_attributes "constructor

METHOD get_technical_attributes. SELECT SINGLE weight tankcap FROM saplane INTO (ex_weight, ex_tankcap) WHERE planetype = im_type. IF sy-subrc <> 0. ENDMETHOD. ENDCLASS. ... ex_weight = 100000. ex_tankcap = 10000. ENDIF. "get_technical_attributes "lcl_airplane IMPLEMENTATION

SAP AG

BC401

11-27

*---------------------------------------------------------------------* * CLASS lcl_carrier DEFINITION *---------------------------------------------------------------------* CLASS lcl_carrier DEFINITION. PUBLIC SECTION. "---------------------------------------INTERFACES lif_partners. METHODS: constructor IMPORTING im_name TYPE string, get_name RETURNING value(ex_name) TYPE string, add_airplane FOR EVENT airplane_created OF lcl_airplane IMPORTING sender, PRIVATE SECTION. "----------------------------------DATA: name ENDCLASS. TYPE string, "lcl_carrier DEFINITION airplane_list TYPE TABLE OF REF TO lcl_airplane.

*---------------------------------------------------------------------* * CLASS lcl_carrier IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_carrier IMPLEMENTATION. METHOD lif_partners~display_partner. display_attributes( ). ENDMETHOD. METHOD add_airplane. APPEND sender TO airplane_list. ENDMETHOD. METHOD display_attributes. skip 2. WRITE: icon_flight AS ICON, name . ULINE. ULINE. display_airplanes( ). ENDMETHOD. METHOD display_airplanes. DATA: r_plane TYPE REF TO lcl_airplane. LOOP AT airplane_list INTO r_plane. r_plane->display_attributes( ). ENDLOOP. ENDMETHOD. "display_airplanes "display_attributes "add_airplane "lif_partners~display_partner

SAP AG

BC401

11-28

METHOD constructor. name = im_name. SET HANDLER add_airplane FOR ALL INSTANCES. 5$,6( (9(17 OLIBSDUWQHUVaSDUWQHUBFUHDWHG ENDMETHOD. ENDCLASS. "constructor "lcl_carrier IMPLEMENTATION

*&---------------------------------------------------------------------* *& Include SAPBC401_VEHD_i * *&---------------------------------------------------------------------* *---------------------------------------------------------------------* * define client lcl_travel_agency * it will use the interface lif_partners * * implement EVENT in LCL_VEHICLE and LCL_RENTAL *---------------------------------------------------------------------* ,17(5)$&( OLIBSDUWQHUV 0(7+2'6 GLVSOD\BSDUWQHU (9(176 SDUWQHUBFUHDWHG

HYHQW GHILQHG LQVLGH WKH LQWHUIDFH (1',17(5)$&(

OLIBSDUWQHUV

*---------------------------------------------------------------------* * CLASS lcl_vehicle DEFINITION *---------------------------------------------------------------------* CLASS lcl_vehicle DEFINITION. PUBLIC SECTION. "------------------METHODS: get_average_fuel IMPORTING im_distance TYPE s_distance im_fuel TYPE ty_fuel RETURNING value(re_avgfuel) TYPE ty_fuel. METHODS METHODS METHODS METHODS constructor IMPORTING im_make TYPE string. display_attributes. set_make IMPORTING im_make TYPE string. get_make EXPORTING ex_make TYPE string.

CLASS-METHODS get_count EXPORTING re_count TYPE i. EVENTS: vehicle_created. PRIVATE SECTION. "------------------DATA: make METHODS TYPE string. init_make.

SAP AG

BC401

11-29

CLASS-DATA: ENDCLASS.

n_o_vehicles TYPE i. "lcl_vehicle DEFINITION

*---------------------------------------------------------------------* * CLASS lcl_vehicle IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_vehicle IMPLEMENTATION. METHOD get_average_fuel. re_avgfuel = im_distance / im_fuel. ENDMETHOD. METHOD constructor. make = im_make. ADD 1 TO n_o_vehicles. raise event vehicle_created. ENDMETHOD. METHOD set_make. IF im_make IS INITIAL. init_make( ). ELSE. make = im_make. ENDIF. ENDMETHOD. METHOD init_make. make = default make. ENDMETHOD. METHOD get_make. ex_make = make. ENDMETHOD. METHOD display_attributes. WRITE: make. ENDMETHOD. METHOD get_count. re_count = n_o_vehicles. ENDMETHOD. ENDCLASS. "get_count "lcl_vehicle IMPLEMENTATION "display_attributes "get_make "init_make "set_make " me->init_make( ). also possible "constructor "get_average_fuel

SAP AG

BC401

11-30

*---------------------------------------------------------------------* * CLASS lcl_rental DEFINITION *---------------------------------------------------------------------* CLASS lcl_rental DEFINITION. PUBLIC SECTION. "------------------METHODS: 0(7+2'6 METHODS PRIVATE SECTION. "------------------DATA: name TYPE string, vehicle_list TYPE TABLE OF REF TO lcl_vehicle. ENDCLASS. "lcl_rental DEFINITION constructor IMPORTING im_name TYPE string. DGGBYHKLFOH IRU HYHQW YHKLFOHBFUHDWHG RI OFOBYHKLFOH LPSRUWLQJ VHQGHU display_attributes.

INTERFACES: lif_partners.

*---------------------------------------------------------------------* * CLASS lcl_rental IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_rental IMPLEMENTATION. METHOD lif_partners~display_partner. display_attributes( ). ENDMETHOD. METHOD constructor. "lif_partners~display_partner

name = im_name. set handler add_vehicle for all instances. UDLVH HYHQW OLIBSDUWQHUVaSDUWQHUBFUHDWHG ENDMETHOD. METHOD add_vehicle. "add_vehicle "constructor

APPEND sender TO vehicle_list. ENDMETHOD.

SAP AG

BC401

11-31

METHOD

display_attributes.

DATA: r_vehicle TYPE REF TO lcl_vehicle. skip 2. WRITE: / WRITE: icon_transport_proposal AS ICON, name. Here comes the vehicle list: . ULINE. ULINE.

LOOP AT vehicle_list INTO r_vehicle. r_vehicle->display_attributes( ). ENDLOOP. ENDMETHOD. ENDCLASS. "display_attributes "lcl_rental IMPLEMENTATION

*---------------------------------------------------------------------* * CLASS lcl_travel_agency DEFINITION *---------------------------------------------------------------------* CLASS lcl_travel_agency DEFINITION. PUBLIC SECTION. "------------------METHODS: 0(7+2'6 METHODS PRIVATE SECTION. "------------------DATA: name TYPE string, partner_list TYPE TABLE OF REF TO lif_partners. ENDCLASS. "lcl_travel_agency DEFINITION constructor IMPORTING im_name TYPE string. DGGBSDUWQHU IRU HYHQW SDUWQHUBFUHDWHG RI OLIBSDUWQHUV ,03257,1* VHQGHU display_agency_partners.

*---------------------------------------------------------------------* * CLASS lcl_travel_agency IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_travel_agency IMPLEMENTATION. METHOD display_agency_partners. DATA: r_partner TYPE REF TO lif_partners. WRITE: icon_dependents AS ICON, name. WRITE: Here are the partners of the travel agency: .ULINE.ULINE. LOOP AT partner_list INTO r_partner. r_partner->display_partner( ). ENDLOOP. ENDMETHOD. "display_agency_partners

SAP AG

BC401

11-32

METHOD

constructor.

name = im_name. VHW KDQGOHU DGGBSDUWQHU IRU DOO LQVWDQFHV ENDMETHOD. METHOD add_partner. "add_partner "lcl_travel_agency IMPLEMENTATION "constructor

$33(1' VHQGHU 72 SDUWQHUBOLVW ENDMETHOD. ENDCLASS.

SAP AG

BC401

11-33

*OREDO &ODVVHV DQG ,QWHUIDFHV &RQWHQWV


l /RFDO YHUVXV JOREDO FODVVHV DQG LQWHUIDFHV l &ODVV %XLOGHU

SAP AG 2002

SAP AG

BC401

12-1

*OREDO &ODVVHV DQG ,QWHUIDFHV 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 'HVFULEH WKH GLIIHUHQFH EHWZHHQ ORFDO DQG JOREDO FODVVHV DQG LQWHUIDFHV l &UHDWH JOREDO FODVVHV DQG LQWHUIDFHV XVLQJ WKH &ODVV %XLOGHU

SAP AG 2002

SAP AG

BC401

12-2

s$7x y H PQ@ R 6 S6 A F9@ E DH 6 B 89@ 876 5 H G I6 C A C B A 6 h U fc e g fe d b ` Y U X c X aX WVT %# !   $ "  0(  & 2  %  1)&'!  !  3 4   4  !          g e d k ijhfQQu Q s FD n m f l n p po sd $ k F r o k Q q n k o s oe k q k $t u | z } |Iz{ wwy } I{y xvv   I I|| ~ ~~ ~~|| ww ww  ~ a|| 

fW eb c i w v t WX U b c 1h ue b e i qp r sX e U c g be e i

SAP AG
SAP AG 2002

*OREDO &ODVVHV DQG ,QWHUIDFHV ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

12-3

*OREDO &ODVVHV DQG ,QWHUIDFHV 

&ODVV %XLOGHU &ODVV %XLOGHU


:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV :RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH $SSOLHG H[DPSOH

SAP AG 2002

SAP AG

BC401

12-4

5HYLHZ /RFDO &ODVVHV DQG ,QWHUIDFHV

3URJUDPP

5(3257 SURJ &/$66 OFOBDLUSODQH '(),1,7,21


...

3URJUDPP

5(3257 SURJ '$7$ UBDLUSODQH 7<3( 5() 72 OFOBDLUSODQH

(1'&/$66

...

...

/RFDO SURJUDP FODVVHV


/RFDO FODVVHV DUH RQO\ YLVLEOH LQ WKH SURJUDP WKH\ ZHUH GHILQHG LQ 1R JOREDO DFFHVV SRVVLEOH 1RW VWRUHG LQ WKH 5HSRVLWRU\ QR ZKHUHXVHG OLVW DQG VR RQ

SAP AG 2002

n n

Local classes and interfaces are only known within the program in which they are defined and implemented. Local classes and interfaces are not stored in the Repository (no TADIR entry). There is no "global" access to these classes or interfaces (for example, from other programs).

SAP AG

BC401

12-5

*OREDO &ODVVHV DQG ,QWHUIDFHV


l l $%$3 :RUNEHQFK GHYHORSPHQW WRRO

7RRO IRU FUHDWLQJ WHVWLQJ DQG PDQDJLQJ JOREDO FODVVHV DQG LQWHUIDFHV
6WRUHG LQ 5HSRVLWRU\

*HQHUDWHV WKH IUDPHZRUN FRGLQJ IRU H[DPSOH &/$66 QDPH! '(),1,7,21


l l l

$FFHVV IURP DOO SURJUDPV XVLQJ 7<3( 5() 72

0DQDJHV WKH LQFOXGH SURJUDPV LQ ZKLFK WKH FRGLQJ LV VWRUHG

&XVWRPHU QDPHVSDFH IRU JOREDO FODVVHV DQG LQWHUIDFHV < RU = :KHUHXVHG OLVW DYDLODEOH

SAP AG 2002

Unlike local program classes and interfaces, global classes and interfaces can be created and implemented using the ABAP Workbench tool &ODVV %XLOGHU. These classes and interfaces are then available active throughout the whole system. Global class and interface names share the same namespace. Individual methods of global classes and interfaces can be transported separately.

n n

SAP AG

BC401

a$j

12-6

&UHDWLQJ *OREDO &ODVVHV LQ WKH 2EMHFW 1DYLJDWRU


Workbench
Edit
Goto
Utilities
Environment
System
Help

SAP AG 2002

n n n n n

You can also display the Class Builders global classes in the Object Navigator. Being able to view classes through the navigation window on the left is especially useful when developing code. Here, you can also create a new class. Proceed as you would when creating a program: Use the context menu for a package node or directly for a class node within a package. Alternatively, you can also create a new class by entering a class name and then choosing 'LVSOD\. In this case, you must specify a package. I dialog box asks you to make further specifications for the new class: Instance creation: Where are instances of this new class to be created? For example, if you specify SULYDWH the class could only be instantiated within the class itself. The default setting is SXEOLF.

Exception class (this will be discussed in more detail later, in the context of exception handling) Persistent class (this will be discussed in more detail later, in the context of special techniques) Final class (represents the end of an inheritance tree) Only modeled (class definition only for modeling)

SAP AG

) s s $a h 1s7D

Object Name

Description

1RUPDO $%$3 FODVV ([FHSWLRQ FODVV 3HUVLVWHQW FODVV )LQDO FODVV 2QO\ PRGHOHG

BC401

11{

'aQ a1{ua'1

MIME Repository Repository Browser Repository Information System Tag Library Transport Organizer

&UHDWH D FODVV LQ WKH 2EMHFW 1DYLJDWRU


$

,QVWDQFH FUHDWLRQ

SULYDWH SURWHFWHG DEVWUDFW

1s77 s$

12-7

VD )hD h 7 D )7 ) j 1V
 h

 ' & " $ %F " h D7Va

h

h h h

j j j 7 h D  

V " DVF DVF Q7

"#$ !7        V  7 D 1"a

Q h 71V"

D

h7V Q$ 1"'

1h

V$V"

V j" 
$ $F)$V (

4 j$5 7V
)$2VDh ( VD (

232$  !D1 ( 0 3 

{{{u{

SAP AG

In the Class Builder, choosing the $WWULEXWHV tab displays the class attributes and their properties (instance attribute/static attribute and the visibility area)

&ODVV %XLOGHU $WWULEXWHV

Class

SAP AG 2002

Edit

Goto

Utilities

Environment

System

BC401
Help

'HILQH FODVV DWWULEXWHV

12-8

$D D{7a V$h jQ7 "  (  6 ( 7 ) 1V V )


E A E R W E V U R CI D GGG YQ 'XGAB22ST2QBPBAHGC F E 1BAB9 D C @

"    h  hDV j$)"  V !'#  V " ' "  7 r )h  V " '  h th7V  DV   ( 7 % ( " h th7V  DV  hDjV a s7 "VFh 8 D $# AD c IDD hDjV H1b Ya`%W E e C Y 2B2A aId E A g QfI AD iBh`1HGC F E YDI A V qI iA a11#`p

$5 47 $
1j7Q (

3 3  2'$ s71 ( 0

$V )$V (

{ {{u{

SAP AG

Choose 3DUDPHWHU to define the signature for the method the cursor is currently on.

This task is simplified by the &RQVWUXFWRU function, choosing which automatically creates the constructor method. You still have to specify the interface and the source code for the method.

Under the 0HWKRGV tab, you can create methods.

&ODVV %XLOGHU 0HWKRGV

Class

SAP AG 2002

Edit

Goto

Utilities

Environment

'HILQH VLJQDWXUH RI VHOHFWHG PHWKRG KHUH &216758&725

System

BC401
Help

'HILQH FODVV PHWKRGV

12-9

"Dh wD) $  Q7 hD VQh j"$7 "  (   ( 7 ) 1V V )


E A E R W E V U S Q CI AD GGGQ Y 'XGAB22TR2BPBHGC F E 1BAB9 D C @

 " v' hu1 0 h  h7V$V" "     h  hDV j$)"  V !'#  V " '  7 r )h  V " ' "  h th7V  DV   ( 7 % ( " h th7V  DV  hDjV s7  ' "VFh 8 D $#

$5 7 $ 4

hDjV

AHGb PPW D c YIDD

2B2A ad E e C YI

iBgh`1HDGC F E A QfI A

E YDI A V qI iA a11#`p

23'$ s71 ( 0 3  

$V )$V (

{ {{u{

SAP AG
Class

All interface methods are then automatically listed under the 0HWKRGV tab.

All interface methods must then be implemented in this server class. In the example, the interface zif_fly consists of a single method start.

A global interface that was created in the Class Builder can be announced in a server class by choosing the ,QWHUIDFHV tab.

&ODVV %XLOGHU ,QWHUIDFH 0HWKRGV

SAP AG 2002

 0HWKRG LPSOHPHQWDWLRQ RI D JOREDO LQWHUIDFH

Edit

Goto

Utilities

Environment

System

BC401
Help

 $QQRXQFH D JOREDO LQWHUIDFH LQ D JOREDO FODVV

12-10

( (

7 

D#$y$ u h2

 'Q& " $ % " )7 7$

h

   

D) Q#8#  ( 7 % ( "
E A E h1h YQ R W E A V U S R Q C AD GB22T2BPIBHGC F iHGb PHW E AD c YIDD

u   V s    x h5 V" 7h7V  FQ V7hh7D

7hDjV EG219 D C A @ 4 V 7 $h

2B2A P2d E e C YI EhGG`BHGC F A g QfI AD 3 3   B'Q &V1 ( 0

$V )$V (

E Y I A V qI iA aD12p

Q1DV (

{{{{uaQ

SAP AG
Class SAP AG 2002
Edit
Goto
Utilities
Environment

Selecting a method and choosing 3DUDPHWHUV displays a list of all the methods signature parameters.

&ODVV %XLOGHU 0HWKRG ,QWHUIDFH

6LJQDWXUH SDUDPHWHUV RI PHWKRG KHUH &216758&725

System

BC401
Help

12-11

 % $7 hG  '&   ) Q7 &  ) GV    $ w 1DV$ % x


$2  (  "6 ( 7 hDjV

7 % V a' )) #   "   


Q
$ 6 ) # "  72 h

  $ u5    w ) 5 "V"



"Q7) "

{j {{ a{Qj

SAP AG
SAP AG 2002
Method
Edit
Goto
Utilities

In this example (constructor), the signature consists of the import parameters im_make and im_planetype.

When implementing methods, you can also display the signature of the method to aid your work.

&ODVV %XLOGHU 0HWKRG ,PSOHPHQWDWLRQ

Environment

BC401
System
Help

'LVSOD\ WKH LQWHUIDFH WR KHOS LQWHUIDFH WR KHOS 'LVSOD\ WKHZLWK LPSOHPHQWLQJ WKH PHWKRG ZLWK LPSOHPHQWLQJ WKH PHWKRG

12-12

&ODVV %XLOGHU ,QKHULWDQFH


Class
Edit
Goto
Utilities
Environment
System
Help

$QQRXQFH VXSHUFODVV
)$2VDh ( VD (

6XSHUFODVV 6XSHUFODVV
6XSHUFODVV
Description Instantiation

SAP AG 2002

=&/B$,53/$1(B



Final

n n n

Here, for example, is the 6XSHUFODVV function, which you can use to specify the superclass for the current class. In this example, the superclass ZCL_AIRPLANE_00 is specified for the subclass ZCL_CARGO_PLANE_00.

In the Class Builder, inheritance relationships are defined by choosing the 3URSHUWLHV tab.

SAP AG

BC401

Q h 71V"

4 j$5 7V

D

h7V Q V 1

{u{
3 3   V u $ ( 1 ( 0   

1h

V$V"

$ $F)$V ( V j" 

12-13

B#hD dTh$BQn$  i f d g f e d G2 $GiQn$  i s j s s d e d

i f d d yx QunD hhi

r Qiq p o h "F d z  i f d nD dhh$

d l m hss i k $j
w v u g s t
 } }  )%sx%u!&~ u{ } |
Class
Edit
Goto
Utilities
Handler

BiudhD udhgG$f1Q#TV f e d q srG` p G d o i f d d nD i)$

d m FhQ lh k j
 } }  ~ } 5 )!w!!&'|%{
Handler

sT

SAP AG
SAP AG 2002

If the class is completed and activated, you can use the test tool to create an instance of the class and test method calls of the class.

&ODVV %XLOGHU 7HVW (QYLURQPHQW

&UHDWH DQ LQVWDQFH ZLWK WKH WHVW HQYLURQPHQW

BC401

12-14

*OREDO &ODVVHV DQG ,QWHUIDFHV 

&ODVV %XLOGHU &ODVV %XLOGHU


:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV :RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH $SSOLHG H[DPSOH

SAP AG 2002

SAP AG

BC401

12-15

*OREDO &ODVVHV LQ WKH 2EMHFW 1DYLJDWRU


Workbench
Edit
Goto
Utilities
Environment
System
Help

Name of airplane Number of planes Airplane type CONSTRUCTOR


Displays airplane attributes Displays number of airplanes

SAP AG 2002

n n

You can also display the Class Builders global classes in the Object Navigator. Being able to view classes through the navigation window on the left is especially useful when editing source code. You can drag & drop specific components into the right Editor window and automatically create and insert source code (as a template).

SAP AG

BC401

ih uh ##%2'G61 ######### Bu#uBuuB#

PT

Bi#funD ud G$BQi$  d g f e d 1Bs# $1d Qi$  i j s s e d gs j s f g G#Q sGq r 2hTGuudnD i es i f BnD iG$f i f d d g %p6f i d

G " d o nD i)$ i f d d

Object Name

Class Airplane

Description

1xFxa

'aQ a1{ua'1

MIME Repository Repository Browser Repository Information System Tag Library Transport Organizer

/HIW 'LVSOD\LQJ D FODVV LQ WKH DOORZV \RX WR DFFHVV WKH FODVV FRPSRQHQWV

5LJKW (GLW DQ $%$3 SURJUDP LQ WKH

m )d
12-16

r uih

FQs7

u  'Hi
Pattern

i f d d Q#nD uhhV k z s  n )

Bi#funD ud G$BQi$  d g f e d 1Bs# $1d Qi$  i j s s e d gs j s f g G#Q sGq r 2hTGuudnD i es i f BnD iG$f i f d d g %p6f i d

TnnThnnTnnThnnTnnTh #fg BQ1$i Gg 1T s i v v s ds fg Bs #f hi h QF Q s BdTh e i QF Q$2 ``P```P```P``HH``HH``HH``HH`H`H`H` e i QF Q$2 ``P```P```P``HH``HH``HH``HH`H`H`H`

i f d d Q#nD uhhV k z s  n )
Transport Organizer

m "hd

r IhQu

Q

') ai
Edit
Goto
Utilities
Environment
System
Help

&ODVV %XLOGHU &5($7( 2%-(&7

Workbench

MIME Repository

'aQ a1{ua'1

Object Name

G " d o nD i)$ i f d d

SAP AG

6HOHFWLQJ DQG GUDJJLQJ a class name WR WKH ULJKW automatically creates a CREATE OBJECT statement with the whole interface of the constructor. You still have to add the calls actual parameters.

'UDJ

SAP AG 2002

GURS the class name: Instantiates an object.

Repository Information WKH (GLWRU System Tag Library

Repository Browser

&5($7( 2%-(&7

6HOHFWLQJ DQG GUDJJLQJ LQWR FUHDWHV

Displays airplane attributes Displays number of airplanes

CONSTRUCTOR

Name of airplane Number of planes Airplane type

Class Airplane

Description

BC401
Pattern

&5($7( 2%-(&7 UBDLUSODQH (;325,1*  ,03257,1* 

12-17

BfnD d 1Q2#TV  i d g f e d 12s $Gd #TV  i j s s e d 1#s g s j f g r q G i e s i f d 2iGnD i f d g 1#nD udhG$f i p d 6%f

iGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh nhBGBTGnTGu2B2i

i f d d Q#nD uhhV k z s  n )
Transport Organizer

m "hd

r IhQu

Q
Pattern

u  'Hi
Edit
Goto
Utilities
Environment
System
Help

&ODVV %XLOGHU &$// 0(7+2'

Workbench

MIME Repository

'aQ a1{ua'1

Repository Information System Tag Library 6HOHFWLQJ

Repository Browser

DQG GUDJJLQJ LQWR WKH (GLWRU FUHDWHV &$//

hihG1ih1GGhiuhG hBi Gih2uGGhhG GhhGhGhhGhGhiGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh Gih2uGGhhG GhhGhGhhGhGhiGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh

Object Name

si s" % ) d
Description

o G "F hd i f d d QnD ihV

SAP AG

6HOHFWLQJ DQG GUDJJLQJ a method WR WKH ULJKW automatically creates a CALL METHOD statement with the whole interface of the method. In this case you must also add the calls actual parameters and set the calls reference variable.

'UDJ

SAP AG 2002

GURS the method name: Calls a method

0(7+2'

Displays number of airplanes

Displays airplane attributes

CONSTRUCTOR

Name of airplane Number of planes Airplane type

BC401

&$// 0(7+2' [[[!GLVSOD\BDWWULEXWHV

12-18

*OREDO &ODVVHV DQG ,QWHUIDFHV 

&ODVV %XLOGHU &ODVV %XLOGHU


:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV :RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH $SSOLHG H[DPSOH

SAP AG 2002

SAP AG

BC401

12-19

6$3 *ULG &RQWURO


5x

$$ $$ $$ $$ /+ /+ /+ /+ /+

        

        

86' 86' 86' 86' '(0 '(0 '(0 '(0 '(0

SAP AG 2002

An applied example for the use of ABAP Objects is the standard output tool 6$3 *ULG &RQWURO. It is implemented in ABAP Objects and provides the user with a wide range of functions (for example sorting data, reorganizing/hiding columns, totalling). Principle of 6$3 &RQWURO )UDPHZRUN: To use such a control the developer no longer has to re-implement functions in the control, but simply specify the instances of the classes that contain these functions. The exact procedure for using the SAP Grid Control will be discussed in the following.

SAP AG

BC401

1Q 1{)1

'

i1

1w

7RROEDU

        

$ $   $ $ $ $ $

12-20

,QFOXGLQJ D *ULG &RQWURO ,QVWDQFH LQ D 'LDORJ 3URJUDP


<RX UHTXLUH WZR FODVVHV IRU XVLQJ D 6$3 *ULG &RQWURO

6FUHHQ $UHD
&XVWRP &RQWDLQHU &RQWURO
$/9 *ULG &RQWURO
 $  !  (  " 7 (  7 i (
Public Private

Public

SAP AG 2002

To implement a SAP Grid Control, you must implement: An instance of the container control An instance of the displaying SAP Grid Control The data retrieval The display of data using a method call

An SAP container is capable of housing other controls (such as the SAP Grid Control, tree control, picture control, splitter control, and so on). It manages these controls in a logical collection and provides a physical area for visualization. Each control "lives" in a container. Because containers are themselves controls, containers can be nested within one another. The container becomes the SDUHQW of its control. Global classes are available for accessing the custom control and the SAP Grid Control. At runtime, an object each of the class &/B*8,B&86720B&217$,1(5 and the class &/B*8,B$/9B*5,' is created. These objects contain all necessary information for accessing the controls. For information on the object types (classes) and the corresponding methods, refer to the online documentation.

SAP AG

BC401

` #V  7 G ( $  $ 
Private

12-21

'LVSOD\LQJ 'DWD ZLWK D *ULG &RQWURO ,QVWDQFH

...

&5($7( 2%-(&7 FRQWDLQHUBU (;3257,1* FRQWDLQHUBQDPH


...

&217$,1(5B

r FVm ss B  o

Public

&5($7( 2%-(&7 JULGBU (;3257,1* LBSDUHQW


...

FRQWDLQHUBU

Public

Private

JULGBU!VHWBWDEOHBIRUBILUVWBGLVSOD\ LBVWUXFWXUHBQDPH
6$3/$1(
LWBRXWWDE LWDEBVDSODQH
...
alv_grid_control

SAP AG 2002

n n n

The control is displayed on a screen, the call of which is not shown in this example. To create the object that communicates with the container control, it is sufficient to include the name of the container area
&217$,1(5B
on the screen, on which the container area is defined. To create the object that communicates with the SAP Grid Control, you must include the reference variable that refers to the object for the custom container. This is because the object for the SAP Grid Control contains a reference variable that points to the container object as a private attribute. This relationship tells the object, which container it should be included in. To display data in a SAP Grid Control, it must be provided in an internal table. Then a method is called that receives the content of the internal table and its structure. The name of this method id VHWBWDEOHBIRUBILUVWBGLVSOD\. If, while the program runs, only the content of the internal table to be displayed changes, it is sufficient to call the method UHIUHVKBWDEOHBGLVSOD\ with the container area before the the screen is re-sent.

SAP AG

BC401

2 iQ oBQ s o
Private

'$7$ FRQWDLQHUBU 7<3( 5() 72 FOBJXLBFXVWRPBFRQWDLQHU JULGBU 7<3( 5() 72 FOBJXLBDOYBJULG 


...

12-22

*OREDO &ODVVHV DQG ,QWHUIDFHV 8QLW 6XPPDU\

l 'HVFULEH WKH GLIIHUHQFH EHWZHHQ ORFDO DQG JOREDO FODVVHV DQG LQWHUIDFHV l &UHDWH JOREDO FODVVHV DQG LQWHUIDFHV XVLQJ WKH &ODVV %XLOGHU

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

12-23

([HUFLVHV
8QLW *OREDO &ODVVHV DQG ,QWHUIDFHV 7RSLF &UHDWLQJ *OREDO &ODVVHV

At the conclusion of these exercises, you will be able to: Use the Class Builder to process global classes and interfaces

A travel agency communicates with its business partners. Various hotels are now also to be business partners.

0RGHO VROXWLRQ &/B+27(/ &/B+286( ,)B3$571(56 Create the global class =&/B B+27(/ in the Class Builder.

1-1 1-2

The class is to have the following attributes: QDPH type STRING, private instance attribute PD[BEHGV type I, private instance attribute QBRBKRWHO type I, private static attribute The class is to have the following methods: FRQVWUXFWRU with import parameters LPBQDPH and LPBEHGV GLVSOD\BDWWULEXWHV for displaying the instance attributes of the class GLVSOD\BQBRBKRWHOV static method for displaying instances created Implement these methods. Activate and test your class using the Class Builder test tool.

1-3

1-4 1-5

SAP AG

BC401

12-24

Define the global interface =,)B B3$571(56 with the interface method GLVSOD\BSDUWQHU. Implement the interface in the class =&/B B+27(/. 2SWLRQDO Include the new class in the program with the airlines and car rental companies. Adjust the sections that refer to the local interface OLIBSDUWQHUV to suit the new global interface.

OFOBWUDYHOBDJHQF\
XVHV

]LIBSDUWQHUV
LPSOHPHQWV

OFOBFDUULHU

OFOBKRWHO

OFOBFDUBUHQWDO

OFOBDLUSODQH

OFOBYHKLFOHV









4 RSWLRQDO Define an inheritance relationship in the Class Builder. Your hotel is to inherit from the superclass =&/B B+286(. Tip: Construct the class =&/B B+286( copying the class =&/B B+27(/ to =&/B B+286( and then making changes to this copy. The house should only have an attribute QDPH (protected) and the method GLVSOD\BDWWULEXWHV; delete all superfluous components. Finally, define the inheritance relationship between the house and the hotel.

SAP AG

BC401

12-25

6ROXWLRQV
8QLW 7RSLF *OREDO &ODVVHV DQG ,QWHUIDFHV &UHDWLQJ *OREDO &ODVVHV

*&---------------------------------------------------------------------* *& Report *& *& REPORT SAPBC401_CLSS_MAIN_A * * * *&---------------------------------------------------------------------* Global Class lcl_hotel is also a business partner of the travel agency sapbc401_clss_main_a.

*&---------------------------------------------------------------------*

TYPES: ty_fuel TYPE p DECIMALS 2, ty_cargo TYPE p DECIMALS 2. INCLUDE <icon>. include sapbc401_vehd_j. INCLUDE sapbc401_clss_a. DATA: r_vehicle TYPE REF TO lcl_vehicle, r_truck TYPE REF TO lcl_truck, r_bus TYPE REF TO lcl_bus, r_passenger type ref to lcl_passenger_plane, r_cargo type ref to lcl_cargo_plane, r_carrier type ref to lcl_carrier, r_rental type ref to lcl_rental, r_agency type ref to lcl_travel_agency, UBKRWHO W\SH UHI WR FOBKRWHO START-OF-SELECTION. *######################## ******* create travel_agency ***************************************** CREATE OBJECT r_agency EXPORTING im_name = Fly&Smile Travel. ******* create rental ***************************************** CREATE OBJECT r_rental EXPORTING im_name = HAPPY CAR RENTAL.

SAP AG

BC401

12-26

******* create truck ***************************************** CREATE OBJECT r_truck EXPORTING im_make = MAN im_cargo = 45. ***** Create CARRIER ******************************************** create object r_carrier exporting im_name = Smile&Fly-Travel. ***** Passenger Plane ******************************************** CREATE OBJECT r_passenger EXPORTING im_name = LH BERLIN im_planetype = 747-400 im_seats = 345. ***** cargo Plane ************************************************ CREATE OBJECT r_cargo EXPORTING im_name = US HErcules im_planetype = 747-500 im_cargo = 533. ******* create hotel ***************************************** FUHDWH REMHFW UBKRWHO H[SRUWLQJ LPBQDPH LPBEHGV
+2/,'$< ,11


******* show attributes of all partners of travel_agency ****** r_agency->display_agency_partners( ).

SAP AG

BC401

12-27

6SHFLDO 7HFKQLTXHV &RQWHQWV


l $EVWUDFW DQG ILQDO FODVVHV l 9LVLELOLW\ RI FRQVWUXFWRUV l )ULHQGV l 2EMHFW 6HUYLFHV

SAP AG 2002

SAP AG

BC401

13-1

6SHFLDO 7HFKQLTXHV 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l &UHDWH DQG GHVFULEH DEVWUDFW DQG ILQDO FODVVHV l ([SODLQ WKH YLVLELOLW\ FULWHULD RI FRQVWUXFWRUV l 'HVFULEH WKH IULHQG FRQFHSW l 'HVFULEH WKH SHUVLVWHQFH VHUYLFH RI 2EMHFW 6HUYLFHV

SAP AG 2002

SAP AG

BC401

13-2

y & &"$Gx R P S U T 8 8 EB "@A H FGP E8 CD 9@A 8 A 8 P QI8 F B D B 9 7 h W fc e g fY &c CY 5CY $V e d b a ` XW %# ! !   &$ "   ) '   ' 3  %  120(!  6   6  ! 45        igSSQ C "$ j hf e d e k & m m o on  l j " q n j r C p m j n s nd j p j uGt f n s n v &n d j p l n } | z 5 ~{xyw 5z }} y~| z

fX eb c i w u s XY W b c vh te b e i qp r Y e W c &g be e i

SAP AG
SAP AG 2002

6SHFLDO 7HFKQLTXHV ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

13-3

6SHFLDO 7HFKQLTXHV 

$EVWUDFW DQG ILQDO FODVVHV $EVWUDFW DQG ILQDO FODVVHV


9LVLELOLW\ RI FRQVWUXFWRUV 9LVLELOLW\ RI FRQVWUXFWRUV
7KH IULHQG FRQFHSW 7KH IULHQG FRQFHSW
3HUVLVWHQW REMHFWV 3HUVLVWHQW REMHFWV

SAP AG 2002

SAP AG

BC401

13-4

$EVWUDFW &ODVVHV

l $EVWUDFW FODVVHV WKHPVHOYHV FDQQRW EH LQVWDQWLDWHG DOWKRXJK WKHLU VXEFODVVHV FDQ l $EVWUDFW LQVWDQFH PHWKRGV DUH GHILQHG LQ WKH FODVV EXW QRW LPSOHPHQWHG
7KH\ PXVW EH UHGHILQHG LQ VXEFODVVHV

5HIHUHQFHV WR DEVWUDFW FODVVHV FDQ UHIHU WR LQVWDQFHV RI VXEFODVVHV

&/$66 OFOBYHKLFOH '(),1,7,21 $%675$&7 38%/,& 6(&7,21 0(7+2'6 HVWLPDWHBIXHOBFRQVXPSWLRQ $%675$&7 ,03257,1*  (1'&/$66

&ODVV FDQQRW EH LQVWDQWLDWHG

0HWKRG QRW LPSOHPHQWHG LQ WKLV FODVV


SAP AG 2002

It is not possible to instantiate objects of an abstract class However, this does not mean that references to such a class are not useful. On the contrary, they are very useful, since they can (and must) refer to instances in subclasses of the abstract class at runtime. The CREATE OBJECT statement is extended in this context. You can specify the class of the instance to be created explicitly: CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>. Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, for example to define a uniform interface. Abstract instance methods are used to specify particular interfaces for subclasses, without having to immediately provide implementation for them. Abstract methods need to be redefined and thereby implemented in the subclass (here you also need to include the corresponding redefinition statement in the DEFINITION part of the subclass). Classes with at least one abstract method are themselves abstract. Static methods and constructors cannot be abstract (they cannot be redefined).

n n

n n

SAP AG

BC401

13-5

)LQDO &ODVVHV

l )LQDO FODVVHV FDQQRW KDYH VXEFODVVHV


g

...

...

)LQDO PHWKRGV FDQQRW EH UHGHILQHG LQ VXEFODVVHV

&/$66 OFOBEXV '(),1,7,21 ,1+(5,7,1* )520 OFOBYHKLFOH 38%/,& 6(&7,21 0(7+2'6 HVWLPDWHBQXPEHUBRIBIUHHBVHDWV ),1$/ (1'&/$66

SAP AG 2002

n n n

A final class cannot have subclasses, and can protect itself in this way against (uncontrolled) specialization. A final method in a class cannot be redefined in a subclass, and can protect itself in this way against (uncontrolled) redefinition. Some features: A final class implicitly only contains final methods. In this case, you cannot enter FINAL explicitly for these methods. Methods cannot be both final and abstract. Classes, on the other hand, that are both abstract and final can be useful: Only static components can be used.

SAP AG

BC401

ii

&/$66 OFOBWUXFN '(),1,7,21 ),1$/ ,1+(5,7,1* )520 OFOBYHKLFOH  (1'&/$66

...

13-6

6SHFLDO 7HFKQLTXHV 

$EVWUDFW DQG ILQDO FODVVHV $EVWUDFW DQG ILQDO FODVVHV


9LVLELOLW\ RI FRQVWUXFWRUV 9LVLELOLW\ RI FRQVWUXFWRUV
7KH IULHQG FRQFHSW 7KH IULHQG FRQFHSW
3HUVLVWHQW REMHFWV 3HUVLVWHQW REMHFWV

SAP AG 2002

SAP AG

BC401

13-7

:KR &DQ ,QVWDQWLDWH &ODVVHV"


l 7KH XVDELOLW\ RI FODVVHV FDQ EH UHVWULFWHG &/$66 FOBFODVV '(),1,7,21 
(YHU\ XVHU FOLHQW FDQ FUHDWH LQVWDQFHV RI D FODVV GHIDXOW VHWWLQJ 2QO\ WKH FODVV LWVHOI DQG DOO LWV VXEFODVVHV FDQ FUHDWH LQVWDQFHV RI WKLV FODVV

&5($7( 38%/,&

l 7KLV LPSOLFLWO\ FRQWUROV WKH YLVLELOLW\ RI WKH FODVV


{vv~

SAP AG 2002

If you want to ensure that not every user (client) can instantiate a class, you can use the following additions to restrict the visibility area of the constructor and hence limit the use of the class. (The following additions must be preceded by CLASS ... DEFINITION.) The CREATE PUBLIC addition is implicitly available for every class definition, provided neither of the other two CREATE additions is used. It defines the default that HYHU\ user can create instances of a known class. The optional additions CREATE PROTECTED and CREATE PRIVATE, on the other hand, have the effect that not every user can create instances of a class. In the case of CREATE PROTECTED, only VXEFODVVHV of the class or the class itself and, in the case of CREATE PRIVATE, RQO\ WKH FODVV LWVHOI can create instances of the class. Therefore, the additions CREATE PROTECTED and CREATE PRIVATE allow you to control instance creation and, for example, are a prerequisite for the instance management of persistent objects, for which the uniqueness of objects must be ensured. (Persistent objects will be discussed in more detail later.)

If it is to be impossible to instantiate a class more than once (for example, because it serves as a data administrator or data container), you can use the singleton concept. The class is defined with the addition CREATE PRIVATE and FINAL and instantiated using its static constructor. A public static component could then make the reference to the class available to an external user.

SAP AG

BC401

g 

l 3RVVLEOH XVH FRQFHSW


{S

&5($7( 3527(&7(' &5($7( 35,9$7(

2QO\ WKH FODVV FDQ FUHDWH LQVWDQFHV RI LWVHOI

 OFOBVLQJOHWRQ
Public

Private

13-8

6SHFLDO 7HFKQLTXHV 

$EVWUDFW DQG ILQDO FODVVHV $EVWUDFW DQG ILQDO FODVVHV


9LVLELOLW\ RI FRQVWUXFWRUV 9LVLELOLW\ RI FRQVWUXFWRUV
7KH IULHQG FRQFHSW 7KH IULHQG FRQFHSW
3HUVLVWHQW REMHFWV 3HUVLVWHQW REMHFWV

SAP AG 2002

SAP AG

BC401

13-9

)ULHQGV
l ,Q UDUH FDVHV FODVVHV KDYH WR ZRUN WRJHWKHU FORVHO\ DQG PDNH DOO WKHLU FRPSRQHQWV LQFOXGLQJ WKH SURWHFWHG DQG SULYDWH RQHV DYDLODEOH WR HDFK RWKHU
(IILFLHQW GLUHFW DFFHVV WR WKH GDWD RI D FODVV SURYLGLQJ IULHQGVKLS

n n n n

0HWKRGV WKDW DFFHVV WKH VDPH GDWD FDQ WKHUHIRUH EH VSUHDG RYHU VHYHUDO FODVVHV

3DFNDJH FUHDWLRQ VXSSRUW


vEy5v E2 {

SAP AG 2002

In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes. A class can provide friendship to other classes and interfaces (and hence all classes that implement the interface). To do this you use the )5,(1'6 additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed. Friends are allowed to access the protected and private components of the class providing the friendship and can always create instances of this class, regardless of the CREATE addition to the CLASS statement. In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it. Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. Therefore, extreme caution is advised when providing friendship. The higher up a friend is in the inheritance tree, the more subclasses can access all components of a class providing friendship. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.

SAP AG

BC401

{v

"r

iEgiE r 2

tg{ 5y tg{

yv {
13-10

GQ5

igttE r Si

5

6SHFLDO 7HFKQLTXHV 

$EVWUDFW DQG ILQDO FODVVHV $EVWUDFW DQG ILQDO FODVVHV


9LVLELOLW\ RI FRQVWUXFWRUV 9LVLELOLW\ RI FRQVWUXFWRUV
7KH IULHQG FRQFHSW 7KH IULHQG FRQFHSW
3HUVLVWHQW REMHFWV 3HUVLVWHQW REMHFWV

SAP AG 2002

SAP AG

BC401

13-11

3HUVLVWHQFH 6HUYLFH

$%$3 SURJUDP

'DWDEDVH

SAP AG 2002

n n

The persistence service helps the programmer to work object-oriented with data in relational databases. In principle, ABAP programs work with data and objects that exist(s) in the internal session at runtime. They are transient when the program is stopped. If this data is to be stored programindependently, that is persistently, it must be stored in the database. (You could also use files on operating system level.) In ABAP Objects, this is done using the OPEN SQL interface. To be able to work with ABAP Objects persistently, the SHUVLVWHQFH VHUYLFH was introduced with SAP R/3 Basis Release 6.10. Objects and their attributes can be written to the database and reimported.

SAP AG

BC401

v E 2 ~y5&E2

{ v

3HUVLVWHQFH VHUYLFH

{ Ev S
13-12

)HDWXUHV RI WKH 3HUVLVWHQFH 6HUYLFH


l 2EMHFWV RI D UXQQLQJ $%$3 SURJUDP DUH l 3HUVLVWHQW FODVVHV DUH FUHDWHG LQ WKH &ODVV %XLOGHU

&UHDWH &ODVV &/B3(56,67(17B&/$66


Class Type

Normal ABAP Class Exception Class 3HUVLVWHQW &ODVV

l 3HUVLVWHQFH VHUYLFH WDVNV


/RDGLQJ WKH REMHFWV IURP WKH GDWDEDVH 0DQDJLQJ FKDQJHV WR WKH REMHFWV 6WRULQJ WKH REMHFWV LQ WKH GDWDEDVH

SAP AG 2002

n n

To use the persistence service for objects, their classes must be created as so-called SHUVLVWHQW FODVVHV in the Class Builder. The term SHUVLVWHQW FODVV indicates that the objects of the class and their state are managed by the persistence service. In ABAP programs, objects of these classes are, for example, not created using the normal statement CREATE OBJECT, but instead using a method of the persistence service that ensures the correct initialization. When creating a persistent class, the Class Builder automatically creates a corresponding class, the so called FODVV DFWRU or DJHQW, the methods of which are used to manage the objects of the persistent class. As well as their unique identity, persistent classes can also contain so-called key attributes, which the persistence service uses to ensure the uniqueness of the persistent objects contents.

SAP AG

BC401

l 7KH SHUVLVWHQFH VHUYLFH DOORZV \RX WR ZRUN ZLWK

REMHFWV

...

13-13

&ODVV $JHQW
5

l :LWKLQ WKH SHUVLVWHQFH VHUYLFH WKH FODVV DFWRU RU WKH SHUVLVWHQW REMHFWV

...

PDQDJHV

7KH DJHQW SURYLGHV D UDQJH RI VHUYLFHV PHWKRGV  ZKLFK DUH XVHG WR PDQDJH WKH REMHFWV DQG WKH HQFDSVXODWHG GDWD

7HFKQLFDOO\ WKH DJHQW LV D VLQJOHWRQ ZKLFK LV DGGUHVVHG XVLQJ WKH SXEOLF VWDWLF DWWULEXWH $*(17
7KH DJHQW D VLQJOHWRQ RI WKH FODVV &$B&$55,(5 DQG IULHQG RI WKH SHUVLVWHQW FODVV &/B&$55,(5

'$7$ UBFDUULHU 7<3( 5() 72 FOBFDUULHU UBDJHQW 7<3( 5() 72 FDBFDUULHU FDUUQDPH 7<3( VBFDUUQDPH UBDJHQW FDBFDUULHU !DJHQW

75< UBFDUULHU UBDJHQW!JHWBSHUVLVWHQW LBFDUULG FDUUQDPH UBFDUULHU!JHWBFDUUQDPH  :5,7(


/+
 FDUUQDPH &$7&+ F[BRVBREMHFWBQRWBIRXQG (1'75<
SAP AG 2002

For every persistent class cl_persistent, the Class Builder generates two further classes ca_persistent and cb_persistent. These classes make up the class-specific part of the persistence service. ca_persistent is the so-called FODVV DFWRU (or agent), which is used to manage the managed object of the class cl_persistent, and in which all actual database accesses take place. The class actor inherits the relevant methods from the abstract superclass cb_persistent. The programmer can extend the class actor and redefine the methods (especially the database accesses). The superclass cb_persistent cannot be changed. The class actor is a friend of the managed class. It has the attribute CREATE PRIVATE and exactly one instance of the class actor is created when it is first accessed. The static attribute AGENT is a reference variable with the type of the class ca_persistent. When the attribute is first accessed in an ABAP program, the static constructor of the class ca_persistent creates exactly one instance of this class, which points to the attribute AGENT. This object is part of the persistence service and its methods are used to manage the object of the persistent class. For each program there is only one object of the class ca_persistent, because you cannot create objects from outside using CREATE OBJECT. The class actor manages one or more objects of the relevant persistent class. These objects must have different keys.

SAP AG

/+


BC401

13-14

6SHFLDO 7HFKQLTXHV 8QLW 6XPPDU\

l &UHDWH DQG GHVFULEH DEVWUDFW DQG ILQDO FODVVHV l ([SODLQ WKH YLVLELOLW\ FULWHULD RI FRQVWUXFWRUV l 'HVFULEH WKH IULHQG FRQFHSW l 'HVFULEH WKH SHUVLVWHQFH VHUYLFH RI 2EMHFW 6HUYLFHV

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

13-15

([HUFLVHV  RSWLRQDO
8QLW 6SHFLDO 7HFKQLTXHV 7RSLF 6LQJOHWRQ &ODVVHV

At the conclusion of these exercises, you will be able to: Create a singleton; requirement for next exercise

0RGHO VROXWLRQ &/B6,1*/(721 6$3%&B63&6B0$,1B$

$GYDQFHG Create the global class =&/B B6,1*/(721 in the Class Builder. The following must be specified for the class:
1-1 You must be able to be instantiate the class only once. (Tip: Instantiation should take place automatically when the class is first accessed.) Instantiation only takes place within the class. The class has a static reference variable UBVLQJOHWRQ that refers to the instantiated object. The class has a static reference variable UBVLQJOHWRQ that refers to the instantiated object.

1-2 1-3

1-4

In the main program =%&B B0$,1B63(&,$/, instantiate the singleton class =&/B B6,1*/(721 by calling the JHWBVLQJOHWRQ method. 2-1 2-2 Why must the JHWBVLQJOHWRQ method in this example be static? Verify your blueprint by calling JHWBVLQJOHWRQ several times. (Debug.) When does instantiation take place and how often?

SAP AG

BC401

13-16

([HUFLVHV  RSWLRQDO
8QLW 6SHFLDO 7HFKQLTXHV 7RSLF &RQFOXGLQJ 3URMHFW ([HUFLVH
At the conclusion of these exercises, you will be able to: Create a friends relationship and access the data of a class providing the friendship (a singleton) from the befriended class (Note that the friends concept is mainly used in bigger projects with more complex classes)

0RGHO VROXWLRQ &/B$*(1&< 6$3%&B63&6B0$,1B%

$GYDQFHG Use your singleton and the main program from the last exercise.
3-1 Add a static attribute FRQQHFWLRQBOLVW (with type TY_CONNECTIONS, that is an itab) to the singleton class. When instantiating the singleton, this internal table is to be automatically filled with the flight connections from the table SPFLI. B$*(1&<, which is to be provided friendship by the

3-2

Create a global class =&/B singleton. 4-1

Define and implement a constructor for =&/B B$*(1&<. In the constructor, the private attribute of the class QDPH is to be initialized. This class is to have a public method JHW FRQQHFWLRQ. 4-2-1 Import parameter: LPBFDUULG and LPBFRQQLG (types: S_CARR_ID and S_CONN:ID) 4-2-2 Export parameter: A structure with line type SPFLI Name the export parameter H[BFRQQHFWLRQ.

4-2

SAP AG

BC401

13-17

4-2-3 In the method, the internal table of the class providing the friendship is to be accessed in a single record access. (READ TABLE...) If the requested record does not exist, it is sufficient in this example to display an appropriate message using the WRITE statement. 5 Go back to the singleton class. Here, provide friendship to the class =&/B B$*(1&<.

In your main program, instantiate the class =&/B B$*(1&<. The singleton should have been instantiated in the last exercise. There, the internal table with the flight connections is already filled. 6-1-1 Execute the JHWBFRQQHFWLRQ method of =&/B connection that exists in the SPFLI table. (for example LH / 0400) B$*(1&< requesting a flight

IULHQG
 FOBVLQJOHWRQ  OFOBDJHQF\ JHWB FRQQHFWLRQ

FRQQHFWLRQB OLVW

SAP AG

BC401

13-18

6ROXWLRQV  RSWLRQDO
8QLW 7RSLF 6SHFLDO 7HFKQLTXHV 6LQJOHWRQ &ODVVHV

*&---------------------------------------------------------------------* *& Report *& *& *& *& *& *& *& SAPBC401_spcS_MAIN_A * *&---------------------------------------------------------------------* Work with singleton: The singleton class consists of a static pointer r_singleton pointing to the class itself; the class is final and has the attribute create private. in the class-constructor the class is instantiated via this reference. The static method get_singleton passes the reference to the outside world so that the singleton can be used!

*&---------------------------------------------------------------------* REPORT sapbc401_spcs_main_a.

'$7$ UBVLQJOH W\SH UHI WR FOBVLQJOHWRQ START-OF-SELECTION. *######################## UBVLQJOH FOBVLQJOHWRQ !JHWBVLQJOHWRQ 

SAP AG

BC401

13-19

6ROXWLRQV  RSWLRQDO
8QLW 7RSLF
*& Report *& *& *& *& *& *& *& *& The class cl_agency is a friend of the singleton; cl_agency works directly with the private data of the singleton the method get_connection reads single records out of an internal table of the singleton; so the singleton acts as a kind of data-container. cl_agency uses this data in the singleton directly. (performance aspects, package-concept...)

6SHFLDO 7HFKQLTXHV

&RQFOXGLQJ 3URMHFW ([HUFLVH


* (show package concept )

*&---------------------------------------------------------------------* SAPBC401_spcS_MAIN_B *&---------------------------------------------------------------------* Practice with singleton & friends

*&---------------------------------------------------------------------* REPORT sapbc401_spcs_main_b.

'$7$ UBVLQJOH W\SH UHI WR FOBVLQJOHWRQ UBDJHQF\ W\SH UHI WR FOBDJHQF\ UHF W\SH VSIOL

START-OF-SELECTION. *######################## UBVLQJOH FOBVLQJOHWRQ !JHWBVLQJOHWRQ 


$JHQF\

/+

FUHDWH REMHFW UBDJHQF\ H[SRUWLQJ LPBQDPH

UBDJHQF\!JHWBFRQQHFWLRQ H[SRUWLQJ LPBFDUULG LPBFRQQLG

LPSRUWLQJ H[BFRQQHFWLRQ ZULWH  UHFFDUULG UHFFRQQLG



UHF 

SAP AG

BC401

13-20

([FHSWLRQ +DQGOLQJ &RQWHQWV


l 3UHGHILQHG H[FHSWLRQV DQG H[FHSWLRQV \RX GHILQH \RXUVHOI l 5DLVLQJ KDQGOLQJ DQG SDVVLQJ DORQJ H[FHSWLRQV

SAP AG 2002

SAP AG

BC401

14-1

([FHSWLRQ +DQGOLQJ 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l &UHDWH \RXU RZQ H[FHSWLRQ FODVVHV l 5DLVH H[FHSWLRQV LQ D SURJUDP l +DQGOH H[FHSWLRQV l 3DVV H[FHSWLRQV DORQJ

SAP AG 2002

SAP AG

BC401

14-2

y d d WUx Q @ G P9 6 R6 B "89 E CDG B6 3789A 7I6 5 G F H6 C @ A @ 6 h T fb We g fe c a Y VT WX db X `3X WUS # !  )' &  0( ! !   %$ "   2   & 1  %   4 4  ! 3        g e d k ijhfPPH P  "U m f l n n p po Wd k " r o k P q n k o s o e k q k Ut u g o s o d v o e k q m o w x z  y r o  ~ H H D333|}{

fV ea b i w s VX T a b uvh te a e i p rX e T b qdg ae e i

SAP AG
SAP AG 2002

([FHSWLRQ +DQGOLQJ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

BC401

14-3

&ODVV%DVHG ([FHSWLRQV 2YHUYLHZ


([FHSWLRQ FODVV HLWKHU SUHGHILQHG RU GHILQHG E\ \RX

F[BH[FHSWLRQ

 5$,6( (;&(37,21 7<3( F[BH[FHSWLRQ >(;3257,1* DWWU  DWWU @  ([FHSWLRQ KDQGOLQJ 

SAP AG 2002

We use the term H[FHSWLRQ to refer to a situation that arises while an ABAP program is being executed, where there is no point in continuing to run the program in the normal way. Since SAP R/3 Basis Release 6.10, exceptions and exception handling is based on the exception classes concept. This concept includes the functions of the concepts that preceded it but also enhances (and thus replaces) them. Class-based exceptions are raised either using the ABAP statement RAISE EXCEPTION or by the ABAP runtime environment. For example, if the program tries to divide by zero the runtime environment raises the exception. You can, however, ascertain that this situation has arisen using a query in the ABAP program. From there, you can then raise an exception yourself. You can define exception classes yourself, but there is already a range of predefined exception classes in the system. In an exception situation, an exception is represented by an exception object that is, an instance of an exception class. The attributes of each exception object can contain information about the error situation. The use of class-based exceptions is not limited to object-oriented contexts. Class-based exceptions can be raised and handled in all ABAP processing blocks. In particular, the catchable runtime errors used until now can be handled as class-based exceptions. If a class-based exception occurs, the system interrupts the normal program flow and tries to navigate to a suitable handler. If it cannot find a handler, a runtime error occurs.

SAP AG

3(

([FHSWLRQ UDLVHG HLWKHU LQ $%$3 VWDWHPHQW RU E\ UXQWLPH V\VWHP

BC401

14-4

([FHSWLRQ &ODVVHV 7KH ,QKHULWDQFH +LHUDUFK\


F[BURRW
dHH t hj

F[BQRBFKHFN

F[BG\QDPLFBFKHFN

F[BV\BDULWKPHWLFBHUURU

F[BV\BDULWKPHWLFBRYHUIORZ
SAP AG 2002

All exception classes are derived from the one of the classes CX_NO_CHECK, CX_DYNAMIC_CHECK, or CX_STATIC_CHECK, themselves derived from the common superclass CX_ROOT. The way in which exception classes are assigned to one of these three paths in the hierarchy defines how the associated exceptions are passed along. (This will be discussed in more detail later in this unit.) All exception classes begin with the prefix CX_. In general, they are defined globally in the ABAP Workbench &ODVV %XLOGHU. However you can also define local exception classes. The root class CX_ROOT contains two predefined methods that are inherited by the other classes. The GET_SOURCE_POSITION method returns the program name, include name (if relevant), and line number in the source code where the exception occurred. The GET_TEXT method returns an exception text of a class in the form of a string. You can assign several texts to each class. You can then specify which text is to used when an exception is raised by passing an identifier to the IMPORTING parameter TEXTID of the instance constructor. All exception classes inherit the KERNEL_ERRID attribute from CX_ROOT. This attribute contains the name of the appropriate runtime error if the exception was raised by the runtime environment such as COMPUTE_INT_ZERODIVIDE if the program tries to divide by zero. If the exception is not listed, a runtime error occurs.

n n

SAP AG

B d tHBt "

F[BVWDWLFBFKHFN

F[BV\BPRYHBFDVWBHUURU

BC401

14-5

+DQGOLQJ ([FHSWLRQV
75<  &$7&+ F[B  &$7&+ F[B  &/($183  (1'75<  >,172 UBH[F@ F[B  >,172 UBH[F@

Code whose exceptions (if any) are to be handled

Handlers for the Handlers for the specified specified exception exception classes classes DQG their DQG their subclasses subclasses

Block for "tidying up" if there is no handler available in the TRYENDTRY structure

SAP AG 2002

Like all ABAP control structures, TRY-ENDTRY structures can be nested. Thus the TRY block, CATCH- blocks, and the CLEANUP block in particular can contain complete TRY-ENDTRY structures themselves. The TRY block contains the application code that is to handle the exceptions. If an exception occurs in the TRY block the system searches first for a CATCH statement (which will handle the exception) in the same TRY-ENDTRY structure and then step by step outwards in all the enclosing TRYENDTRY structures. If it finds one, it navigates to this handler. If it cannot find a handler but the TRY-ENDTRY structure is in a procedure, it then tries to pass the exception along to the calling program. (This will be discussed in more detail later.) A CATCH block contains the exception handler that is executed if a specified exception has occurred in the TRY block in the same TRY-ENDTRY structure. After the CATCH statement, you can specify as many exception classes as you wish. In this way, you define an exception handler for all these exception classes and their subclasses. After an exception occurs, the system searches through the listed exception handlers in the order specified. It then executes the first exception handler whose CATCH statement contains the relevant exception class or one of its superclasses. In some cases, the system cannot find a handler for an exception within a specific TRY-ENDTRY structure but the exception is handled in a surrounding TRY-ENDTRY structure or passed along to a calling program. If this occurs, a CLEANUP block is executed before leaving the TRY-ENDTRY structure.

SAP AG

BC401

14-6

([DPSOH +DQGOLQJ D 3UHGHILQHG ([FHSWLRQ


3$5$0(7(56 LQW LQW 7<3( L 7<3( L

'$7$ UHVXOW 7<3( L WH[W 7<3( VWULQJ UBH[F 7<3( 5() 72 F[BURRW 

,I DQ RYHUIORZ HUURU RFFXUV WKH UXQWLPH V\VWHP UDLVHV WKH F[BV\BDULWKPHWLFBRYHUIORZ H[FHSWLRQ

UHVXOW LQW LQW :5,7( UHVXOW &$7&+ F[BV\BDULWKPHWLFBRYHUIORZ ,172 UBH[F WH[W UBH[F!JHWBWH[W  0(66$*( WH[W 7<3(
,
 (1'75< 
Information

75<

L
"

2YHUIORZ LQ WKH RSHUDWLRQ


SAP AG 2002

In the above calculation, if the value range for data type i is exceeded, the runtime system raises the exception CX_SY_ARITHMETIC_OVERFLOW. This exception is handled in the implemented CATCH block. The object reference to the exception object is stored in the reference variable r_exc. Using r_exc and the functional method get_text, the handler accesses the exception text for this exception object and stores in the string variable text. To display exception texts as messages, the MESSAGE statement has been extended so that you can use any string: MESSAGE <string> TYPE <type>. As well as the message <string> that will be displayed, you must display the message type <type>, either as a literal or in a field. If the value range for data type i is not exceeded, no exception is raised and the TRY block is processed completely. The program then continues executing after the keyword ENDTRY. The class CX_SY_ARITHMETIC_OVERFLOW is a subclass of the classes CX_SY_ARITHMETIC_ERROR, CX_DYNAMIC_CHECK, and CX_ROOT. Thus the exception raised above can also be handled if you enter one of these classes after the CATCH statement. The keyword documentation for each keyword lists the exception classes whose exceptions may occur when the appropriate ABAP statement is executed.

n n

SAP AG

BC401

14-7

thHt t3BHt

}HH 3tH3(t 3tHttHt H tt(Ht Ht(HH(H}tjH3 HttH}Bt3ht( 3tUBB3($HH (BBht(Dt(H

tU BhU 3HhBtt(

thHt ( (Ht3hH ( BhHBtH( ( BHhBtH(D (thHBHt3 HH t(H (B(tt

SAP AG
SAP AG 2002

([DPSOH 8VLQJ <RXU 2ZQ ([FHSWLRQV

The relevant information is read from the database table VDSODQH. If the airplane type passed is not available in this table (that is, if sy-subrc <> 0), the values 100.000 and 10.000 respectively are assigned to the export parameters ex_weight and ex_tankcap. We will now change this behavior: If an airplane type is not entered in the table, an exception that we have defined should be raised and handled appropriately.

The above program source code shows the method get_technical_attributes of the class lcl_airplane, which was implemented in an earlier exercise in this training course. It receives an airplane type as an import parameter and returns its weight and tank capacity as export parameters.

BC401

$GDSWLQJ WKH SURJUDP ,I WKHUH LV QR WDEOH HQWU\ DYDLODEOH UDLVH DQG KDQGOH DQ H[FHSWLRQ \RX KDYH ZULWWHQ

14-8

&UHDWLQJ <RXU 2ZQ ([FHSWLRQ &ODVVHV


Workbench
Edit
Goto
Utilities
Environment
System Help

SAP AG 2002

n n n

Exceptions are represented by objects that are instances of exception classes. Defining an exception is thus synonymous with creating an exception class. Exception classes are generally defined globally. For special exceptions that will only occur within a single ABAP program however, you can also define local exception classes. Global exception classes are defined and managed in the Class Builder. When you create a new class, if you use the correct naming convention (prefix ZCX_) and choose the class type ([FHSWLRQ &ODVV, the system automatically displays the ([FHSWLRQ %XLOGHU instead of the Class Builder.

The ([FHSWLRQ %XLOGHU offers all the functions you need to create exception classes and generates specified components that cannot be changed. When you create an exception class, you must also specify which category of exception it will be - that is, whether it is derived from CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK.

SAP AG

XYQeXeW dG 5X U c SW Sb AYYW UVG @ @ a` XX P T PH E QP!RSQIG6FD

Object Name

Description

)LQDO &ODVV 2QO\ PRGHOHG

BC401

C)&  ( #

" ( & $ " " & $ 32'(10)( '%#  "     ! 
&ODVV

MIME Repository Object Navigator Repository Infosystem Tag Library Transport Organizer

SULYDW =&;B:521*B3/$1(7<3( SURWHFWHG &;B67$7,&6B&+(&. ,QKHULWV )URP DEVWUDFW &ODVV 7\SH 8VXDO $%$3 &ODVV ([FHSWLRQ &ODVV 3HUVLVWHQW &ODVV

B @A88564 97
14-9

PP TR PH G e%18Qr6EqD 5pWhgW !YiSeAf

0DLQWDLQLQJ <RXU 2ZQ $WWULEXWHV DQG ([FHSWLRQ 7H[WV


Class
Edit
Goto
Utilities
Environment
System
Help

Exception ID
CX_ROOT
SAP AG 2002

ZCX_WRONG_PLANETYPE This airplane type is unknown

n n

The methods are all inherited from CX_ROOT. You can also add your own methods. The instance constructor is generated automatically. You can also define your own attributes, whose contents specify the exception in more detail. The ([FHSWLRQ %XLOGHU ensures that the instance constructor has identically-named IMPORTING parameters for these attributes. The exception texts of global classes are defined on the 7H[WV tab of the Exception Builder. They can contain parameters. To do this, use the elementary attributes of the exception class by enclosing their name in ampersands (&) in the exception text. The exception texts of global exception classes are stored in their different translations in the 2SHQ 7H[W 5HSRVLWRU\ OTR). Note that several texts can be assigned to a single class. You assign a text to an exception using the TEXTID attribute, which contains the globally unique ID of the text object within an exception object at runtime. The method GET_TEXT then exports this text, replaces any text parameters with the contents of the relevant attributes as necessary, and returns the text as a character string. For each global class, the Exception Builder generates a default text whose name matches the class name. (The name of this default text cannot be changed.) You need to create names for other texts. For each text, the Exception Builder generates a static constant that contains the associated ID in the OTR. You can then specify which text is to used when an exception is raised by passing an identifier to the IMPORTING parameter TEXTID of the instance constructor. If you do not specify a text, the default text is used.

SAP AG

FV3)q1d% | | | IVr 3 qI IVr IVr IVr IVr } FdIIA 6VFF| 6 Vrr

BC401

r } V

'

<RXU RZQ DWWULEXWHV SURYLGH WKH XVHU ZLWK DGGLWLRQDO LQIRUPDWLRQ


V V V V V rIA } rIA } rIA } } rIA }AF IVr

} A}Fr6rrqq

x s { z x o v t s q o m k uiywuhr2pnljigedywtuyu3!ti)ywus h f s x x v s v s x x vt
} } 6 A'| IIq6

I!V }
([FHSWLRQ WH[W XVHG WR GHVFULEH H[FHSWLRQ VLWXDWLRQ LQ PRUH GHWDLO
Text
An exception occurred

} }} AFr ~|

rr3% rV |

r 3 ' 3 y 36 ~  d F w~| } }}

AIAAq }

14-10

5DLVLQJ DQG +DQGOLQJ ([FHSWLRQV <RX +DYH :ULWWHQ


(thttB3HtBt3B

([DPSOH LPBW\SH $

SAP AG 2002

If the airplane type passed to the method has not been stored in the table VDSODQH, the exception we defined previously, ]F[BZURQJBSODQHW\SH, is raised. In addition, a TRY-ENTRY control structure is implemented that is only processed if sy-subrc <> 0. The TRY block contains the application code that is to handle the exceptions. When the exception is raised, the IMPORTING parameter SOBW\SH of the instance constructor is filled. (This parameter is automatically generated by the Exception Builder.) Using this parameter, the program then assigns the value of the airplane type to the identically-named attribute. The exception that has been raised is handled in the CATCH block. The reference to the exception object is stored in the reference variable UBH[F, which was created as a local data object in the method (TYPE REF TO cx_root). Since the IMPORTING parameter TEXTID of the instance constructor was not filled when the exception was raised, the default text generated when the exception class was created is addressed using the functional method JHWBWH[W. The method GET_TEXT then exports this text, replaces the text parameter with the contents of the attribute SOBW\SH, and returns the text as a character string.

The returned text is stored in the local data object WH[W, which has the type VWULQJ The text is then displayed as an information (type ,) message.

SAP AG

BHH HgP H}tB H33(B3H 3H H3htHH(3hHHBhth 3Hh 3HhH( 3HhBt3( (B3 hH

tttHHt HHhHHhHtjH3h HH h3tH( B3Ht  Hh


Information

t ( 3BH( th HHt


BC401

}Ht

L
"

7KH DLUSODQH W\SH $ LV XQNQRZQ

14-11

3DVVLQJ ([FHSWLRQV $ORQJ


thHBHt HHHt H}th (BBthH Ht  t3B tH H (3HB 3
h(}B H(B HtBH HBhHtBh HBhHtB
Ht Ht tBtt3th HdtH3H3(B

SAP AG 2002

Exceptions that occur in procedures (methods, function modules, or subroutines) do not necessarily need to be handled there; they can be passed along to the calling program. The calling program can then handle the exception itself or also pass it along to its own caller, and so on. The highest levels to which an exception can be passed are processing blocks without local data areas - that is, event blocks or dialog modules. The exceptions passed along by the called procedures must be dealt with there, as must any exceptions raised within this processing block itself. Otherwise a runtime error occurs. To pass along an exception from a procedure, you generally use the RAISING addition when defining the procedure interface. In methods of local classes and subroutines, specify the RAISING addition directly when defining the procedure (METHODS meth ... RAISING cx_... cx_..., FORM form ... RAISING cx_... cx_...). After RAISING, list the exception classes whose objects are to passed along. In methods of global classes, the exception classes whose objects are to be propagated are entered in the exception table of the method in the Class Builder. Check the ([FHSWLRQ &ODVV field in this exception table. Similarly, exceptions raised by function modules are passed along by being entered in the Function Builder.

n n

SAP AG

BC401

t "V Y j B Hh eV YhtQ tuwj Hhh tthtt tH(t3e B H(

BB BB (3(t)) H

hHBH t 3hHt 3BB 3BBhB }t33 t Ht


Ht Ht3th H )) h H 3 BHt

14-12

tBBHH

tttD 3B3 3HhBt3( B3ht tBHH}h tttHtHt (Ht(HBH(ttjH t((H Ht3BH(t HBHB3h Bt3}UBB33(DHHH

thH HBhHtBh 3tH 3tBH}D h 3tHt HHBD h t(tt3hB3 tHH(HH h t3h3B hthBttBHH ht(tHHBtHH (Bh3Bt tBt

([DPSOH 3DVVLQJ ([FHSWLRQV $ORQJ

33HH t ( Bt(H3 Bt(Dt Bth BHH tHH3HBhth H 333HH'B(3td 3B BtBBHHddH( tHt B H(ttB3 tHH(tB3 3tUBB3(D tH t(3H Bt( B}(H3t 3H u 33}(BH

SAP AG
SAP AG 2002

If the exception is raised in the method JHWBWHFKQLFDOBDWWULEXWHV, the program continues by handling this exception. That is, the method JHWBWHFKQLFDOBDWWULEXWHV is terminated and the appropriate CATCH block is processed within the caller. Note in particular that the program no longer executes the WRITE statements entered in the TRY block DIWHU JHWBWHFKQLFDOBDWWULEXWHV is called.

To pass the exception along to the caller of the method, we enter it after the RAISING keyword.

As in the previous example, the exception we have defined (]F[BZURQJBSODQHW\SH) is raised if the airplane type passed to the method JHWBWHFKQLFDOBDWWULEXWHV is not stored in the table VDSODQH. Here, however, the exception is only raised in the method JHWBWHFKQLFDOBDWWULEXWHV, not handled there.

Now, the caller - that is, the method GLVSOD\BDWWULEXWHV - handles the exception. For this purpose, we have implemented a TRY-ENDTRY control structure in this method. The method JHWBWHFKQLFDOBDWWULEXWHV is now called in the TRY block of this control structure.

BC401

14-13

([FHSWLRQV 7KDW 0XVW %H 'HFODUHG


F[BURRW

F[BQRBFKHFN

F[BG\QDPLFBFKHFN

F[BVWDWLFBFKHFN

SAP AG 2002

Subclasses of CX_STATIC_CHECK: The relevant exception must either be handled, or passed along explicitly using the RAISING addition. The syntax check ensures that this is the case. At present, only exceptions you define yourself for error situations in the application code are subclasses of CX_STATIC_CHECK. Subclasses of CX_DYNAMIC_CHECK: The relevant exception does not have to be declared. If such an exception occurs at runtime, just as with subclasses of CX_STATIC_CHECK, it must either be handled or passed along explicitly using a RAISING addition. However, this is not checked in the syntax check. If such an exception occurs at runtime and is not either handled or passed along, a runtime error occurs. Most predefined exceptions with the prefix CX_SY_... for error situations in the runtime environment are subclasses of CX_DYNAMIC_CHECK. Subclasses of CX_NO_CHECK: These exceptions cannot be declared. These exceptions can be handled. Otherwise they are automatically passed along. The syntax check never finds an error here. All exceptions of the category CX_NO_CHECK that are not handled in the call hierarchy are automatically passed to the top level, If they are not caught there, they cause a runtime error. Some predefined exceptions with the prefix CX_SY_... for error situations in the runtime environment are subclasses of CX_NO_CHECK.

SAP AG

BC401

3V 3t 1  B" Bt Q 3( B HYh B (B dthH (e BH


14-14

B Bt e t H t3h B t Y(B (B W(HhHh (e Bt(( B


h(V

3

Q B Bt "( W "t3hY(h tV B "Hd (3(t B"Hu(huQ t B d (B dthh i HHdB


h(BV

3H

([FHSWLRQ +DQGOLQJ 8QLW 6XPPDU\

l &UHDWH \RXU RZQ H[FHSWLRQ FODVVHV l 5DLVH H[FHSWLRQV LQ D SURJUDP l +DQGOH H[FHSWLRQV l 3DVV H[FHSWLRQV DORQJ

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

14-15

([HUFLVH 
8QLW ([FHSWLRQ +DQGOLQJ 7RSLF 'HILQLQJ UDLVLQJ SURSDJDWLQJ DQG FDWFKLQJ FODVVEDVHG H[FHSWLRQV
Define exception classes Raise class-based exceptions Pass exceptions along Catch class-based exceptions
Improve your program so that, when the airplane attributes are displayed, an error text is displayed instead of the default values if the airplane type is invalid. Implement this using the class-based exceptions concept. 3URJUDP =%&B B5$,6(B75<

At the conclusion of these exercises, you will be able to:

0RGHO VROXWLRQ

7HPSODWH

6$3%&B(9(6B0$,1B%

LV \RXU WZRGLJLW JURXS QXPEHU

6$3%&B(;&6B5$,6(B75<

1-1

Copy your solution to the last exercise from the unit (YHQWV, or the corresponding model solution 6$3%&B(9(6B0$,1B%, with all their includes. Give them the new names =%&B B5$,6(B75<, =%&B B5$,6(B75<B&/, and =%&B B5$,6(B75<B&/. Define a JOREDO exception class. (We suggest the name =&;B B,19$/,'B3/$1(7<3(.) Choose the appropriate superclass so that the system performs a syntax check to ensure that, after the relevant exception is raised, it is either handled or passed explicitly along using a RAISING addition. Add an attribute for the airplane type (suggested name: 3/$1(7<3() and assign the type 6B3/$1(7<3( to it.
Create a default error message that can be enhanced dynamically to include the airplane type.

1-2

1-3

Raise the exception in the JHWBWHFKQLFDOBDWWULEXWHV method of your local class OFOBDLUSODQH. Add a 5$,6,1* addition to the definition part of the method, so that the exception can be passed along.
BC401 14-16

SAP AG

1-4

Catch the exception in the GLVSOD\BDWWULEXWHV method of your local class OFOBDLUSODQH. To do this, you will need a local reference to the exception instance (suggested name: UBH[FHSWLRQ). Assign a type to this reference using the superclass &;B5227. Read the error text from the exception instance using the JHWBWH[W method. To do this you will need an auxiliary variable of the type VWULQJ (suggested name H[FBWH[W).

SAP AG

BC401

14-17

6ROXWLRQ 
8QLW ([FHSWLRQ +DQGOLQJ 7RSLF 'HILQLQJ SURSDJDWLQJ DQG UDLVLQJ HYHQWV

0RGHO VROXWLRQ ,QFOXGH %&B(;&6B5$,6(B75<B&/ *&---------------------------------------------------------------------* *& ... *------------------------------------------------------------------* * CLASS lcl_airplane DEFINITION * *------------------------------------------------------------------* CLASS lcl_airplane DEFINITION. PUBLIC SECTION. "--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30. METHODS: constructor IMPORTING im_name display_attributes. CLASS-METHODS: display_n_o_airplanes. EVENTS: airplane_created. PRIVATE SECTION. "---------------------------------------------METHODS: get_technical_attributes IMPORTING im_type EXPORTING ex_weight ex_tankcap RAISING DATA: name TYPE saplane-planetype TYPE s_plan_wei TYPE s_capacity TYPE string im_planetype TYPE saplane-planetype, Include BC401_EXCS_RAISE_TRY_CL2 * *&---------------------------------------------------------------------*

cx_bc401_invalid_planetype.

TYPE string,

planetype TYPE saplane-planetype.

SAP AG

BC401

14-18

CLASS-DATA: n_o_airplanes TYPE i. ENDCLASS. "lcl_airplane DEFINITION

*------------------------------------------------------------------* * CLASS lcl_airplane IMPLEMENTATION * *------------------------------------------------------------------* CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. ... ENDMETHOD. METHOD display_attributes. DATA: weight TYPE saplane-weight, cap TYPE saplane-tankcap, r_exception TYPE REF TO cx_root, exc_text TYPE string. WRITE: / icon_ws_plane AS ICON, / Name des Flugzeugs(001), AT pos_1 name, / Type of airplane: (002), AT pos_1 planetype. * handle exception in case of invalid planetype: 75< get_technical_attributes( EXPORTING im_type = planetype IMPORTING ex_weight = weight ex_tankcap = cap ). WRITE: / Gewicht:(003), weight, Tankkap:(004), cap. &$7&+ F[BEFBLQYDOLGBSODQHW\SH ,172 UBH[FHSWLRQ H[FBWH[W :5,7(  H[FBWH[W &2/25 &2/B1(*$7,9( "display_attributes UBH[FHSWLRQ!JHWBWH[W  "constructor

(1'75<

ENDMETHOD. METHOD display_n_o_airplanes. ... ENDMETHOD.

"display_n_o_airplanes

METHOD get_technical_attributes. SELECT SINGLE weight tankcap FROM saplane INTO (ex_weight, ex_tankcap)

SAP AG

BC401

14-19

WHERE planetype = im_type. IF sy-subrc <> 0. 5$,6( (;&(37,21 7<3( F[BEFBLQYDOLGBSODQHW\SH (;3257,1* SODQHW\SH ENDIF. ENDMETHOD. ENDCLASS. "get_technical_attributes "lcl_airplane IMPLEMENTATION LPBW\SH

SAP AG

BC401

14-20

'\QDPLF 3URJUDPPLQJ &RQWHQWV


l '\QDPLF DWWULEXWHV l )LHOG V\PEROV l 'DWD UHIHUHQFH YDULDEOHV l 5XQWLPH 7\SH ,GHQWLILFDWLRQ 577,

SAP AG 2002

SAP AG

BC401

15-1

'\QDPLF 3URJUDPPLQJ 8QLW 2EMHFWLYHV

$W WKH FRQFOXVLRQ RI WKLV XQLW \RX ZLOO EH DEOH WR l 8VH G\QDPLF DWWULEXWHV LQ VXLWDEOH $%$3 VWDWHPHQWV l 8VH GDWD REMHFWV JHQHUDWHG DW UXQWLPH l $VFHUWDLQ WKH DWWULEXWHV RI GDWD REMHFWV DW UXQWLPH

SAP AG 2002

SAP AG

BC401

15-2

y d d Vx R @ G P9 6 S6 BQ "89 E CDG B6 3789A 7I6 5 G F H6 C @ A @ 6 h U fb e g fe c a Y WU X db X `3X VT %# !  !   $ "  )' &   & 1  %  0(!    4 4  ! 23         h g f d j iPePt D m f l k n n p po Qd k j " r o j s P q n j o t oe j q j Vu v g o t o d w d o e j q m o y { z r o x | n m o r d j u ~ H}} H ~

fW ea b i w s WX U a b uvh te a e i p rQX e U b qdg e a e i

SAP AG

'\QDPLF 3URJUDPPLQJ ,QWHJUDWLRQ LQ &RXUVH &RQWHQW

SAP AG 2002

BC401

15-3

'\QDPLF 3URJUDPPLQJ 

)LHOG 6\PEROV LQ 'HWDLO )LHOG 6\PEROV LQ 'HWDLO


'\QDPLF &DOOV '\QDPLF &DOOV
'DWD 5HIHUHQFHV 'DWD 5HIHUHQFHV
$VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\ $VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\

SAP AG 2002

SAP AG

BC401

15-4

)LHOG 6\PEROV
),(/'6<0%2/6 IV! 7<3(_/,.(  _7<3( $1<` $66,*1  GDWDREMHFW 72 IV! 81$66,*1 IV!  IV! ,6 $66,*1(' 
"B0
BC401

*HQHULF RU FRPSOHWH W\SH VSHFLILFDWLRQ


s
s
s
s
15-5

SAP AG 2002

n n

You declare field symbols using the ),(/'6<0%2/6 IV! statement. Note that the parentheses (<>) DUH part of the syntax. Field symbols allow you to access an assigned data object - that is, all the accesses that you make to the field symbol are made to the data object assigned to it. Field symbols are similar to dereferenced pointers. Thus, you can only access the content of the data object to which the field symbol points. (That is, field symbols use value semantics).

You use the ASSIGN statement to assign a data object to the field symbol IV!. If the field symbol is generically typed (TYPE ANY), it adopts the type of the data object. By specifying a type for the field symbol, you can ensure that only compatible objects are assigned to it. Example: DATA: date TYPE d VALUE 19991231, time TYPE t. FIELD-SYMBOLS: <fs_date> TYPE d, <fs_time> TYPE t. ASSIGN: date TO <fs_date>, time TO <fs_time>. <fs_time> = <fs_date>. returns a syntax error. Conversely, using the following construction would deliver a runtime error:FIELD-SYMBOLS: <fs_date> TYPE ANY, <fs_time> TYPE ANY.

n n

Use the expression <fs> IS ASSIGNED to find out whether the field symbol <fs> is assigned to a field. The statement UNASSIGN <fs>. sets the field symbol <fs> so that it points to nothing. The logical expression <fs> IS ASSIGNED is then false.

SAP AG

"B0

81$66,*1 IVBLQW! ,) IVBLQW! ,6 $66,*1(' :5,7(  LQW IVBLQW! (/6( :5,7( 


ILHOGV\PERO QRW DVVLJQHG
IQD  (1',)

"B0

IVBLQW!  :5,7(  LQW IVBLQW!

"B0

$66,*1 LQW 72 IVBLQW! :5,7(  LQW IVBLQW!

'$7$ LQW 7<3( L 9$/8(  ),(/'6<0%2/6 IVBLQW! 7<3( L

7KH &$67,1* $GGLWLRQ


$66,*1  GDWDREMHFW 72 IV! &$67,1* >7<3( W\SH_@ 

7<3(6 %(*,1 2) VWBGDWH \HDU  7<3( Q PRQWK  7<3( Q GD\  7<3( Q (1' 2) VWBGDWH RSWLRQ  LPSOLFLW ),(/'6<0%2/6 IV! 7<3( VWBGDWH $66,*1 V\GDWXP 72 IV! &$67,1*

,QKDOW GHV ]XJHZLHVHQHQ 7KH FRQWHQW RI WKH DVVLJQHG 'DWHQREMHNWV ZLUG VR GDWD REMHFW LV LQWHUSUHWHG DV LQWHUSUHWLHUW DOV RE HU GHQ LI LW KDG WKH LPSOLFLW RU LPSOL]LW E]Z H[SOL]LW H[SOLFLW W\SH VSHFLILHG DQJHJHEHQ 7\S KlWWH

SAP AG 2002

If you use the CASTING addition when you assign a data object to a field symbol that has a different type, you can remove the restrictions of having to use the data objects original type. The access is then interpreted as though the data object had the data type of the field symbol. If you use the CASTING TYPE <type> addition when you assign a data object to a field symbol that has a different type, you can access the data object using the field symbol as if the object had the type <type>. In the above example, note that the system field sy-datum is an elementary character-type component of length . You can also use type casting dynamically when you assign a data object to a field symbol. For example: PARAMETERS tabname TYPE dd02l-tabname. DATA: dummy TYPE i, line(65535) TYPE c. FIELD-SYMBOLS <fs_wa> TYPE ANY. ASSIGN line TO <fs_wa> CASTING TYPE (tabname). You can now access line through <fs_wa> as if this elementary data object had the same type as the line type of the transparent table passed using tabname. (You define the dummy data object only to ensure that line has the correct alignment).

n n

SAP AG

BC401

RSWLRQ  H[SOLFLW ),(/'6<0%2/6 IV! 7<3( $1< $66,*1 V\GDWXP 72 IV! &$67,1* 7<3( VWBGDWH 

"s
15-6

V$PVD

"s

DFFHVV DIWHU FDVWLQJ :5,7(  IV!\HDU IV!PRQWK IV!GD\

"s

'\QDPLF 3URJUDPPLQJ 

)LHOG 6\PEROV LQ 'HWDLO )LHOG 6\PEROV LQ 'HWDLO


'\QDPLF &DOOV '\QDPLF &DOOV
'DWD 5HIHUHQFHV 'DWD 5HIHUHQFHV
$VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\ $VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\

SAP AG 2002

SAP AG

BC401

15-7

8VLQJ '\QDPLF $WWULEXWHV LQ 6WDWHPHQWV


l 5HSODFLQJ D OLWHUDO ZLWK D YDULDEOH

&$// 75$16$&7,21
7B&2'(
 G\QDPLF GDWDBREMHFW
7B&2'(
 &$// 75$16$&7,21 GDWDBREMHFW

68%0,7 UHSRUWBQDPH G\QDPLF GDWDBREMHFW


5(3257B1$0(
 68%0,7 GDWDBREMHFW 
l 5HSODFLQJ D OLVW ZLWK DQ LQWHUQDO WDEOH

6(7 3)67$786
67$786B1$0(
(;&/8',1*
)&

)&
  G\QDPLF  ILOO LQWHUQDOBWDEOH ZLWK IXQFWLRQ FRGHV  6(7 3)67$786
67$786B1$0(
(;&/8',1* LQWHUQDOBWDEOH 
SAP AG 2002

n n

In many ABAP statements, you can pass attributes dynamically. The syntax for doing this differs, depending on the statement you use: If you pass the attribute as a literal in the static form of the statement, you can replace the literal with a variable. If you pass the attribute as an identifier in the static form of the statement, you can replace the identifier with a variable in parentheses. Make sure there are no spaces between either parenthesis and the variable. If the attribute is a list, you can replace it with an internal table. Note: you PXVW use upper case when filling data objects with literals. For more information on which of these three options you can use with a specific ABAP statement if any - refer to the ABAP documentation for that statement. In Open SQL statements, you can also specify the logical conditions of the WHERE clause dynamically at runtime, by entering the ABAP source code in a variable. In the FROM clause of the SELECT statement, you can specify either individual table names or a dynamic JOIN expression using a variable. Similarly, you can program the GROUP-BY and HAVING clauses in SELECT statements. For more information, refer to the keyword documentation.

n n n

SAP AG

BC401

5HSODFLQJ DQ LGHQWLILHU ZLWK D YDULDEOH LQ SDUHQWKHVHV

VSDFHV 

15-8

&DOOLQJ 0HWKRGV '\QDPLFDOO\


7<3(322/ DEDS 7<3(6 DEDSBSDUPELQGBWDE  7<3(6 DEDSBH[FSELQGBWDE  
,QWHUQDO WDEOH W\SHV
(" P ss
0 (vD s(

7<3(322/6 DEDS
t

SAP AG 2002

You can call instance and static methods dynamically using parentheses in the syntax, as is normal in ABAP. Use the PARAMETER-TABLE and EXCEPTION-TABLE additions of the CALL METHOD statement to pass the actual parameters dynamically. The parameter table must have the attributes of the ABAP_PARMBIND_TAB table type. The table has three columns: NAME (for the name of the formal parameter); KIND for the way the parameter is passed (exporting, importing, changing, or receiving); VALUE with the type REF TO data (for the value of the actual parameter). The way the parameter is passed is specified for each formal parameter in the declaration of the called method. Thus, the content of the KIND column can be initial. For the value of the actual parameter, the VALUE reference of the line in the table must point to the data object containing that value. You can use the GET REFERENCE statement to achieve this. The exception table must have the attributes of the ABAP_EXCPBIND_TAB table type. This table has two columns: NAME for the name of the exception; VALUE for the value to be assigned to sysubrc. VALUE must be of type i.

n n n

SAP AG

BC401

st

)LOO SWDE DQG HWDE  &$// 0(7+2' UHI! GDWDBREMHFW 3$5$0(7(57$%/( SWDE (;&(37,217$%/( HWDE

st

'$7$ SWDE 7<3( DEDSBSDUPELQGBWDE HWDE 7<3( DEDSBH[FSELQGBWDE  GDWDBREMHFW


0(7+2'B1$0(


 ( 
15-9

s( ( 3 (s

'\QDPLF 3URJUDPPLQJ 

)LHOG 6\PEROV LQ 'HWDLO )LHOG 6\PEROV LQ 'HWDLO


'\QDPLF &DOOV '\QDPLF &DOOV
'DWD 5HIHUHQFHV 'DWD 5HIHUHQFHV
$VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\ $VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\

SAP AG 2002

SAP AG

BC401

15-10

'DWD 5HIHUHQFH 9DULDEOHV


7<3(6 UHIW\SH ^ 7<3( 5() 72 W\SHBQDPH _ /,.( 5() 72 GRBQDPH _ 7<3( 5() 72 GDWD ` 7<3( 5() 72 W\SHBQDPH _ /,.( 5() 72 GRBQDPH _ 7<3( 5() 72 GDWD `
$Q\ FRPSOHWHO\ VSHFLILHG W\SH

'$7$

UHI

*(7 5()(5(1&( 2) GDWDREMHFW ,172 UHI

*HQHULF W\SH DVVLJQPHQW

'$7$

*HW D UHIHUHQFH WR D GDWD REMHFW


SAP AG 2002

Data reference variables contain data references - that is, pointers to data objects. You use the TYPES reftype TYPE REF TO type_name statement to define a reference type to a data object, where type_name is any completely specified type. You can also use the generic variant TYPE REF TO data here. You define the data reference variable itself using the DATA statement. This reference variable is a data object that can contain any data object (TYPE REF TO data) or a data object of the specified type. You work with data references using references. That is, when you access a data reference variable the data reference itself is accessed, so that changes are made to the addresses. Data references are handled in ABAP like any data object with an elementary data type. This means that a reference variable can be defined not only as a single field, but also as the smallest indivisible unit in a complex data object, such as a structure or an internal table. After it has been defined, the data reference variable is initial - that is, it contains an empty pointer. For a data reference variable to contain a reference that points to a data object, you must use this variable to get a reference to a data object that has already been declared (GET REFERENCE OF dataobject INTO ref). You can also assign an existing data reference from another data reference variable or generate a data object dynamically using it. (This will be discussed in more detail later.)

n n

SAP AG

BC401

*(7 5()(5(1&( 2) GR ,172 UHI

P
15-11

t
t

UHI 7<3( 5() 72 L GR 7<3( L 9$/8( 

A@

S R U S TVTR

 tt'(3B(D % 8 5 3 0 9(B7&642 t13B%)( " % " H(tB #!3

D PH D I G F E
H

B B (B 3 ' &(#! " % $ " !tB   tt 3t3tt 

fytx4uvVtush2(4fTdb`PW p g w eY a q r q p i g a eY c aYX

Ht t (BtB3 3 BH(Bt t (Bt (B BHBt H(t(tB(B(Bt ( (Btt(HBH


tH4Ht  (Bt3 B

% 3HB%1

tCt  3 0 t3B%1 H 3 B BV H0t% 3    3  3134

(

tH $ B3 HB H0% tH  3   3  (1B4H

SAP AG
SAP AG 2002

In this example, the get_make method of the lcl_vehicle class is called dynamically. It has only one export parameter and no exceptions.

First we define a parameter table ptab with the global table type abap_parmbind_tab from the type group abap. The associated work area is called wa_ptab

We fill the work area wa_ptab with the associated values. We assign the name of the export parameter EX_MAKE to the component name. The component value contains the reference to the corresponding actual parameter gd_make. We also define the data reference variable ref (TYPE REF TO data) and fill the corresponding reference using GET REFERENCE. The kind component of the work area need not be filled.

After the method call, the actual parameter gd_make contains the passed value.

We then insert this work area in the parameter table ptab using the INSERT statement. Finally we assign the name of the calling method to the data object meth. We then call the method using the syntax already shown.

([DPSOH 8VLQJ 'DWD 5HIHUHQFH 9DULDEOHV LQ '\QDPLF 0HWKRG &DOOV

BC401

15-12

*HQHUDWLQJ 'DWD 2EMHFWV DW 5XQWLPH

'$7$

UHI ^ 7<3( 5() 72 W\SHBQDPH _ /,.( 5() 72 GRBQDPH ` &5($7( '$7$ UHI <RX FDQ DOVR DVVLJQ D W\SH G\QDPLFDOO\ '$7$ UHI 7<3( 5() 72 GDWD
&5($7( '$7$ UHI 7<3( ^ W\SHBQDPH _ LWDEBW\SHBGHI `

&5($7( '$7$ UHIBLWDE 7<3( 67$1'$5' 7$%/( 2) SDBWDE :,7+ '()$8/7 .(<
6WDQGDUG WDEOH ZLWK VWDQGDUG NH\ DQG OLQH W\SH DSSURSULDWH IRU WKH FRQWHQW RI WKH GDWD REMHFW SDBWDE
SAP AG 2002

All data objects declared in the declaration part of a program using the appropriate statement (such as DATA) are generated statically and can be addressed from when the first event block is executed. However, you can also use data reference variables to generate any data object you want dynamically while the program is executing. You can use either of the above variants to do this. Both of these variants generate a data object in the internal session of the current ABAP program. The data reference in the data reference variable ref points to this object after the statement has been executed. This dynamically generated data object does not have its own name; it can only be addressed using the data reference variable. If you want to access the content of the data object, you need to dereference the data reference first. In the second variant, you specify the data type of the data object you want to generate after the TYPE addition of the CREATE-DATA statement. In this case, you can specify the data type dynamically: CREATE DATA ref TYPE (dataobject). You cannot do this in other ABAP statements. dataobject is the name of a field containing the name of the relevant data type. When you generate internal tables using the second variant, you must specify the table kind statically. You can, however, specify the line type either statically or dynamically. You can also specify the key components either statically or dynamically (as the contents of a table containing the component names). You can also specify the initial number of table lines statically or dynamically (as the contents of a variable).

SAP AG

BC401

H B
15-13

3$5$0(7(56 SDBWDE 7<3( GGWDEQDPH '$7$ UHIBLWDE 7<3( 5() 72 GDWD

'HUHIHUHQFLQJ 'DWD 5HIHUHQFHV


$66,*1 UHI! 72 IV! >&$67,1*  @
<RX FDQ WKHQ DGGUHVV WKH FRQWHQWV RI WKH LQWHUQDO WDEOH XVLQJ WKH ILHOG V\PERO

),(/'6<0%2/6 IVBLWDE! 7<3( $1< 7$%/( &5($7( '$7$ UHIBLWDE 7<3( 67$1'$5' 7$%/( 2) SDBWDE :,7+ '()$8/7 .(< $66,*1 UHIBLWDE! 72 IVBLWDE! 6(/(&7 )520 SDBWDE ,172 7$%/( IVBLWDE!

SAP AG 2002

To access the contents of the data object, you need to dereference the data reference first. To access data objects generated with data reference variables with generic types (TYPE REF TO data), you can use field symbols: ASSIGN ref->* TO <fs>. This statement assigns the relevant data object (the one to which the data reference in the reference variable ref points) to the field symbol <fs>. If the data object i s assigned successfully, sy-subrc is set to 0. If the field symbol is fully generically typed, it adopts the type of the data object. If the field symbol is partially or completely typed, the system checks the compatibility of the data types. You can also cast to the assigned data object. If the data reference in ref is initial or invalid, it cannot be dereferenced. In that case the field symbol remains unchanged and sy-subrc is set to 4. If the data reference variable ref is completely typed (that is, not generically), you can use the prefix ref! to access the contents of the data object to which ref is pointing. You can write this expression in any operand position. If the data reference is typed, you can also address the components of the referenced data object directly and use them in any operand position. DATA ref TYPE REF TO sflight. CREATE DATA ref. ref->fldate = ref->fldate + 5. WRITE: / ref->seatsmax.

n n

SAP AG

BC401

H B
15-14

t 0

3$5$0(7(56 SDBWDE '$7$ UHIBLWDE

7<3( GGWDEQDPH 7<3( 5() 72 GDWD

([DPSOH *HQHUDWLQJ 'DWD 2EMHFWV DW 5XQWLPH


3$5$0(7(56 SDBGEWDE 7<3( GGOWDEQDPH '()$8/7
6)/,*+7
 '$7$ GBUHI 7<3( 5() 72 GDWD ),(/'6<0%2/6 IVBZD! 7<3( $1< IVBFRPS! 7<3( $1< 67$572)6(/(&7,21 &5($7( '$7$ GBUHI 7<3( SDBGEWDE  $66,*1 GBUHI! 72 IVBZD! 6(/(&7 )520 SDBGEWDE ,172 IVBZD! '2 $66,*1 &20321(17 V\LQGH[ 2) 6758&785( IVBZD! 72 IVBFRPS! ,) V\VXEUF 1(  6.,3 (;,7 (1',) :5,7( IVBFRPS! (1''2 (1'6(/(&7
SAP AG 2002

This example displays the content of a transparent table. You can make the FROM clause of the SELECT statement dynamic. For the INTO clause, you will need a data object that has a line type compatible with that of the table being displayed. Since the name - and thus the line type of the table is not known until runtime, you should not create the data object until then. Unlike conventional data objects, you can specify the type of a data object FUHDWHG DW UXQWLPH dynamically. The TYPE addition of the CREATE DATA statement contains the name of the table, so that the system creates the appropriate structure. The statement ASSIGN d_ref->* TO <fs_wa> assigns the data object to the field symbol. The data type of the table is inherited by the field symbol, so type casting is no longer necessary. You can now write each data record from the SELECT statement into the compatibly-typed data object using the field symbol <fs_wa>. If you knew the component names, you could display the fields directly using WRITE <fs_wa>... . However, you will not normally know the names of the components, nor how many of them there are. For this reason, you must display the components using the ASSIGN-COMPONENT variant: The components of the structure IVBZD are assigned one-by-one to the field symbol IVBFRPS! and then displayed. When the loop runs out of components, the program reads the next data record.

n n n

SAP AG

BC401

15-15

'\QDPLF 3URJUDPPLQJ 

)LHOG 6\PEROV LQ 'HWDLO )LHOG 6\PEROV LQ 'HWDLO


'\QDPLF &DOOV '\QDPLF &DOOV
'DWD 5HIHUHQFHV 'DWD 5HIHUHQFHV

$VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\ $VFHUWDLQLQJ 'DWD $WWULEXWHV '\QDPLFDOO\

SAP AG 2002

SAP AG

BC401

15-16

$VFHUWDLQLQJ ,QWHUQDO 7DEOH $WWULEXWHV


'$7$ QRBRIBOLQHV 7<3( L LQLWLDOBOLQHV 7<3( L WDEOHBNLQG 7<3( F '(6&5,%( 7$%/( LWDE /,1(6 QRBRIBOLQHV 2&&856 LQLWLDOBOLQHV .,1' WDEOHBNLQG RWKHU YDULDQW OLQHV LWDE  QRBRIBOLQHV
&RQVWDQWV XVHG WR HYDOXDWH WKH UHWXUQ YDOXH

1XPEHU RI OLQHV LQ WKH LQWHUQDO WDEOH


1XPEHU RI OLQHV UHVHUYHG ZKHQ WDEOH ZDV ILUVW FUHDWHG
7DEOH NLQG

WDEOHBNLQG

7<3(322/ V\GHV V\GHVBNLQGXQGHILQHG V\GHVBNLQGVWDQGDUG V\GHVBNLQGVRUWHG V\GHVBNLQGKDVKHG

SAP AG 2002

The DESCRIBE TABLE statement allows you to obtain the following information about an internal table:
l l l

The number of lines: After LINES, you must enter a type L variable (here no_of_lines)

The number of table lines initially reserved:After OCCURS, you must enter a type L variable (here: initial_lines)

The table kind: After KIND, you must enter a type F variable. The runtime system fills the variable with a constant defined in the type group SYDES - SYDES_KIND-UNDEFINED, SYDES_KINDSTANDARD, SYDES_KIND-SORTED, or SYDES_KIND-HASHED.

n n

Since SAP R/3 Basis Release 6.10, there is also a built-in function, lines. 1RWH You can use the information about the number of lines in an internal table to query at runtime whether an internal table has any entries in it at all. Alternatively, use the IS INITIAL query. The generically-typed interface parameters of subroutines, function modules, and methods can use the information on the table kind.

SAP AG

BC401

15-17

'\QDPLF 7\SH 'HVFULSWLRQV IRU 577, &ODVVHV


+LHUDUFK\ RI GHVFULSWLRQ FODVVHV

5XOHV

3DVV WKH GDWD REMHFW WR WKH VWDWLF PHWKRG '(6&5,%(B%<B'$7$ RI WKH FODVV &/B$%$3B7<3('(6&5

l l

(YDOXDWH WKH SXEOLF DWWULEXWHV RI WKLV REMHFW

*HW WKH UHIHUHQFH WR WKH DSSURSULDWH GHVFULSWLRQ REMHFW

SAP AG 2002

Since the introduction of ABAP Objects, there is now a system called the RTTI concept (Run Time Type Information) that you can use to find out type attributes at runtime. It is based on system classes. The concept includes all ABAP types, and so covers all of the functions of the statements DESCRIBE FIELD and DESCRIBE TABLE. There is a description class for each type with special attributes for special type attributes. The class hierarchy of the description classes corresponds to the hierarchy of the types in the ABAP type system. In addition, the description classes for complex types, references, classes, and interfaces have special methods used to specify references to sub-types. Using these methods, you can navigate through a compound type to all its sub-types. To obtain a reference to a description object of a type, you must use the static methods of the class CL_ABAP_TYPEDESCR or the navigation methods of the special description class. The description objects are then created from one of the subclasses. At runtime, exactly one description object exists for each type. The attributes of the description object contain information on the attributes of the type.

SAP AG

BC401

l u q

hj

t o kl

y n uq q dtT wBl l x l 4j m k kqoojn m z s r i v p n l j j k4&g j i h

D Af C VG 4 VG 1 P  A9 PC1 U U D f G G

AD D U U PC1 U U D H D C PG  VG 4 VG G1 G G

H E  G C PG 1 P DD D U U F F S S V4 U U D E G G

D C P A G G U U V4 U U D G G G C PG 1

D F F D D U U D D V4 U U G C PG 1 D G G

P D A A  G C PG 1 H T eC9dd 4V4 U U D e G G

P D D H e e D D V4 U U C G C PG 1 G G

P A  G  P 4DT D D A S S GCP1 U U D G G

 G G CP1 U U D G  G G G  D G G
15-18

D C   VG  PG  CD D CP1 U U D G G

'HFODULQJ 7\SHV '\QDPLFDOO\ ([DPSOH


 '$7$ GHVFUBUHI 7<3( 5() 72 FOBDEDSBVWUXFWGHVFU ZDBFRPS 7<3( DEDSBFRPSGHVFU  67$572)6(/(&7,21  JHW UHIHUHQFH WR W\SH GHVFULSLRQ REMHFW E\ ZLGHQLQJ FDVW GHVFUBUHI " FOBDEDSBW\SHGHVFU !GHVFULEHBE\BGDWD IVBZD!   7232)3$*( /223 $7 GHVFUBUHI!FRPSRQHQWV ,172 ZDBFRPS :5,7( ZDBFRPSQDPH (1'/223

SAP AG 2002

n n

We can now enhance the example of dynamic type declarations so that the system also displays the column names of the transparent table in the list. Since we need the attributes of a structure, we first define a reference to the appropriate description class. Instances of this class possess a COMPONENTS attribute, which you use to describe the individual components of the relevant structure. This attribute is an internal table. Therefore you also need to define a work area with a compatible line type. The (functional) method call returns the reference to the description instance of the structure. (The system creates the structure dynamically, which is why it is accessed through a field symbol). Only the abstract class CL_ABAP_TYPEDESCR contains the method DESCRIBE_BY_DATA. Its RETURNING parameter is typed as a reference to this superclass. However, since the actual parameter descr_ref has the type of the subclass CL_ABAP_STRUCTDESCR, we need to assign the object using a (widening) cast. You can then access the attributes of the description instance in any form. In this example, the component names are displayed as the column headers. (We have omitted the formatting options for the sake of clarity.) For more information and syntax examples, refer to the online documentation, either under the keyword 577, or the class CL_ABAP_TYPEDESCR.

SAP AG

BC401

15-19

'\QDPLF 3URJUDPPLQJ 8QLW 6XPPDU\

l 8VH G\QDPLF DWWULEXWHV LQ VXLWDEOH $%$3 VWDWHPHQWV l 8VH GDWD REMHFWV JHQHUDWHG DW UXQWLPH l $VFHUWDLQ WKH DWWULEXWHV RI GDWD REMHFWV DW UXQWLPH

<RX DUH QRZ DEOH WR

SAP AG 2002

SAP AG

BC401

15-20

2SWLRQDO
8QLW '\QDPLF 3URJUDPPLQJ 7RSLF &DVWLQJ 7\SHV

At the conclusion of these exercises, you will be able to: Use field symbols to cast types Perform calculations on dates
Change your program so that the default value for the key date is calculated differently. It should be the first day of the following month.

0RGHO VROXWLRQ

7HPSODWH

3URJUDP

6$3%&B7$%6B352&(66B'$7$ 6$3%&B'<16B&$67,1*

=%&B

B&$67,1*

1-1

Copy your solution to the last exercise from the unit 8VLQJ ,QWHUQDO 7DEOHV =%&B B352&(66B'$7$ or the corresponding model solution 6$3%&B7$%6B352&(66B'$7$ and give it the new name =%&B B&$67,1*. Define a structure type (we suggest the name: VWBGDWH) with three components: Year, month, and day. Assign an appropriate type to each component. Define a field symbol based on this type (suggested name: IVBGDWH!). In the /2$'2)352*5$0 event block: Copy todays date to the input parameter SDBGDWH. Assign the input parameter to the field symbol. Now fill the input parameter with the first day of the next month by accessing the pseudo-components for the year, month, and day, using the field symbol.

1-2

1-3

SAP AG

BC401

15-21

6FUHHQ ([HUFLVH 
8QLW '\QDPLF 3URJUDPPLQJ 7RSLF '\QDPLF 2SHQ 64/ 6WDWHPHQWV DQG *HQHUDWLQJ 'DWD 2EMHFWV DW 5XQWLPH
At the conclusion of these exercises, you will be able to: Generate data objects dynamically Program dynamic SQL statements
Develop an ABAP program that can be used as an DG KRF data browser. You should be able to access only the table name on the selection screen. Delegate filtering by field content and similar functions to an SAP Grid Control instance, which you should also use to display the data. The purpose of this program is to make you more familiar with dynamic programming techniques. (There is already a 'DWD %URZVHU, installed as a standard tool in the ABAP Workbench.) 3URJUDP =%&B B&5($7(B'$7$B64/

0RGHO VROXWLRQ

7HPSODWH

SAPBC401_DYN7_CREATE_DATA_SQL SAPBC401_DYN6_CREATE_DATA_SQL

2-1

Copy your program, SAPBC401_DYN7_CREATE_DATA_SQL, giving it the name =%&B B&5($7(B'$7$B64/. Get to know the source code and runtime behavior of this program. Then implement the following concept: At runtime, the program should generate an internal table whose line type is compatible with the selected transparent table. The former should then be filled with the complete contents of the latter using a dynamic SQL statement. The internal table filled in this way should then be passed to a SAP Grid Control instance. Define a reference for the internal table that will be generated (we suggest the name UHIBLWDE). At program runtime, generate the internal table. At this time, it is known which line type must be used. The table must be a standard table with a non-unique standard key.

2-2

2-3

2-4

SAP AG

BC401

15-22

2-5

Assign a generically-typed field symbol to the generated internal table (suggested name: IVBLWDE!), so that its contents can be accessed. Before calling the screen, insert an Open SQL statement that copies the complete contents of the selected transparent table into the internal table using an array fetch. If an error occurs, make sure the program terminates. Generate a SAP Grid Control instance within the PBO module that you have created, ,1,7B&21752/6B. Again, if an error occurs, make sure the program terminates.
The relevant class is called &/B*8,B$/9B*5,'.

2-6

2-7

2-8

Pass the internal table filled in this way to this SAP Grid Control instance. If an error occurs, make sure the program terminates.
The relevant method is called 6(7B7$%/(B)25B),567B',63/$<. You need only pass values to two parameters: The line type to ,B6758&785(B1$0( and the name of the internal table to ,7B2877$%.

SAP AG

BC401

15-23

6ROXWLRQ   2SWLRQDO
8QLW '\QDPLF 3URJUDPPLQJ 7RSLF &DVWLQJ 7\SHV

5(3257 VDSEFBG\QVBFDVWLQJ TYPE-POOLS col. TYPES: BEGIN OF st_flight_c, ... END OF st_flight_c, BEGIN OF st_flight, ... END OF st_flight, %(*,1 2) VWBGDWH \HDU 

(1' 2) VWBGDWH

GD\ 

PRQWK  7<3( Q 7<3( Q

7<3( Q

CONSTANTS c_number TYPE i VALUE 30. DATA: datastring set_string TYPE string, TYPE string,

wa_flight_c TYPE st_flight_c, wa_flight TYPE st_flight.

SAP AG

BC401

15-24

DATA: it_sets TYPE STANDARD TABLE OF string WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE c_number, it_flights TYPE SORTED TABLE OF st_flight WITH UNIQUE KEY fldate carrid connid INITIAL SIZE c_number, it_doubles TYPE SORTED TABLE OF st_flight WITH NON-UNIQUE KEY fldate carrid connid INITIAL SIZE c_number, it_col_flights TYPE bc401_t_flights_color, it_col_doubles LIKE it_col_flights, wa_col_flight LIKE LINE OF it_col_flights. ),(/'6<0%2/6 IVBGDWH! 7<3( VWBGDWH PARAMETERS: pa_date LIKE sy-datum, pa_alv AS CHECKBOX DEFAULT X.

LOAD-OF-PROGRAM. SDBGDWH

$66,*1 SDBGDWH 72 IVBGDWH! &$67,1* ,) IVBGDWH!PRQWK   (/6( IVBGDWH!PRQWK IVBGDWH!GD\





V\GDWXP

IVBGDWH!PRQWK  

(1',)

IVBGDWH!\HDU

IVBGDWH!PRQWK

IVBGDWH!\HDU  




AT SELECTION-SCREEN. IF pa_date < sy-datum. MESSAGE e085(bc401). ENDIF. START-OF-SELECTION. ...


SAP AG BC401 15-25

" date in the past

6ROXWLRQ 
8QLW 7RSLF '\QDPLF 2SHQ 64/ 6WDWHPHQWV DQG *HQHUDWH 'DWD 2EMHFWV DW 5XQWLPH '\QDPLF 3URJUDPPLQJ

2-1

The copy template displays a selection screen where you can enter the name of a transparent table. It then shows a screen that displays the contents of this table. From this screen, you can either navigate back to the selection screen or end the program. This runtime behavior is implemented using standard ABAP programming techniques. A docking container control instance that fills the whole screen has already been generated and attached to the screen.

2
REPORT

0RGHO VROXWLRQ 6$3%&B'<16B&5($7(B'$7$B64/


sapbc401_dyns_create_data_sql.

DATA: ok_code LIKE sy-ucomm, popans. DATA: ref_docking TYPE REF TO cl_gui_docking_container, UHIBDOY 7<3( 5() 72 FOBJXLBDOYBJULG '$7$ UHIBLWDE 7<3( 5() 72 GDWD ),(/'6<0%2/6 IVBLWDE! 7<3( $1< 7$%/( PARAMETERS pa_tab TYPE dd02l-tabname DEFAULT SPFLI.

SAP AG

BC401

15-26

START-OF-SELECTION. &5($7( '$7$ UHIBLWDE 7<3( 67$1'$5' 7$%/( 2) SDBWDE :,7+ 12181,48( '()$8/7 .(< $66,*1 UHIBLWDE! 72 IVBLWDE! 6(/(&7 )520 SDBWDE ,172 7$%/( IVBLWDE! ,) V\VXEUF !  0(66$*( D UIZ :,7+ WH[WQGW (1',) CALL SCREEN 100. *&--------------------------------------------------------* *& Module clear_ok_code OUTPUT * *&--------------------------------------------------------* MODULE clear_ok_code OUTPUT. CLEAR ok_code. ENDMODULE. " clear_ok_code OUTPUT *&--------------------------------------------------------* *& Module STATUS_0100 OUTPUT * *&--------------------------------------------------------* MODULE status_0100 OUTPUT. SET PF-STATUS ST100. SET TITLEBAR T100. ENDMODULE. " STATUS_0100 OUTPUT *&--------------------------------------------------------* *& Module init_controls_0100 OUTPUT * *&--------------------------------------------------------* MODULE init_controls_0100 OUTPUT. IF ref_docking IS INITIAL. CREATE OBJECT ref_docking EXPORTING SIDE extension EXCEPTIONS OTHERS . IF sy-subrc <> 0. MESSAGE a015(rfw). ENDIF. &5($7( 2%-(&7 UHIBDOY (;3257,1* LBSDUHQW (;&(37,216 HUURUBFQWOBFUHDWH HUURUBFQWOBLQLW HUURUBFQWOBOLQN HUURUBGSBFUHDWH 27+(56 
SAP AG

= DOCK_AT_LEFT = 2000 = 6

UHIBGRFNLQJ     
BC401 15-27

,) V\VXEUF !  0(66$*( D UIZ :,7+ WH[WDHU (1',) &$// 0(7+2' UHIBDOY!VHWBWDEOHBIRUBILUVWBGLVSOD\ (;3257,1* LBVWUXFWXUHBQDPH SDBWDE &+$1*,1* LWBRXWWDE IVBLWDE! (;&(37,216 LQYDOLGBSDUDPHWHUBFRPELQDWLRQ  SURJUDPBHUURU  WRRBPDQ\BOLQHV  27+(56   ,) V\VXEUF !  0(66$*( D UIZ  (1',) ENDIF. ENDMODULE. " init_controls_0100 OUTPUT

*&--------------------------------------------------------* *& Module leave_programm INPUT * *&--------------------------------------------------------* MODULE leave_programm INPUT. CLEAR popans. CALL FUNCTION POPUP_TO_CONFIRM_STEP EXPORTING textline1 = text-dml textline2 = text-rcn titel = text-cnc cancel_display = IMPORTING answer = popans. CASE popans. WHEN J. LEAVE PROGRAM. WHEN N. CLEAR ok_code. ENDCASE. ENDMODULE. " leave_programm INPUT

SAP AG

BC401

15-28

*&--------------------------------------------------------* *& Module user_command_0100 * *&--------------------------------------------------------* MODULE user_command_0100 INPUT. CASE ok_code. WHEN BACK. CLEAR popans. CALL FUNCTION POPUP_TO_CONFIRM_STEP EXPORTING textline1 = text-dml textline2 = text-rbk titel = text-bak cancel_display = IMPORTING answer = popans. CASE popans. WHEN J. LEAVE TO SCREEN 0. WHEN N. ENDCASE. WHEN OTHERS. ENDCASE.

SAP AG

BC401

15-29

Вам также может понравиться