Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3585

Re: Determine class name used for calling a static method

$
0
0

Bonjour,

 

it seems, that to be sure to go through the class constructor of lcl_sub class, you should not call an inherited method "directly"  (lcl_sub=>say_hello( ).).

Instead you should "encapsulate" this call (of inherited method) inside a method of lcl_sub class (that is not inherited) :

  • class lcl_sub : define a new static method (say_hello_encapsule) that will call the static super method (say_hello)

 

REPORT zhello.

 

CLASS lcl_super DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS say_hello.

    CLASS-METHODS class_constructor.

  PROTECTED SECTION.

    CLASS-DATA hello TYPE string.

ENDCLASS.

 

CLASS lcl_super IMPLEMENTATION.

  METHOD class_constructor.

    hello = `Hello`.

  ENDMETHOD.

  METHOD say_hello.

    WRITE hello.

  ENDMETHOD.

ENDCLASS.

 

CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.

  PUBLIC SECTION.

    CLASS-METHODS class_constructor.

    CLASS-METHODS say_hello_2.

    CLASS-METHODS say_hello_encapsule.
ENDCLASS.

 

CLASS lcl_sub IMPLEMENTATION.

  METHOD class_constructor.

    lcl_sub=>hello = `World`.

  ENDMETHOD.

  METHOD say_hello_2.

    WRITE lcl_sub=>hello.

  ENDMETHOD.

   METHOD say_hello_encapsule.
     lcl_sub=>say_hello( ). "call inherited method
   ENDMETHOD.                    "say_hello_encapsule

ENDCLASS.

 

START-OF-SELECTION.

  lcl_sub=>say_hello( ).     "Hello <-- lcl_sub class constructor not accessed!

  lcl_sub=>say_hello_encapsule( ). "World <-- lcl_sub class constructor is accessed

  lcl_super=>say_hello( ).   "Hello World

  lcl_sub=>say_hello_2( ).   "World <-- class constructor executed

  lcl_super=>say_hello( ).   "World (correct)


Viewing all articles
Browse latest Browse all 3585

Trending Articles