14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1

ABAP in Eclipse

(how the TYPE is chosen is black magic), or you can say that the variable is an

IMPORTING or CHANGING (or whatever) parameter, and then the method signature

in the class definition is changed accordingly.

1.2.7 Creating Class Constructors

A common object-oriented rule is that you should never create objects using the

CREATE OBJECT statement; rather, you should ha ve a factory method that gives

you the object. The advantage of this is that if your class variable is referenced to

an interface rather than a concrete class, then the factory method will decide the

exact subclass for you, which makes the program more resistant to change.

In ABAP in Eclipse, if you put yourcursor on the class name and press (CTRL) + (1)

then you get assorted options, including creating the factory method, creating a

class constructor, or creating an instance constructor (Figure 1.17).

Figure 1.17 Automatically Generating a Constructor

Moreover, if you have declared some variables in the class definition before you

do this, then a wizard pops up and asks you which of these variables you want to

have as IMPORTING parameters in the constructor. Therefore, if you had member

variables for a logging class and the in ventor name, for example, which you

wanted passed in every time a new instance was created, you would choose them

from the list presented. The generated result would look like Listing 1.12.

CLASS zcl_monster DEFINITION.

CONSTRUCTOR IMPORTING i_logger TYPE REF TO zcl_bc_logger

i_inventor_name TYPE zde_inventor_name.

CLASS zcl_monster IMPLEMENTATION.

METHOD constructor.

Super->constructor( ).

me->logger = i_logger.

me->inventor_name = i_inventor_name.

ENDMETHOD.

Listing 1.12 Automatically Generated Constructor

60

www.allitebooks.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!